Paint the Grid Again (隐藏建图+优先队列+拓扑排序)

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

2
2
XX
OX
2
XO
OX

Sample Output

R2 C1 R1
No solution
/*
题意:一个矩阵有两种操作,将每行染成黑色,将每列染成白色每行,每列只能操作一次
    现在给你特定的矩阵,最初的序列是什么颜色也没有的,问你至少操作几次才能形成
    制定的矩阵

初步思路:对于单个元素来说,如果最后的颜色是黑色那么肯定是先进性染白色的操作,
    然后进行的黑色操作,将行列信息建成图,然后用拓扑排序,进行排序并字典序输出

#错误:有一个点,就是最开始的入度为零的点,是不能操作的,因为这些点并没有状态转
    化过来
*/
#include <bits/stdc++.h>
using namespace std;
vector<int>edge[1005];
int inv[1005];//表示每个点的入度
int t;
int n;
char mapn[505][505];
int frist[1005];//表示是不是第一个点
void topu(vector<int> &v){//用于存储操作的顺序
    priority_queue<int,vector<int>,greater<int> >q;
    for(int i=0;i<n*2;i++){//将所有的入度为零的点加入队列
        if(inv[i]==0){
            q.push(i);
            frist[i]=1;
        }
    }
    while(!q.empty()){
        int x=q.top();
        v.push_back(x);
        q.pop();
        for(int i=0;i<edge[x].size();i++){
            int Next=edge[x][i];
            inv[Next]--;//将于这个点相关的边都删掉
            if(inv[Next]==0){//如果入度为零了那么加进队列
                q.push(Next);
            }
        }
    }
    for(int i=0;i<n*2;i++){
        if(inv[i]){
            v.clear();
            break;
        }
    }
}
void init(){
    for(int i=0;i<1005;i++){
        edge[i].clear();
    }
    memset(inv,0,sizeof inv);
    memset(frist,0,sizeof frist);
}
int main(){
    // freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",mapn[i]);
            for(int j=0;j<n;j++){//按照元素信息进行建图
                if(mapn[i][j]=='O'){//如果最后的颜色是黑色的那么肯定是先进行列染白色的,然后进行行染黑
                    edge[i].push_back(n+j);
                    inv[n+j]++;
                }else{//如果最后的颜色是白色,那么肯定是先进行 行染黑色,然后进行列染白色
                    edge[n+j].push_back(i);
                    inv[i]++;
                }
            }
        }
        //建好图了然后进行topu排序
        vector<int>v;
        v.clear();
        topu(v);
        if(v.size()==0){
            puts("No solution");
        }else{
            for(int i=0;i<v.size()-1;i++){
                if(frist[v[i]]) continue;
                if(v[i]>=n){
                    printf("C%d ",v[i]-n+1);
                }else{
                    printf("R%d ",v[i]+1);
                }
            }
            if(v[v.size()-1]>=n){
                printf("C%d\n",v[v.size()-1]-n+1);
            }else{
                printf("R%d\n",v[v.size()-1]+1);
            }
        }
    }
    return 0;
}

 

posted @ 2017-04-10 20:24  勿忘初心0924  阅读(459)  评论(0编辑  收藏  举报