ZOJ

来源:互联网 发布:无心整合包捏脸数据 编辑:程序博客网 时间:2024/04/30 23:27

Paint the Grid Again

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


因为最后一步肯定是涂一整行或一整列,所以最后只可能要么存在整行,要么存在整列,从后向前还原搜索,最主要的就是想到这种逆推

#include<bits/stdc++.h>#define MEM(a) memset(a,0,sizeof(a))#define bs 1000000007#define eps 1e-8#define lson l,m,node<<1#define rson m+1,r,node<<1|1typedef long long ll;using namespace std;int n;int book[2][505];int ans[2005][3];int num[2][505],flog,now;//flog=0 hang  flog=1 liechar ch[505][505];int dfs(){//printf("%d\n",flog);int cnt=0,i,j;if(flog==-1||flog==2)return 0;for(i=n;i>=1;i--){if(num[flog][i]>=n){cnt++;//printf("haha%d %d %d\n",i,cnt,num[flog][1]);if(!book[flog][i]){ans[now][0]=flog;ans[now][1]=i;now++;for(j=1;j<=n;j++){num[!flog][j]++;}book[flog][i]=1;}}}if(cnt==n){flog=2;return 0;}if(cnt==0){flog=-1;return 0;}flog=!flog;dfs();return 0;}int main(){int t,i,j;scanf("%d",&t);while(t--){now=0;flog=-1;MEM(book);MEM(num);scanf("%d",&n);for(i=1;i<=n;i++)scanf("%s",ch[i]+1);for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(ch[i][j]=='X')num[0][i]++;elsenum[1][j]++;}}for(i=1;i<=n;i++)if(num[0][i]==n)flog=0;else if(num[1][i]==n)flog=1;if(flog==-1){printf("No solution\n");}else{dfs();if(flog==-1){printf("No solution\n");}else{for(i=now-1;i>=0;i--){if(ans[i][0]==0){printf("R");}else{printf("C");}printf("%d",ans[i][1]);if(i==0)printf("\n");elseprintf(" ");}}}}}


0 0
原创粉丝点击