• Paint the Grid Again ZOJ


    Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

    []   [Go Back]   [Status]  

    Description

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).

    Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color. Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.

    Please write a program to find out the way to paint the grid.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the color of the cells should be painted to, after Leo finished his painting.

    Output

    For each test case, output "No solution" if it is impossible to find a way to paint the grid.

    Otherwise, output the solution with minimum number of painting operations. Each operation is either "R#" (paint in a row) or "C#" (paint in a column), "#" is the index (1-based) of the row/column. Use exactly one space to separate each operation.

    Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k, the first k - 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have the same type, the one with smaller index of row/column is the lexicographically smaller one.

    Sample Input

    2
    2
    XX
    OX
    2
    XO
    OX
    

    Sample Output

    R2 C1 R1
    No solution

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #include <math.h>
     6 #include <map>
     7 #include <queue>
     8 #include <vector>
     9 using namespace std;
    10 vector<int>a[1200];
    11 vector<int>ab;
    12 int b[1200],n,m;
    13 void fun(int x)
    14 {
    15     if(x<=n)
    16         printf("C%d",x);
    17     else printf("R%d",x-n);
    18 }
    19 void work()
    20 {
    21     int now,i,ans=0;
    22     //for(i=0; i<=m; i++)sort(a[i].begin(),a[i].end());
    23     queue<int>q;
    24     ab.clear();
    25     while(!q.empty())q.pop();
    26     for(i=1; i<=m; i++)if(!b[i])q.push(i),b[i]=-1,ans++;
    27     while(!q.empty())
    28     {
    29         now=q.front();
    30         q.pop();
    31         if(!b[now])
    32             ab.push_back(now);
    33         for(i=0; i<a[now].size(); i++)
    34         {
    35             b[a[now][i]]--;
    36             if(!b[a[now][i]])q.push(a[now][i]),ans++;
    37         }
    38     }
    39     if(ans!=m)
    40     {
    41         printf("No solution
    ");
    42         return ;
    43     }
    44     for(i=0; i<ab.size(); i++)
    45     {
    46         fun(ab[i]);
    47         if(i==ab.size()-1)
    48             printf("
    ");
    49         else printf(" ");
    50     }
    51 }
    52 int main()
    53 {
    54     int t,i,j;
    55     char x;
    56     scanf("%d",&t);
    57     while(t--)
    58     {
    59         scanf("%d",&n);
    60         m=2*n;
    61         for(i=0; i<=m; i++)a[i].clear();
    62         memset(b,0,sizeof(b));
    63         getchar();
    64         for(i=1; i<=n; i++)
    65         {
    66             for(j=1; j<=n; j++)
    67             {
    68                 x=getchar();
    69                 if(x=='X')
    70                 {
    71                     a[j].push_back(i+n);
    72                     b[i+n]++;
    73                 }
    74                 else
    75                 {
    76                     a[i+n].push_back(j);
    77                     b[j]++;
    78                 }
    79             }
    80             getchar();
    81         }
    82         work();
    83     }
    84 }
    View Code
  • 相关阅读:
    javascript 学习笔记714章
    数据库设计的四个范式
    【转】utf8的中文是一个汉字占三个字节长度
    java 中文url的解决
    so动态链接库的使用
    linux常用命令
    控制台编译Qt程序
    构造函数初始化列表 组合类构造函数
    const volatile
    std::pair
  • 原文地址:https://www.cnblogs.com/ERKE/p/3676201.html
Copyright © 2020-2023  润新知