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 Tindicating 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
题意:给你一个n*n的矩阵,共有两种操作:①将每行染成黑色②将每列染成白色。每行、每列只能操作一次。最初矩阵颜色既不是黑色也不是白色,现在给你一个已经涂好色的矩阵,问你至少操作几次才能从初始矩阵涂成给你的矩阵。
思路:对于矩阵中单个元素来说,如果最终颜色是黑色,那么肯定是先进行染白, 再进行染黑操作。如果最终颜色是白色,那么肯定是先进行染黑, 再进行染白操作。将这些信息建成图,然后用拓扑排序,进行排序并字典序输出(利用优先队列)
入度为零的点,是不能操作的,因为这些点并不能从其他状态转化过来
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=510;
char s[maxn][maxn];
struct node{
int id;
int next;
}side[maxn*maxn];
int head[maxn*maxn],ans[maxn*maxn],in[maxn*maxn],book[maxn*maxn],cnt,n,cont;
void add(int x,int y)
{
side[cnt].id=y;
side[cnt].next=head[x];
head[x]=cnt++;
}
void init()
{
memset(in,0,sizeof(in));
memset(book,0,sizeof(book));
memset(head,-1,sizeof(head));
cnt=0;
}
void topu()
{
priority_queue<int ,vector<int>,greater<int> > q;
for(int i=0;i<2*n;i++)
if(in[i]==0)
{
q.push(i);
book[i]=1;
}
cont=0;
while(q.size())
{
int tmp=q.top();
q.pop();
ans[++cont]=tmp;
for(int i=head[tmp];i!=-1;i=side[i].next)
{
int y=side[i].id;
in[y]--;
if(in[y]<=0)
q.push(y);
}
}
}
int main()
{
int t,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(int i=0;i<n;i++)
scanf("%s",s[i]);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(s[i][j]=='O')
{
add(i,n+j);
in[n+j]++;
}
else
{
add(j+n,i);
in[i]++;
}
}
topu();
int flag=0;
for(int i=0;i<2*n;i++)
if(in[i])
{
flag=1;
break;
}
if(flag) printf("No solution\n");
else
{
for(int i=1;i<=cont;i++)
{
if(book[ans[i]]) continue;
if(i==cont)
{
if(ans[i]>=n)
printf("C%d\n",ans[i]-n+1);
else
printf("R%d\n",ans[i]+1);
}
else
{
if(ans[i]>=n)
printf("C%d ",ans[i]-n+1);
else
printf("R%d ",ans[i]+1);
}
}
}
}
return 0;
}