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组成的矩阵,可以选择某一连通块变成另一 ...
随机推荐
- js中子页面父页面方法和变量相互调用
(1)子页面调用父页面的方法或者变量: window.parent.方法()或者变量名window.parent相当于定位到父页面 之后的操作和在父页面中写代码一样写 window.parent.aa ...
- .bash_profile和.bashrc的什么区别
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置./etc/bashrc:为每一个运 ...
- lucene 编辑距离
lucene 编辑距离实际上表明良好个不同的字符串需要经过多少次编辑和变换才能转换成对方.通常的编辑行为包括了增加一个检索项目,删除一个检索项,修改一个检索项
- Oracle Sql优化之分层查询(connect by)
1.对于表中行与行存在父子关系时,可以通过connect by查询方式,查询行与行之间的父子关系 ),'-')|| empno as tempno, ename,mgr,level, decode(, ...
- 设置批量商品优惠、如何修改ZenCart产品显示图片的大小
利用下面的方法,可以实现: 买一送一.买一件第二件5折.买三件优惠10%等功能. 管理页面 - 商品管理 - 价格管理 - (选择商品) - 编辑 - 添加空白折扣. 应用上面的办法,能够完成:买一送 ...
- zf-表单填写以及相关业务流程
这里是表单设置的地方 这是字段信息,如果设置了共享申请人名称的话 那么登记办件时,输入的申请人信息,会自动写入到这个字段中 还有这种,没设置共享的,那么就是申请后,自己输入信息即可 现在我们去申请办件 ...
- android屏幕适配方法
android中图片适配是一件很棘手的事情,本篇文章详细的介绍屏幕显示的原理,为什么会资源文件中会存在 drawable-ldpi.drawable-mdpi.drawable-hdpi.drawab ...
- Android Handler 异步消息处理机制的妙用 创建强大的图片加载类(转)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 最近创建了一个群,方便大家交流,群号: ...
- ExtJS4.1自带API打不开的问题解决
在ext官网个下载的最新版本的extjs,本来想看看里面的docs文档的,结果却发现打不开,总是转个不停,于是就打开index.html的源码,看到引入ext的js文件的时候,看到引入的是ext.js ...
- js浏览器兼容
//window.event IE:有window.event对象 FF:没有window.event对象.可以通过给函数的参数传递event对象.如onmousemove=doMouseMo ...