ZOJ 3780 Paint the Grid Again(逆推)
来源:互联网 发布:微星网络唤醒bios设置 编辑:程序博客网 时间:2024/05/29 21:33
题目地址:点击打开链接
题意:
给一块n*n的格子,每次可以将任意行变成X,或任意列变成O,后操作的将覆盖原先的操作,每行每列只能操作一次。给出最终图形,要求按先后顺序输出操作方法。
思路:
某一个状态,他要么只有行同为X,要么只有列同为O,一个状态不可能同时某行都为X,某列都回O,因为一定有交点。所以我们倒着来,因为要字典序,
所以我们要逆字典序找,找到行同为X或列同为O,就把这行或列标记掉。
可以dfs也可以循环写
dfs代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 1e3+5;char pic[maxn][maxn];bool r[maxn], c[maxn], ok;int n, cnt, rNum, cNum;struct node{ char ch; int val; node() {} node(char cc, int vv): ch(cc), val(vv) {}}ans[maxn];void dfs(){ if(cNum == n || rNum == n) { ok = 1; for(int i = cnt-1; i >= 1; i--) printf("%c%d%c", ans[i].ch, ans[i].val, i==1 ? '\n' : ' '); return ; } if(ok) return; int flag; for(int i = n; i >= 1; i--) { if(c[i]) continue; flag = 1; for(int j = 1; j <= n; j++) { if(pic[j][i] != 'O') { flag = 0; break; } } if(flag) { c[i] = 1; cNum++; ans[cnt++] = node('C', i); for(int j = 1; j <= n; j++) pic[j][i] = 'X'; dfs(); cNum--; c[i] = 0; cnt--; for(int j = 1; j <= n; j++) pic[j][i] = 'O'; } } for(int i = n; i >= 1; i--) { if(r[i] == 1) continue; flag = 1; for(int j = 1; j <= n; j++) { if(pic[i][j] != 'X') { flag = 0; break; } } if(flag) { r[i] = 1; rNum++; ans[cnt++] = node('R', i); for(int j = 1; j <= n; j++) pic[i][j] = 'O'; dfs(); r[i] = 0; rNum--; cnt--; for(int j = 1; j <= n; j++) pic[i][j] = 'X'; } }}int main(void){ int t; cin >> t; while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf(" %c", &pic[i][j]); cnt = 1; memset(r, 0, sizeof(r)); memset(c, 0, sizeof(c)); ok = rNum = cNum = 0; dfs(); if(!ok) puts("No solution"); } return 0;}
循环 :
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 1005;bool r[maxn], c[maxn];char pic[maxn][maxn];int n;struct node{ char ch; int d; node(){} node(char cc, int dd): ch(cc), d(dd) {}}ans[maxn];bool judge(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(pic[i][j] != '.') return 1; return 0;}int main(void){ int t; cin >> t; while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf(" %c", &pic[i][j]); int index = 1; bool ok = 1; memset(c, 0, sizeof(c)); memset(r, 0, sizeof(r)); while(judge()) { if(!ok) break; for(int i = n; i >= 1; i--) { if(c[i]) continue; ok = 1; for(int j = 1; j <= n; j++) { if(r[j]) continue; if(pic[j][i] != 'O') { ok = 0; break; } } if(ok) { c[i] = 1; for(int j = 1; j <= n; j++) pic[j][i] = '.'; ans[index++] = node('C', i); } } if(ok) continue; for(int i = n; i >= 1; i--) { if(r[i]) continue; ok = 1; for(int j = 1; j <= n; j++) { if(c[j]) continue; if(pic[i][j] != 'X') { ok = 0; break; } } if(ok) { r[i] = 1; for(int j = 1; j <= n; j++) pic[i][j] = '.'; ans[index++] = node('R', i); break; //这个不能少,这行刷去后可能导致某列可刷,但不break,会导致ok变0输出No } } } if(ok) for(int i = index-1; i >= 1; i--) printf("%c%d%c", ans[i].ch, ans[i].d, i==1 ? '\n' : ' '); else puts("No solution"); } return 0;}
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
- 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
- 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浙江省赛】
- JavaScript之初探this
- 关于docker容器的监控
- python实时输出
- DS1302芯片在am335xD上的移植
- PHP PDO 使用详解
- ZOJ 3780 Paint the Grid Again(逆推)
- JavaEE JavaSE JavaME的区别
- python画图
- 迷茫<第一篇:初到北京>
- Andrnoid 使用Glide时ImageView不显示的问题
- queue vector <int>倒序输出
- int,long,long long的范围及使用
- Java 远程调用有道api接口实例
- IntelliJ IDEA JDK tomcat配置