ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接: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题]的更多相关文章
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- 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 ...
- 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 ...
- ZOJ 3780 Paint the Grid Again
拓扑排序.2014浙江省赛题. 先看行: 如果这行没有黑色,那么这个行操作肯定不操作. 如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行 如果不全是 ...
- 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 ...
- zjuoj 3780 Paint the Grid Again
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...
- 2014 Super Training #4 D Paint the Grid Again --模拟
原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结 ...
- 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 ...
- zoj p3780 Paint the Grid Again
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5267 题意:Leo 有一个N*N 的格子,他又有一把魔法刷,这个刷子能把 ...
随机推荐
- SPREAD for Windows Forms 下箭头追加行
''' <summary> ''' 下矢印の動作クラス ''' </summary> ''' <remarks></remarks> Public Cl ...
- GSAP JS基础教程--动画的控制及事件
好多天没有写无博文啦,今天无聊就再写一下! 今天要讲的是TweenLite的一些事件以及,TweenLite动画的控制,TweenMax类似,请自行参考官方文档:http://api.greensoc ...
- Yarn执行流程
在Yarn中,JobTracker被分为两部分:ResourceManager(RM)和ApplicationMaster(AM). MRv1主要由三部分组成:编程模型(API).数据处理引擎(Map ...
- SpringBoot(一)-- 知识点介绍
一.简介 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置.简单来说,它提供了一堆依赖打包,并 ...
- sutdio中替换全局方法
Ctril Shift R Text to find :是要搜索的内容或者要被替换的内容 Replace with :是要替换的内容 Preview:是可以看到预览 在Scope的选项卡里 Whole ...
- java如何调用另一个包里面的类
我现在有两个包: 我想在Boss里面实现对Employee的调用, Employee.java: package payroll2; public class Employee { public vo ...
- PHP域名解析(一个IP绑多域名)----看看可以,并不值得借鉴
PHP域名解析(一个IP绑多域名)----看看可以,并不值得借鉴 好处当然是不用买多网卡.不用设置其它端口为WEB端口了,一张网卡上.都用同一个80端口建很多网站. 假设有三个域名: [url ...
- MySQL使用查询结果生成临时表
MySQL中不支持对同一个表使用其查询结果更新or删除本表内数据(也就是update或delete后的where条件为针对相同表的select),解决方案是创建临时表做过度保存中间数据: 可以直接使用 ...
- es6 - class的学习
http://es6.ruanyifeng.com/#docs/class:class Person { constructor{ //构造函数,里边放不被继承的私有属性和方法 this.proper ...
- vuex - 辅助函数学习
官网文档: https://vuex.vuejs.org/zh-cn/api.html 最底部 mapState 此函数返回一个对象,生成计算属性 - 当一个组件需要获取多个状态时候,将这些状态都声 ...