ZOJ 3780 Paint the Grid Again
拓扑排序。2014浙江省赛题。
先看行:
如果这行没有黑色,那么这个行操作肯定不操作。
如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行
如果不全是黑色,那么看这一行的每一个元素,如果有白色的,那么白色所在列向这一行连边。
再看列:
与看行类似,不再赘述。
建图建完之后进行拓扑排序。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = + ;
int T, n;
char s[maxn][maxn];
int G[ * maxn][ * maxn];
vector<int>g[ * maxn];
bool flag[ * maxn];
int tot[ * maxn];
int w[maxn], b[maxn];
struct cmp{
bool operator ()(int &a, int &b){
return a>b;//最小值优先
}
};
priority_queue<int, vector<int>, cmp>Q;//最小值优先
vector<int>ans; void init()
{
memset(G, -, sizeof G);
for (int i = ; i <= * n; i++) g[i].clear();
memset(flag, , sizeof flag);
memset(tot, , sizeof tot);
while (!Q.empty()) Q.pop();
ans.clear();
for (int i = ; i<n; i++)
{
int sum = ;
for (int j = ; j<n; j++) if (s[i][j] == 'X') sum++;
b[i] = sum;
} for (int j = ; j<n; j++)
{
int sum = ;
for (int i = ; i<n; i++) if (s[i][j] == 'O') sum++;
w[j] = sum;
}
} void read()
{
scanf("%d", &n);
for (int i = ; i<n; i++) scanf("%s", s[i]);
} void work()
{
for (int i = ; i<n; i++)
{
if (b[i] == ) flag[i + n] = ;
else if (b[i] == n)
{
for (int j = ; j<n; j++) if (w[j]) G[j][i + n] = ;
}
else
{
for (int j = ; j<n; j++) if (s[i][j] == 'O') G[i + n][j] = ;
}
} for (int j = ; j<n; j++)
{
if (w[j] == ) flag[j] = ;
else if (w[j] == n)
{
for (int i = ; i<n; i++) if (b[i]) G[i + n][j] = ;
}
else
{
for (int i = ; i<n; i++) if (s[i][j] == 'X') G[j][i + n] = ;
}
} for (int x = ; x< * n; x++)
for (int y = ; y< * n; y++)
if (G[x][y] == )
{
tot[y]++; g[x].push_back(y);
} for (int i = ; i< * n; i++) if (flag[i] && tot[i] == ) Q.push(i); while (!Q.empty())
{
int top = Q.top(); Q.pop();
ans.push_back(top);
for (int i = ; i<g[top].size(); i++) {
tot[g[top][i]]--;
if (tot[g[top][i]] == ) Q.push(g[top][i]);
}
} if (ans.size()<n) printf("No solution\n");
else
{
for (int i = ; i<ans.size(); i++)
{
if (ans[i] >= && ans[i] <= n - ) printf("C%d", ans[i] + );
else printf("R%d", ans[i] - n + ); if (i<ans.size() - ) printf(" ");
else printf("\n");
}
} } int main()
{
scanf("%d", &T);
while (T--)
{
read();
init();
work();
}
return ;
}
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 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds Me ...
- zjuoj 3780 Paint the Grid Again
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...
- 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 ...
- zoj p3780 Paint the Grid Again
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5267 题意:Leo 有一个N*N 的格子,他又有一把魔法刷,这个刷子能把 ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
随机推荐
- easyui easyui-filebox 显示中文
<input class="easyui-filebox" name="uploadFile" id="uploadFileid" d ...
- Light OJ 1008
找规律. 首先令n=sqrt(s),上取整.讨论当n为偶数时,若n*n-s<n则x=n,y=n*n-s+1否则x=-n*n+2*n+s-1,y=n;如果n为奇数,交换x,y即可,对称的. Sam ...
- HDU 2671 Can't be easier
简单的几何题目 点(a,b)关于直线Ax+By+C=1对称点的公式 #include<cstdio> #include<cstring> #include<cmath&g ...
- Json.net对数据的解析
在官网下载Json.net文件后,解压完将Net20下面的DLL复制到Assets目录下. using UnityEngine; using System.Collections; using New ...
- 休眠唤醒不断开wifi.
文件: /home/mxy/code/v1/frameworks/base/services/java/com/auto/opandora/Opandora.java 屏蔽掉: 957 SetWifi ...
- 转 oraenv
代码如下: lines, characters #!/bin/sh # # Get the machine type and then set up ORATAB and TNS_ADMIN vari ...
- 修改smali文件,重打包,实现调用第三方SO文件
Java代码: static{ // //loadlibary里 要把SO文件名的lib和后缀去掉.libfgma.so --> fgma System.loadLibrary("fg ...
- 解决BT5不能使用putty连接问题
root@bt:~# cd /etc/sshroot@bt:/etc/ssh# sshd-generate Generating public/private rsa1 key pair.Your i ...
- FOJ 2206 函数求解
水题 /* *********************************************** Author :Zhou Zhentao Email :774388357@qq.com C ...
- Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconn
使用MySQL执行update的时候报错: MySQL 在使用mysql执行update的时候,如果不是用主键当where语句,会报如下错误,使用主键用于where语句中正常. 异常内容: ...