题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780

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

题意:

给出N*N的方格阵,规定一次只能涂抹一行或者一列,行只能涂X(黑色),列只能涂O(白色);

现给出最终每个方格的颜色,求最少几次涂抹可以达成。

(若有多种方案,输出字典序最小的,规定首先列C小于行R,在同为R或者同为C的情况下index越小越优先)

题解:

模拟涂抹过程,反着把每一次涂抹倒推回去。

最后一次涂抹必然导致一行全为X或者一列全为O,找出来,把这一行/列的颜色给清清除,反复如此即可。

AC代码:

#include<bits/stdc++.h>
using namespace std; int n;
int cell[][];
bool rvis[],cvis[];
int cntr,cntc; struct ANS{
char pos;
int idx;
}ans[*]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
char tmp[];
for(int i=;i<=n;i++)
{
scanf("%s",tmp+);
for(int j=;j<=n;j++)
{
if(tmp[j]=='X') cell[i][j]=;
else cell[i][j]=;
}
} cntr=cntc=n;
memset(rvis,,sizeof(rvis));
memset(cvis,,sizeof(cvis));
int cnt=;
bool haveSOL=;
while()
{
int rowOK=;
for(int r=n;r>=;r--)
{
if(rvis[r]) continue; bool ok=;
for(int j=;j<=n;j++)
{
if(cvis[j] || cell[r][j]==) continue;
ok=; break;
} if(ok)
{
//printf("R%d\n",r);
rvis[r]=;
cntr--;
ans[cnt++]=(ANS){'R',r};
rowOK=;
break;
}
} int colOK=;
if(!rowOK)
{
for(int c=n;c>=;c--)
{
if(cvis[c]) continue; bool ok=;
for(int i=;i<=n;i++)
{
if(rvis[i] || cell[i][c]==) continue;
ok=; break;
} if(ok)
{
//printf("C%d\n",c);
cvis[c]=;
cntc--;
ans[cnt++]=(ANS){'C',c};
colOK=;
break;
}
}
} if(cntr== || cntc==)
{
haveSOL=;
break;
}
if(colOK+rowOK==)
{
printf("No solution\n");
break;
}
} if(haveSOL)
{
for(int i=cnt-;i>=;i--)
{
if(i!=cnt-) printf(" ");
printf("%c%d",ans[i].pos,ans[i].idx);
}
printf("\n");
}
}
}

最新文章

  1. Ubuntu搭建NFS
  2. IDA插件栈字符串识别插件
  3. Spring aop 原始的工作原理的理解
  4. 聚合函数:sum,avg,max,min,count
  5. BZOJ 3809 莫队+(分块|BIT)
  6. DataGridView 添加行号
  7. Linux编辑器的选择使用
  8. 【POJ1195】【二维树状数组】Mobile phones
  9. java 异常处理与返回
  10. 委托(4).net 3.5中的委托
  11. java 常用工具整理
  12. vue-cli3使用webpack-spritesmith配置雪碧图
  13. SpringBoot(12) SpringBoot创建非web应用
  14. java.net.ServerSocket 解析
  15. Guideline 2.1 - Information Needed
  16. laravel 数据库迁移转 sql 语句
  17. 彻底解决Odoo8.0单时区应用中的时区问题
  18. Linux tar This does not look like a tar archive
  19. HDU 1281 棋盘游戏 【二分图最大匹配】
  20. 抽象类(abstract class)和接口(interface)的异同

热门文章

  1. Mybatis数据库连接报错:对实体 &quot;characterEncoding&quot; 的引用必须以 &#39;;&#39; 分隔符结尾
  2. laravel 5.3升级5.4
  3. HttpClient(四)-- 使用代理IP 和 超时设置
  4. javascript 资料
  5. Splash runjs() 方法
  6. 手把手让你实现开源企业级web高并发解决方案(lvs+heartbeat+varnish+nginx+eAccelerator+memcached)
  7. 开源项目:MMTweenAnimation
  8. &lt;转&gt;机器学习系列(9)_机器学习算法一览(附Python和R代码)
  9. Delphi应用程序的调试(五)其他调试工具
  10. windows下php使用protobuf