直击题目:点击打开链接
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.
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.
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.
2 2 XX OX 2 XO OX
R2 C1 R1 No solution题目大意:
有一个空白矩阵,然后画出最终的状态,问你画的步骤(步骤输出字典序最小的‘R’>'C').刷的规则为:要么刷一行,要么刷一列,刷一行时,这一行全是黑色(‘X’),刷一列的时候这一列全是白色(‘O’).新的颜色会覆盖旧的颜色。如果不能刷出所给的这种状态,则输出No solution.
题解:
因为一刷刷一行或一列,那么最后一刷要么是一行要么是一列。而且题目说了每行每列最多刷一次。那么就倒着往前推就行了。先进行行在进行列(字典序最小)。找出一行没有‘O’的就表示走到这一步了。同理,找出一列没有‘X’。那么当找出这样的行和列的时候就把他变为空白或者变为相反的颜色,但是变为相反的颜色会有缺点。因每一点最多被刷两次,如果变为相反的颜色容易混论而且还不知道该点被刷了几次。所以不如直接变成空白。就这样直到全是空白为止。或者没有符合要求的行列为止。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
struct node
{
char ch;
int id;
}q[255555];
char s[555][555];
int visc[555],visr[555];
bool work(int n){
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(s[i][j]!='.')return true;
}
}
return false;
}
int main()
{
int t,top,n;
scanf("%d",&t);
while(t--)
{
memset(visc,0,sizeof(visc));
memset(visr,0,sizeof(visr));
top=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
}
int flag=1;
int sum,num;
sum=num=1;
while(work(n))
{
if(sum==0&&num==0)
{
flag=0;
break;
}
num=0;
for(int i=n-1;i>=0;i--)
{
if(visr[i])continue;
int ok=1;
for(int j=0;j<n;j++)
{
if(s[i][j]=='O')
{
ok=0;
break;
}
}
if(ok)
{
num++;
for(int j=0;j<n;j++)
{
s[i][j]='.';
}
q[top].ch='R';
q[top++].id=i+1;
visr[i]=1;
}
}
if(!work(n))break;
sum=0;
for(int j=n-1;j>=0;j--)
{
if(visc[j])continue;
int ok=1;
for(int i=0;i<n;i++)
{
if(s[i][j]=='X')
{
ok=0;
break;
}
}
if(ok)
{
sum++;
for(int i=0;i<n;i++)
{
s[i][j]='.';
}
q[top].ch='C';
q[top++].id=j+1;
visc[j]=1;
}
}
}
if(!flag)printf("No solution\n");
else
{
for(int i=top-1;i>=0;i--)
{
if(i==0)
printf("%c%d\n",q[i].ch,q[i].id);
else
printf("%c%d ",q[i].ch,q[i].id);
}
}
}
return 0;
}