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 的格子,他又有一把魔法刷,这个刷子能把 ...
随机推荐
- 基于JavaScript判断浏览器到底是关闭还是刷新(超准确)
这篇文章主要介绍了基于JavaScript判断浏览器到底是关闭还是刷新(超准确)的相关资料,需要的朋友可以参考下 本文是小编总结的一些核心内容,个人感觉对大家有所帮助,具体内容请看下文: 页面加载时只 ...
- 2、一、Introduction(入门):1、Application Fundamentals(应用程序基础)
一.Introduction(入门) 1.Application Fundamentals(应用程序基础) Android apps are written in the Java programmi ...
- js获取视频截图
参考:https://segmentfault.com/q/1010000006717959问题:a.获取的好像是第一帧的图?第一帧为透明图时,获取的个透明图片b.得先加载视频到video,做视频上传 ...
- Java -- Java 类集 -- 目录
13.1 认识类集 13.1.1 基本概念 13.1.2 类集框架主要接口 13.2 Collection接口 13.2.1 Collection接口的定义 13.2.2 Collection子接口的 ...
- 【GIS】GDAL Python 影像裁剪
# -*- coding: utf-8 -*- """ Created on Fri Nov 30 11:45:03 2018 @author: Administrato ...
- linux修改文件所属用户和用户组
使用chown命令可以修改文件或目录所属的用户: 命令:chown 用户 目录或文件名 例如:chown testAdmin /home/work (把home目录下的xua ...
- SVN 快速入门
一.SVN 简介 (1) SVN 是 Subversion 的缩写,是一个开源的版本控制系统(2) SVN 基于 C/S 架构,有一台中央服务器,多台客户端通过网络从中央服务器拉取或提交代码,以此达到 ...
- Python中执行外部命令
有很多需求需要在Python中执行shell命令.启动子进程,并捕获命令的输出和退出状态码,类似于Java中的Runtime类库. subprocess模块的使用: Python使用最广泛的是标准库的 ...
- java web当中表单提交到后台出现乱码的解决方法
1.如果提交方式为post,想不乱码,只需要在服务器端设置request对象的编码即可,客户端以哪种编码提交的,服务器端的request对象就以对应的编码接收,比如客户端是以UTF-8编码提交的,那么 ...
- 【LeetCode OJ】Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return ...