题目链接: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");
}
}
}

ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]的更多相关文章

  1. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  2. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  3. ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

    Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cel ...

  4. ZOJ 3780 Paint the Grid Again

    拓扑排序.2014浙江省赛题. 先看行: 如果这行没有黑色,那么这个行操作肯定不操作. 如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行 如果不全是 ...

  5. ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)

    Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows ...

  6. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  7. 2014 Super Training #4 D Paint the Grid Again --模拟

    原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结 ...

  8. ZOJ 3781 Paint the Grid Reloaded(BFS)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...

  9. zoj p3780 Paint the Grid Again

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5267 题意:Leo 有一个N*N 的格子,他又有一把魔法刷,这个刷子能把 ...

随机推荐

  1. UVA 1232 - SKYLINE(线段树)

    UVA 1232 - SKYLINE option=com_onlinejudge&Itemid=8&page=show_problem&category=502&pr ...

  2. Dubbo -- 系统学习 笔记 -- 示例 -- 多注册中心

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多注册中心 可以自行扩展注册中心,参见:注册中心扩展 (1) 多注册中心注册 比如 ...

  3. Dubbo -- 系统学习 笔记 -- 示例 -- 启动时检查

    示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发 ...

  4. Extjs定义的Fckeditor控件

    Ext.namespace('CRM.Panels'); //Ext.BoxComponent 这里继承是参考的Ext.form.Field CRM.Panels.Fckeditor = Ext.ex ...

  5. 使用InternetGetConnectedState判断本地网络状态(C#举例)

    函数原型:函数InternetGetConnectedState返回本地系统的网络连接状态. 语法: BOOL InternetGetConnectedState( __out LPDWORD lpd ...

  6. 转载nginx+uwsgi+django

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  7. PHP代码审计笔记--任意文件下载漏洞

    在文件下载操作中,文件名及路径由客户端传入的参数控制,并且未进行有效的过滤,导致用户可恶意下载任意文件.  0x01 客户端下载 常见于系统中存在文件(附件/文档等资源)下载的地方. 漏洞示例代码: ...

  8. iOS开发——iOS7(及以后版本) SDK自带二维码(含条形码)扫码、二维码生成

    本文转载至 http://www.cnblogs.com/leotangcn/p/4357907.html 现在很多APP都涉及了二维码扫码功能,这个功能简单实用,很多情况下用户乐于使用,现在本文带来 ...

  9. 页面调用Iframe中数据

    <iframe src="html的路径(至于MVC中cshtml直接路径好像是不行的,得使用action进行请求出来的路径)" id="iframechild&q ...

  10. thinkphp5 URL的访问

    ThinkPHP采用单一入口模式访问应用,对应用的所有请求都定向到应用的入口文件,系统会从URL参数中解析当前请求的模块.控制器和操作,下面是一个标准的URL访问格式: localhost/index ...