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组成的矩阵,可以选择某一连通块变成另一 ...
随机推荐
- dfs手写栈模板
在竞赛中如果系统栈很小的话,过深的递归会让栈溢出,这个时候我们就要自己手写栈,将递归转化成手工栈. 方法其实也很简单. 基本思路上,我们就是用栈不断的pop,push.但是何时push,何时pop呢? ...
- c++ 日志操作
程序需要一个简单的日志类,为此简单学习了Boost.Log和google的glog,前者功能非常强大,后者非常小巧但是不够灵活,最终打算自己写一个. 环境: win7 32位旗舰版.VS2010旗舰版 ...
- linux command----vi
vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器.Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved),与vi编辑器完全兼容,而且实现了很多增强功能. ...
- asm: Writing Inline Assembly
A usual IA includes these parts: asm [volatile] ( AssemblerTemplate : OutputOperands [ : InputOperan ...
- 动画——animation(2)
日常中,我们使用的动画来源有两个方面—— 第一个,自己去定义. 通过@keyframes去定义即可,格式如下: @keyframe animatename{ 0%{ //这里面写初始的对象的css样式 ...
- ZOJ 2872 Binary Partitions
先写一个完全背包,然后找规律,然后打表. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- Kth order statistcs
Selection: selection is a trivial problem if the input numbers are sorted. If we use a sorting algor ...
- jquery选择器 之 获取父级元素、同级元素、子元素(转)
一.获取父级元素 1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div"><a id="href_fir& ...
- AutoMapper使用简单总结
近期,在用AutoMapper整理一些模型对象映射,顺便小结一下使用的体会.难免有写得不对的地方,谢谢指出! 1. AutoMapper是一个.NET的对象映射工具,可以方便地进行对象间的赋值处理. ...
- php源码分析之base64_encode函数
base64_encode编码规律分析 字符串长度除以3向上取整乘以4等于编码后的字符串长度 ceil(strlen($string)/3)*4 = strlen(base64_encode($str ...