拓扑排序。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的更多相关文章

  1. 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 ...

  2. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  3. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. zoj p3780 Paint the Grid Again

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5267 题意:Leo 有一个N*N 的格子,他又有一把魔法刷,这个刷子能把 ...

  8. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  9. ZOJ 3781 Paint the Grid Reloaded 连通块

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...

随机推荐

  1. Webdriver+testNG+ReportNG+Maven+SVN+Jenkins自动化测试框架的pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  2. iOS屏幕旋转 浅析

    一.两种orientation 了解屏幕旋转首先需要区分两种orientation 1.device orientation 设备的物理方向,由类型UIDeviceOrientation表示,当前设备 ...

  3. nand驱动移植

    首先下载nand flash驱动 s3c_nand.c ,此文件包含着nand flash驱动具体的实现,将其复制到drivers/mtd/nand下: s3c_nand.c 下载地址 s3c_nan ...

  4. loadrunner基本概念、安装及术语(一)

    一.初识loadrunner: LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架 ...

  5. sql server 2005导出数据

    */ EXEC sp_configure 'show advanced options', 1 GO */     配置选项 'show advanced options' 已从 0 更改为 1.请运 ...

  6. HDU2579--Dating with girls(2)--(DFS, 判重)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. acm课程练习2--1005

    题目描述 Mr. West bought a new car! So he is travelling around the city.One day he comes to a vertical c ...

  8. java面向对象_接口

    java接口 interface,是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是类,编写接口的方式和类很相似,但 ...

  9. 转:Selenium中的几种等待方式,需特别注意implicitlyWait的用法

    最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法. ...

  10. PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)

    简单dfs #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...