【ZOJ - 3780】 Paint the Grid Again (拓扑排序)
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
如果某一行存在x,则这一行必然会被横着涂一遍,同时上一次可以为竖着涂这一列;如果是o也可以得到相似的结论,所以这样可以建立起一个有向图,用拓扑排序判断即可。
BFS拓扑排序
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=505;
vector<int>G[N<<1];
int n;
char a[N];
queue<int>que;
bool del[N<<1],vis[N<<1];
int in[N<<1];
bool bfs(){
memset(del,false,sizeof(del));
memset(vis,false,sizeof(vis));
priority_queue<int,vector<int>,greater<int> >q;
for(int i=1;i<=(n<<1);i++) if(in[i]==0)
q.push(i),vis[i]=true;
if(q.size()==0) return false;
for (int i=1;i<=(n<<1);i++){
int cur = q.top();
q.pop();
if(del[cur]) return false;
del[cur]=true;que.push(cur);
for (int j=0;j<G[cur].size();j++){
int nxt=G[cur][j];
if(del[nxt]) return false;
in[nxt]--;
if(in[nxt]==0)
q.push(nxt);
}
}
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(in,0,sizeof(in));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[j].push_back(i+n);
in[i+n]++;
}
else{
G[i+n].push_back(j);
in[j]++;
}
}
}
if(!bfs()||que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
if(vis[x]) continue;
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(!que.empty()) printf(" ");
}
puts("");
}
return 0;
}
dfs
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=515;
vector<int>G[N<<1];
int id[N<<1],n;
char a[N];
int vis[N<<1];
queue<int>que;
bool dfs(int u)
{
vis[u]=-1;
sort(G[u].begin(),G[u].end());
for(int i=0; i<(int)G[u].size(); i++){
int v=G[u][i];
if(vis[v]<0) return false;
if(!vis[v]&&!dfs(v)) return false;
}
vis[u]=1;
if(id[u]!=n) que.push(u);
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(id,0,sizeof(id));
memset(vis,0,sizeof(vis));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[i+n].push_back(j); //横着,大于n的为横着的标号
id[j]++;
}
else{
G[j].push_back(i+n); //竖着,小于n为竖着的标号
id[i+n]++;
}
}
}
for(int i=1; i<=(n<<1); i++) if(!id[i]&&!vis[i]) if(!dfs(i)){
//无前驱的节点可以作为开始节点
while(!que.empty()) que.pop();
break;
}
if(que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(que.empty()) puts("");
else printf(" ");
}
}
return 0;
}
【ZOJ - 3780】 Paint the Grid Again (拓扑排序)的更多相关文章
- 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 E - Paint the Grid Again 拓扑排序
https://vjudge.net/problem/49919/origin 题意:给你n*n只出现O和X的字符阵.有两种操作,一种操作Ri将i行全变成X,一种操作Ci将i列全变成O,每个不同的操作 ...
- ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds Me ...
- ZOJ 3780 Paint the Grid Again
拓扑排序.2014浙江省赛题. 先看行: 如果这行没有黑色,那么这个行操作肯定不操作. 如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行 如果不全是 ...
- zjuoj 3780 Paint the Grid Again
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...
- ZOJ - 3780-Paint the Grid Again-(拓扑排序)
Description Leo has a grid with N × N cells. He wants to paint each cell with a specific color (eith ...
- 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 ...
- 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 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
随机推荐
- Xposed 集成 Android 6.0.1环境中,总结
由于工作需要,需要将xposed集成到android源码中,生成新的ROM就自带xposed的功能. 下面大体上说一下步骤和遇到的问题. 1.下载,并编译android源码,成功. 2.下载 http ...
- PHP简单实现单点登录功能示例
1.准备两个虚拟域名 127.0.0.1 www.openpoor.com127.0.0.1 www.myspace.com 2.在openpoor的根目录下创建以下文件 index.PHP 1 ...
- [USACO 2011 Nov Gold] Above the Median【逆序对】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...
- 51nod 1134最长递增子序列
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素 ...
- 修改SolrCloud在ZooKeeper中的配置文件操作记录
修改SolrCloud在ZooKeeper中的配置文件操作记录. 命令执行目录: /opt/solr-/server/scripts/cloud-scripts/ 1.下载配置文件 ./zkcli., ...
- 221 Maximal Square 最大正方形
在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...
- Tcpdump的用法
见 超级详细Tcpdump 的用法 http://www.itshouce.com.cn/linux/linux-tcpdump.html
- spark on yarn模式下内存资源管理(笔记1)
问题:1. spark中yarn集群资源管理器,container资源容器与集群各节点node,spark应用(application),spark作业(job),阶段(stage),任务(task) ...
- vba,excel,网址提取名字与链接url
'宏操作 Sub 复制超级链接() '这里控制读取A列的第1到10行,你根据自已的要求修改一下起始和结束行数 ).Hyperlinks.Count > ).Value = Cells(a, ). ...
- 批处理 reg add /?
C:\Users\Administrator>reg add /? REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [ ...