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组成的矩阵,可以选择某一连通块变成另一 ...
随机推荐
- cannot create windows service for mysql
这安装完mysql启动的时候总是弹出不能启动错误,解决办法: 先卸载掉MySQL干净:把关于MySQL所有目录及文件都删除掉,还有把注册表删除干净.在注册表找到以下项目并删除:HKEY_LOCAL_M ...
- C、C++、java的区别
经常听到有人在抱怨这个语言哪里哪里 不好,那个语言又是如何的优秀.对于这样 的牢骚,我只是一笑而过. 就我而言,语言只是工具,没有好坏之 分.只要你采用相应的语言,完成对应的工 作,那你的目标就完成了 ...
- WordPress 邮箱防抓取
现在网络上有很多爬虫,专门四处搜集网站代码中出现的邮箱,搜集到了之后就批量出售或者发送垃圾邮件.很多人都把邮箱中的 “@” 换成 “#”,但这样对用户不太方便,而且这种方法很多机器人都可以识破,同样被 ...
- Power oj2470/DFS
题目链接 2469: C 小Y的难题(1) Time Limit: 1000 MS Memory Limit: 65536 KB Total Submit: 9 Accepted: 7 Page Vi ...
- div.2/D. As Fast As Possible<数学题,二分>
题目连接 题意: n个学生出去玩,要前进一段距离,租了一辆可以载k个人的车,问到达到目的地的最短时间. cin: n,l,v1,v2,k. £:所有人一起到达终点的时候时间最短. £:所有人走路和坐车 ...
- php 四种基础算法 ---- 选择排序法
2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...
- JS中获取页面单选框radio和复选框checkbox中当前选中的值
单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...
- wordpress安装插件--su
Add to Any: Subscribe Button 让读者方便的订阅你的博客到任何Feed阅读器 Google XML Sitemaps 生成完全兼容各大搜索引擎的Sitemaps/网站地图. ...
- Java-老夫对泛型的理解。。
什么是泛型,在我看来 泛型就是能够限制类,方法,数组-变量的类型 比如说 List<String,int> list = new List<String,int> 就是有尖括 ...
- ibus用上搜狗拼音词库
1.下载搜狗拼音词库 wget http://hslinuxextra.googlecode.com/files/sougou-phrases-full.7z 2.用sougou-phrases-fu ...