ZOJ 3780 Paint the Grid Again
来源:互联网 发布:网络用语及图片 编辑:程序博客网 时间:2024/06/06 01:12
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).
Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color. Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.
Please write a program to find out the way to paint the grid.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the color of the cells should be painted to, after Leo finished his painting.
Output
For each test case, output "No solution" if it is impossible to find a way to paint the grid.
Otherwise, output the solution with minimum number of painting operations. Each operation is either "R#" (paint in a row) or "C#" (paint in a column), "#" is the index (1-based) of the row/column. Use exactly one space to separate each operation.
Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k, the first k - 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have the same type, the one with smaller index of row/column is the lexicographically smaller one.
Sample Input
22XXOX2XOOX
Sample Output
R2 C1 R1No solution
Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest
题目大意:
你有一把魔力刷子,它可以把一个矩阵的任意一行涂成黑色,也可以把任意一列涂成白色。旧颜色会被新颜色覆盖,且每行每列最多涂一次。
现在给你一个n×n的矩阵,矩阵上各元素要么为黑要么为白,要你求如何把一个原先默认为无色的矩阵刷成给定矩阵,要求输出字典序最小的步骤。
大致思路:
刚开始看感觉这道题很复杂,后来仔细一想发现其实挺简单的…
思考一下我们发现,如果某一个元素为O(白色),那么它肯定有一次C[j],且在R[i]的后面。也就是说,如果所在元素那一行有黑色的话,说明它肯定比那一列先涂,这样那个元素才是白色。以此类推,我们就可以发现里面行列的顺序是可以确定的,接下来就是拓扑排序了。
因为要求字典序最小,那我们肯定优先画列的,既然如此我们建边的时候可以把i放大,然后搜的时候从小的开始搜就可以了。
这里还有一个小插曲,刚开始一直觉得深搜是不行的,觉得这样并不能保证找到字典序最小的序列,后来才发现是自己想错了,其实深搜是可以的…还是太弱了呀…
示例代码:(基于BFS)
//Paint the Grid Again.cpp -- zoj 3780#include <iostream>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <queue>#include <map>using namespace std;typedef long long ll;const int maxn = 500 + 10;int n, t, ans;vector<int> G[maxn*2];int indegree[maxn*2];int topo[maxn*2];char maze[maxn][maxn];bool fx[maxn*2];void toposort(){t = 0, ans = 0;queue<int> que;for( int i=1; i<=2*n; i++ )if( !indegree[i] ) que.push(i);while( !que.empty() ){int v = que.front();que.pop();ans++;if( !fx[v] ) topo[t++] = v;for( int i=0; i<G[v].size(); i++ ){int u = G[v][i];indegree[u]--;if( !indegree[u] ) que.push(u);}}if( ans!=2*n )printf("No solution\n");else{for( int i=0; i<t-1; i++ ){if( topo[i]>=1 && topo[i]<=n )printf("C%d ", topo[i]);elseprintf("R%d ", topo[i]-n);}if( topo[t-1]>=1 && topo[t-1]<=n )printf("C%d\n", topo[t-1]);elseprintf("R%d\n", topo[t-1]-n);}}int main(){int T;scanf("%d", &T);while( T-- ){memset(indegree, 0, sizeof(indegree));memset(fx, 0, sizeof(fx));scanf("%d", &n);for( int i=1; i<=2*n; i++ ) G[i].clear();for( int i=1; i<=n; i++ )scanf("%s", maze[i]+1);for( int i=1; i<=n; i++ ){int f1 = 0, f2 = 0;for( int j=1; j<=n; j++ ){if( maze[i][j]=='O' ){G[i+n].push_back(j);indegree[j]++;f1++;}if( maze[j][i]=='X' )// 这里i、j的用法很巧妙嘛…{G[i].push_back(j+n);indegree[j+n]++;f2++;}if( f1==n ) fx[i+n] = 1;// 如果某一行全为白色,那说明黑色不用涂。if( f2==n ) fx[i] = 1;// 同理。// 这种做法的好处是可以保证节点数总为2*n,便于判断是否有解。}}toposort();}return 0;}
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again (模拟)
- zoj 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again(topsort)
- zoj 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780Paint the Grid Again 模拟
- ZOJ 3780Paint the Grid Again
- ZOJ 3780 - Paint the Grid Again
- Paint the Grid Again ZOJ - 3780
- ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
- zoj 3780Paint the Grid Again 拓扑排序
- zoj 3780 Paint the Grid Again (拓扑排序)
- ZOJ 3780 --Paint the Grid Again 逆向自然求解法
- zoj 3780 Paint the Grid Again(拓扑排序)
- ZOJ-3780-Paint the Grid Again【11th浙江省赛】
- ZOJ 3780Paint the Grid Again-贪心模拟/拓扑排序
- ZOJ 3780 Paint the Grid Again (逆推)
- BZOJ 3791: 作业
- LinkedList
- Xcode bug
- JAVA 源代码 【指定文件夹 搜索指定格式的后缀名 拷贝到 指定目录】
- 10+年程序员总结的20+条经验教训
- ZOJ 3780 Paint the Grid Again
- PHP模拟登陆正方系统获取课表、成绩(一看就懂!!!)
- Flickable和Flipable
- Spring MVC 中的基于注解的 Controller
- 欢迎使用CSDN-markdown编辑器
- Linux模拟鼠标和键盘事件的方法
- LeetCode 19 - Remove Nth Node From End of List
- Android开发(45) android代码混淆
- 流程:注册,开发,真机测试,发布,上线。