Paint the Grid Again ZOJ - 3780

来源:互联网 发布:c语言进制转换函数 编辑:程序博客网 时间:2024/06/07 05:46

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 Tindicating 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

题意:给你一个n*n的棋盘,你可以每次把一行全部涂成x,然后把一列涂成o。然后给你一个最终结果,让你输出最少的操作,并且按照字典序来的。


思路:这里我们给行和列进行标号,为了便于之后的输出我们把行号加n。

之后就是想一想,其实每个点都代表着一种操作,如果当前点是X那么,必定是先刷行后刷列的,而如果当前点是O,那么自然是先刷列后刷行的。

那么我们就可以根据这个行列关系建造一个拓扑图。

建完之后直接跑一次拓扑排序就可以了。

不过这里要注意的一个地方就是,它让你输出最小操作数,那也就是说有些操作是没变要的。

而这些操作其实就是一行全为O,因为一行全为O的话,那么你这一行的行涂X操作就完全没有必要存在了,所以在最后输出的时候要判断当前行或列有没有必要存在。

在建立拓扑图的时候就直接扫一次看看当前行或者列是不是不需要存在的,标记一下即可。

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#include <queue>using namespace std;int n,m;const int MAXN=500+10;char tu[MAXN][MAXN];int in[MAXN*2];bool vis[MAXN*2];int ans[MAXN*2];vector<int>G[MAXN*2];void init(){    for(int i=0;i<n*2;++i)    {        in[i]=0;        vis[i]=1;        G[i].clear();    }}void add(int a,int b){    in[b]++;    G[a].push_back(b);}void top_sort(int n){    int i;    priority_queue<int,vector<int>,greater<int> >q;    for(i=0;i<n;++i)if(!in[i])q.push(i);    int cnt=0;    while(!q.empty())    {        int u=q.top();        q.pop();        ans[cnt++]=u;        int l=G[u].size();        for(i=0;i<l;++i)        {            int v=G[u][i];            in[v]--;            if(!in[v])q.push(v);        }    }    if(cnt<n)printf("No solution");    else    {        n/=2;        int t=0;        for(i=0;i<cnt;++i)        {            if(!vis[ans[i]])continue;//当前行列操作没有必要存在            t++;            if(t>1)printf(" ");            if(ans[i]>=n)printf("R%d",ans[i]-n+1);            else printf("C%d",ans[i]+1);        }    }}int main(){    int i,j;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        init();        for(i=0;i<n;++i)        {            scanf("%s",tu[i]);        }        for(i=0;i<n;++i)        {            int cnt1=0,cnt2=0;            for(j=0;j<n;++j)            {                if(tu[i][j]=='O')//枚举一行(先行后列)                {                    cnt1++;                    add(i+n,j);                }                if(tu[j][i]=='X')//枚举一列(先列后行)                {                    cnt2++;                    add(i,j+n);                }            }            if(cnt1==n)vis[i+n]=0;//一行全为O            if(cnt2==n)vis[i]=0;//一列全为X        }        top_sort(n*2);        puts("");    }    return 0;}




1 0
原创粉丝点击