题目链接: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. 8 -- 深入使用Spring -- 3...1 Resource实现类

    8.3.1 Resource实现类 Resource接口是Spring资源访问的接口,具体的资源访问由该接口的实现类完成. Spring提供的Resource接口的实现类: ⊙ UrlResource ...

  2. NetBpm如何指定下一流程处理人(8)

    NETBPM如何指定下一流程处理人 本着“软件以应用为本”(潘加宇老师对我的影响在这一点上很深.)的原则,我为ERP搭建了一个用NETBPM作的支持网站,想着能够看着软件得以应用,自是非常高兴. 不过 ...

  3. CentOS 6.3下搭建Web服务器

    准备前的工作: 1.修改selinux配置文件(/etc/sysconfig/selinux) 关闭防火墙 (1)把SELINUX=enforcing注释掉 (2)并添加SELINUX=disable ...

  4. PHP压缩html网页代码原理(清除空格,换行符,制表符,注释标记)

    本博启用了一个叫wp super cache的页面压缩工具, 源代码没有去查看,不过原理很简单. 我们可以自己动手书写一个压缩脚本. 清除换行符,清除制表符,去掉注释标记 .它所起到的作用不可小视. ...

  5. 有人在群里问mysql如何选择性更新部分条件的问题

    有人在群里问这个问题 update xt_kh set zhye=zhye+1,hzyj=hzyj+1 where dlgh='kiss0451' and hzms=1 如果这样写 hzms不等于1的 ...

  6. Python3.X如何下载安装urllib2包 ?

    python 3.X版本不需要安装urllib2包,因为urllib和urllib2包集合成在一个包了 那现在问题是: 在python3.x版本中,如何使用:urllib2.urlopen()? 答: ...

  7. map 集合的遍历

    List<Map<String,Object>> autoReplyList= wechatService.queryAutoReplyByOrg(orgId); for(Ma ...

  8. Linq-string判断忽略大小写

    Coupon203Play play = dbContext.Coupon203Plays.Where(u => u.VerifyCode.Equals(verifyCode,StringCom ...

  9. 【Laravel5.5】 Laravel 在views中加载公共页面怎么实现

    背景: 在做后台功能时候,我们需要把头部和尾部摘出来作为公共模板使用 1:我们使用了Blade模板,并创建一个header.blade.php作为通用的模板.将子页面作为yield输出: header ...

  10. 【大数据系列】win10上安装hadoop开发环境

    为了方便采用了Cygwin模拟linux环境的方法 一.安装JDK以及下载hadoop hadoop官网下载hadoop http://hadoop.apache.org/releases.html  ...