zjuoj 3780 Paint the Grid Again

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780

Paint the Grid Again


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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


Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest

分析;

给定n*n的矩阵

有2个操作:

1、把一行变成X

2、把一列变成O

限制:每行(每列)只能变一次

给定结果图,开始时图无O,X,问最小操作步数(且字典序最小)

思路:

对于(i,j)这个格子,若现在涂的是 O,则去掉O这排,(让这排都变成X即可)可以直接认为(i,j)是X

所以当某排的X攒满n个时,就可以去掉这排X

直接模拟即可

先把所有 全为O或全为X的 行和列预处理出来,放到一个栈里

因为字典序最小,所以先处理列再处理行,第i列 用i+n表示, 第i行用i表示
然后给栈排个序,这样就得到处理当前情况的顺序, 入个队列,然后一个个去掉就可以了。

AC代码:

  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<string.h>
  5 #include<math.h>
  6 #include<vector>
  7 #include<queue>
  8 #include<set>
  9 using namespace std;
 10 #define N 1005
 11 vector<int>ans;
 12 char mp[N][N];
 13 int n, h[N], l[N];
 14 int yes[N];
 15 int Stack[N], Top;
 16
 17 void init(){
 18     ans.clear();
 19     memset(yes, 0, sizeof yes);
 20     memset(h, 0, sizeof h);
 21     memset(l, 0, sizeof l);
 22     Top = 0;
 23 }
 24 bool cmp(int a,int b){return a>b;}
 25 //0-n-1 表示列 n-2n-1 表示行
 26 void work(){
 27     sort(Stack, Stack+Top, cmp);
 28     queue<int>q;
 29     int i, j;
 30     for(int i = 0; i < Top; i++){
 31         q.push(Stack[i]), ans.push_back(Stack[i]); yes[Stack[i]]=-1;
 32     }
 33     Top = 0;
 34     while(!q.empty()){
 35         int u = q.front(); q.pop();
 36         Top = 0;
 37         if(u<n)
 38             for(j = 0; j < n; j++)
 39             {
 40                 mp[j][u] = ‘X‘;
 41                 h[j]++;
 42                 if(yes[j+n]!=-1 && h[j]==n)Stack[Top++] = j+n;
 43             }
 44         else {
 45             u-=n;
 46             for(j = 0; j < n; j++)
 47             {
 48                 mp[u][j] = ‘O‘;
 49                 l[j]++;
 50                 if(yes[j]!=-1 && l[j]==n)Stack[Top++] = j;
 51             }
 52         }
 53             sort(Stack, Stack+Top, cmp);
 54             for(i = 0; i < Top; i++)q.push(Stack[i]), yes[Stack[i]] = -1, ans.push_back(Stack[i]);
 55     }
 56     for(int i = 0; i < 2*n; i++)if(yes[i]==0){puts("No solution");return;}
 57     for(int i = ans.size()-1; i>=0; i--){
 58         int u = ans[i];
 59         if(u>=n)printf("R"), u-=n;
 60         else printf("C");
 61         printf("%d",u+1);
 62         i ? printf(" ") : puts("");
 63     }
 64 }
 65 int main(){
 66     int T;scanf("%d",&T);
 67     int i, j;
 68     while(T--){
 69         scanf("%d",&n);
 70         init();
 71         for(i=0;i<n;i++)scanf("%s",mp[i]);
 72         for(i=0;i<n;i++)
 73         {
 74             for(j = 0; j<n; j++)if(mp[i][j]==‘X‘)h[i]++;
 75             if(h[i]==n) Stack[Top++] = i+n;
 76             else if(h[i]==0) yes[i+n] = -1;
 77         }
 78         for(i=0;i<n;i++)
 79         {
 80             for(j = 0; j<n; j++)if(mp[j][i]==‘O‘)l[i]++;
 81             if(l[i]==n) Stack[Top++] = i;
 82             else if(l[i]==0) yes[i] = -1;
 83         }
 84         if(Top==0){puts("No solution");continue;}
 85         work();
 86     }
 87     return 0;
 88 }
 89 /*
 90 99
 91 1
 92 O
 93 3
 94 OOO
 95 OOO
 96 OOO
 97
 98 2
 99 XX
100 OX
101 2
102 XO
103 OX
104
105
106 */

 

时间: 2024-11-09 15:57:00

zjuoj 3780 Paint the Grid Again的相关文章

zoj 3780 Paint the Grid Again (拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 an

ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 an

zjuoj 3773 Paint the Grid

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Leo has a grid with N × N cells. He wants to paint each cell either black or white. After he finishe

2014 Super Training #4 D Paint the Grid Again --模拟

原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结果后来发现就是一个简单的模拟啊,因为每行每列都只能消去一次,直接慢慢消去就好了,因为按字典序从小到大,那就按行从大到小,列从大到小的顺序来消就可以了,消完了标记一下,把那行或者那列的元素都赋为一个特殊的字符'*'即可. 还是应该多思考啊,不要被题目吓到了.探寻题目的本质才能更好的解题. 代码: #i

ZOJ 3781 Paint the Grid Reloaded (最短路)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 在n*m矩阵的图定义连通区域为x值或y值相同且颜色相同的连通,连通具有传递性 每次可以把一个连通区域颜色反转(O变X,X变O) 问把所有块的颜色变为X最小的步数 方法: 很巧妙的最短路问题,先建图,然后以每个顶点为起点,找单源最短路的最大的值(也就是最深的深度),然后这个值取min 建图:相邻的块连边权1的边(即:通过1次反转可以使得两个连通块变为一个连通块

Paint the Grid Reloaded(缩点,DFS+BFS)

Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both A and B. Leo

ZOJ 3781 Paint the Grid Reloaded(BFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in

zoj 3781 Paint the Grid Reloaded (比较隐含的最短路)

Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are