zoj 3780 Paint the Grid Again(拓扑排序)
来源:互联网 发布:大数据就业培训 编辑:程序博客网 时间:2024/06/05 15:15
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 thanY 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
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5267
#include<iostream>#include<algorithm>#include<string>#include<map>#include<queue>#include<vector>#include<cmath>#include<string.h>#include<stdlib.h>#include<cstdio>#define ll long longusing namespace std;int stack[1005];int yes[1005];char x[501][501];int h[1005];int n;vector<int> ans;int top;int cmp(int x,int y){return x>y;}void work(){queue<int> q;sort(stack,stack+top+1,cmp);for(int i=0;i<=top;++i){q.push(stack[i]);ans.push_back(stack[i]);yes[stack[i]]=-1;}while(!q.empty()){top=-1;int fr=q.front();q.pop();if(fr<n){for(int i=0;i<n;++i){h[i+n]++;if(yes[i+n]!=-1&&h[i+n]==n)stack[++top]=i+n;}}else{for(int i=0;i<n;++i){h[i]++;if(yes[i]!=-1&&h[i]==n)stack[++top]=i;}}sort(stack,stack+top+1,cmp); //每次排序的原因是题目要求按序输出for(int i=0;i<=top;++i){q.push(stack[i]);ans.push_back(stack[i]);yes[stack[i]]=-1;}}for(int i=0;i<2*n;++i){if(yes[i]==0){cout<<"No solution"<<endl;return;}}for(int i=ans.size()-1;i>0;--i){int u=ans[i];if(u<n)cout<<"C"<<u+1<<" ";elsecout<<"R"<<u-n+1<<" ";} int u=ans[0];if(u<n)cout<<"C"<<u+1;elsecout<<"R"<<u-n+1;cout<<endl;}int main(){int t;cin>>t;while(t--){top=-1;ans.clear();memset(x,0,sizeof(x));memset(stack,0,sizeof(stack));memset(h,0,sizeof(h));memset(yes,0,sizeof(yes));cin>>n;for(int i=0;i<n;++i){for(int j=0;j<n;++j){cin>>x[i][j];if(x[i][j]=='X')h[i+n]++;}if(h[i+n]==n)stack[++top]=i+n;else if(h[i+n]==0)yes[i+n]=-1;}for(int j=0;j<n;++j){for(int i=0;i<n;++i){if(x[i][j]=='O')h[j]++;}if(h[j]==n)stack[++top]=j;else if(h[j]==0)yes[j]=-1;}if(top==-1)cout<<"No solution"<<endl;elsework();}return 0;}
- zoj 3780 Paint the Grid Again(拓扑排序)
- 拓扑排序(ZOJ 3780,Paint the Grid Again)
- ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
- zoj 3780Paint the Grid Again 拓扑排序
- zoj 3780 Paint the Grid Again (拓扑排序)
- ZOJ 3780Paint the Grid Again-贪心模拟/拓扑排序
- ZOJ 3780 Paint the Grid Again(拓扑排序)
- zoj 3780 Paint the Grid Again 11届浙江省赛 (拓扑排序)
- zoj 3870 Paint 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
- 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
- BlockingQueue详解
- Html5系列(二十五)画布(图像)
- ios常用第三方框架
- 复杂的代码布局
- 引用的用法
- zoj 3780 Paint the Grid Again(拓扑排序)
- GPS位置服务
- codeforces535A:Tavas and Nafas
- int和NSInteger区别
- 电猴网,一家专注于电商人才的求职招聘平台
- eclipse建工程时,出现“在项目中具有不受限制的类路径容器:JRE系统库”错误
- 基于Lockset的数据竞争检测方法汇总(一)
- 看“QQ音乐 闪屏设计”有感
- 成都崇州首例销售地下网络私彩嫌疑人被批捕