Paint the Grid Again----模拟||拓扑

来源:互联网 发布:重庆 深圳 知乎 编辑:程序博客网 时间:2024/06/03 20:44
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 R1

No solution

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780

题意一开始就看的我很迷啊,就是说有一个魔法刷,可以把每一行刷成黑色,或者每一行刷成白色,每一次会覆盖前面的颜

色,现给你目标矩阵,求一个空矩阵能否变换过去,如果能,就按字典序输出。

宇航菊苣用拓扑排序过了,我这种渣渣只能模拟,一层一层的来。不懂得可以顺着代码走一遍。

代码:

#include <cstdio>#include <iostream>#include <cstring>#include <vector>using namespace std;char xin[510][510];int main(){    int t;    scanf("%d",&t);    while(t--){        int n;        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%s",xin[i]);        }        int num=0;        vector<int >map1;        while(num<n*n){            int base=-1,k=-1;            bool flag=false;            for(int hang=n-1;hang>=0;hang--){                int i;                flag=false;                for(i=n-1;i>=0;i--){                    if(xin[hang][i]=='O'){                        break;                    }                    else if(xin[hang][i]=='X'){                        flag=true;                    }                }                if(i<0&&flag){                    base=0;                    k=hang;                    break;                }            }            if(base==0&&k>=0){                map1.push_back(k+1);                for(int i=0;i<n;i++){                    if(xin[k][i]=='X'){                        xin[k][i]='.';                        num++;                    }                }                continue;            }            else{                for(int lie=n-1;lie>=0;lie--){                    flag=false;                    int i;                    for(i=n-1;i>=0;i--){                        if(xin[i][lie]=='X'){                            break;                        }                        else if(xin[i][lie]=='O'){                            flag=true;                        }                    }                    if(i<0&&flag){                        k=lie;                        base=1000;                        break;                    }                }                if(base==1000&&k>=0){                    map1.push_back(base+k+1);                    for(int i=0;i<n;i++){                        if(xin[i][k]=='O'){                            xin[i][k]='.';                            num++;                        }                    }                    continue;                }                else{                    break;                }            }        }        if(num<n*n){            printf("No solution\n");        }        else{            for(int i=map1.size()-1;i>=0;i--){                if(map1[i]>=1000){                    printf(i!=0?"C%d ":"C%d",map1[i]-1000);                }                else{                    printf(i!=0?"R%d ":"R%d",map1[i]);                }            }            cout<<endl;        }    }    return 0;}


0 0
原创粉丝点击