Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;
vector<int>a[];
vector<int>ab;
int b[],n,m;
void fun(int x)
{
if(x<=n)
printf("C%d",x);
else printf("R%d",x-n);
}
void work()
{
int now,i,ans=;
//for(i=0; i<=m; i++)sort(a[i].begin(),a[i].end());
queue<int>q;
ab.clear();
while(!q.empty())q.pop();
for(i=; i<=m; i++)if(!b[i])q.push(i),b[i]=-,ans++;
while(!q.empty())
{
now=q.front();
q.pop();
if(!b[now])
ab.push_back(now);
for(i=; i<a[now].size(); i++)
{
b[a[now][i]]--;
if(!b[a[now][i]])q.push(a[now][i]),ans++;
}
}
if(ans!=m)
{
printf("No solution\n");
return ;
}
for(i=; i<ab.size(); i++)
{
fun(ab[i]);
if(i==ab.size()-)
printf("\n");
else printf(" ");
}
}
int main()
{
int t,i,j;
char x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
m=*n;
for(i=; i<=m; i++)a[i].clear();
memset(b,,sizeof(b));
getchar();
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
x=getchar();
if(x=='X')
{
a[j].push_back(i+n);
b[i+n]++;
}
else
{
a[i+n].push_back(j);
b[j]++;
}
}
getchar();
}
work();
}
}