# Paint the Grid Again

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.

<h4< dd="">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.

<h4< dd="">Sample Input
2
2
XX
OX
2
XO
OX

<h4< dd="">Sample Output
R2 C1 R1
No solution
//用拓扑排序，将每一行i看成一个点，几位i,每一列j看成一个点 ,记为n+j;一共有2*n-1个点
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
vector<int>edge[550*2];
int inde[550*2];
int n;
int first[550*2];
void topo()
{
memset(first,0,sizeof(first));
priority_queue<int,vector<int>,greater<int> >q;
int i;
for(i=0;i<n*2;i++)
{
if(inde[i]==0)
{
q.push(i);
first[i]=1;//标记所有没有入度的点
}

}
vector<int>ans;
ans.clear();
while(!q.empty())
{
int node=q.top();
ans.push_back(node);
q.pop();
for(i=0;i<edge[node].size();i++)//由于建图时，必定从0~2*n-1,所以遍历时，必定是从字典序从小到大
{
inde[edge[node][i]]--;
if(inde[edge[node][i]]==0)
{
q.push(edge[node][i]);
}
}
}
if(ans.size()==n*2)
{
for(i=0;i<ans.size()-1;i++)
{
if(first[ans[i]]==1) continue;//对于入度为0的点，无需操作，比如样例1，本该为C2,R2,C1,R1，但C2的操作会被覆盖，不如不操作
if(ans[i]>=n)
{
printf("C%d ",ans[i]-n+1);
}else{
printf("R%d ",ans[i]+1);
}
}
if(ans[i]>=n)
{
printf("C%d\n",ans[i]-n+1);
}else
{
printf("R%d\n",ans[i]+1);
}
}else
{
printf("No solution\n");
}
}
int main()
{
int t;
while(~scanf("%d",&t))
{
int i,j;
while(t--)
{
for(i=0;i<550*2;i++)
edge[i].clear();
memset(inde,0,sizeof(inde));
scanf("%d",&n);
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<n;j++)
{
char x;
scanf("%c",&x);
if(x=='X')//若(i,j)点位X，则该点必定先刷第j列，再刷第i行
{
edge[n+j].push_back(i);
inde[i]++;
}
else//若(i,j)点位O，则该点必定先刷第i行，再刷第j列
{
edge[i].push_back(n+j);
inde[n+j]++;
}
}//建图
}
topo();
}
}
return 0;
}

• 评论

• 上一篇
• 下一篇