ZOJ 3780 Paint the Grid Again(拓扑排序)

来源:互联网 发布:淘宝7天无理由退换规则 编辑:程序博客网 时间:2024/05/16 15:37

ZOJ 3780 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

题意:一次涂色能把一行涂成X,或把一列涂成O,在用最少次数涂色的前提下,输出字典序最小的涂色方案(列涂色比行涂色小,涂色下标小的较小)。

这题和那道用拓扑排序做的框架堆叠很像,但我当时没认真想那道题…造成了面对这道题的无力…这个故事告诉我们不要放过到手边的题,不然会后悔。

突破点在于:根据当前的涂色状态,是可以反推上一个涂色状态的。对于在i行、j列,当前状态为X的格子,我们可以想到,对于i行的涂色必定发生在对于j列的涂色之后。

也就是说,可以推出若干组涂色的两两相对顺序。

因此,将各种涂色情况设为点,从Cj向Ri引有向边(先发生的涂色->后发生的涂色),对于这个有向图求出的拓扑序列就可以满足最终达到输入涂色状态的要求。

并且,考虑到输出的是字典序最小方案,将列涂色设为前N个点,行涂色设为后N个点,用输出最小值的优先队列来存储每次的零入度点。

可以想到,类似的可以用拓扑排序解决的问题都隐含着某种状态具有先后关系的条件。

#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include <functional>#define maxn 550using namespace std;int head[2*maxn];typedef struct{    int to,next;}e;e edge[maxn*maxn];//1-N为C操作,N-2N为R操作int N,cnt,iq;char maze[maxn][maxn];int queues[2*maxn];int degree[2*maxn];bool vis[2*maxn];void init(){    cnt=0;    iq=0;    memset(head,-1,sizeof(head));    memset(degree,0,sizeof(degree));    memset(vis,false,sizeof(vis));}int addedge(int x,int y){    edge[cnt].to=y;    edge[cnt].next=head[x];    head[x]=cnt;    cnt++;    return 0;}int solve(){    int i,k,s;    priority_queue<int, vector<int>, greater<int> > P;    for(i=1;i<=2*N;i++){        if(!degree[i]){            P.push(i);        }    }    while(!P.empty()){        s=P.top();        P.pop();        queues[iq++]=s;        for(k=head[s];k!=-1;k=edge[k].next){            if(!--degree[edge[k].to]){                P.push(edge[k].to);            }        }    }    return 0;}int main(){    int T,i,j,k;    scanf("%d",&T);    while(T--){        init();        scanf("%d",&N);        for(i=1;i<=N;i++){            scanf("%s",maze[i]+1);        }        for(i=1;i<=N;i++){            for(j=1;j<=N;j++){                if(maze[i][j]=='X'){                    addedge(j,i+N);                    vis[i+N]=true;                    degree[i+N]++;                }                if(maze[i][j]=='O'){                    addedge(i+N,j);                    vis[j]=true;                    degree[j]++;                }            }        }        solve();        if(iq!=N*2){            printf("No solution\n");            continue;        }        for(i=0;i<iq;i++){            if(!vis[queues[i]])                continue;            if(queues[i]<=N){                printf("C");                printf("%d",queues[i]);            }            else{                printf("R");                printf("%d",queues[i]-N);            }            if(i<iq-1)                printf(" ");            else                printf("\n");        }    }    return 0;}

0 0