现在的位置: 首页 > 综合 > 正文

zoj 3780 拓扑排序

2018年02月21日 ⁄ 综合 ⁄ 共 3352字 ⁄ 字号 评论关闭

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

4.15训练赛的题。
看到题的时候一点思路没有。于是就放过了。后来想到了某年regional的一个类似的题。好像使用拓扑排序搞得。于是乎没看题就想啊想怎么建图。。晚上再看一次题,发现少看了个条件。一列一列的只能涂白色,一行一行的只能涂黑色。。
还有一点,输出字典序最小的,先涂列后涂行。
扫描图的时候从大到小扫描,可以保证编号小的行或列先涂。(邻接表存储);
如果刚开始的时候一个点的入度等于0,那么涂的过程中对于此行或者此列没有涉及,故可以略去。
#include<cstring>
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
using namespace std;
#define re freopen("in.txt","r",stdin)
#define we freopen("out.txt","w",stdout)
#define maxn 511*2
#define maxm maxn*maxn/2
#define ll long long
#define ld long double
struct pp
{
    int u,v,next;
}edge[maxm];
int head[maxn];
int cnt;
int ind[maxn];
void add(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    ind[v]++;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
vector<int> ans(maxn);
int aa[maxn];
int toposort(int n)
{
    queue<int> q;
    ans.clear();
    while(!q.empty())q.pop();
    for(int i=1;i<=n;i++){
        if(ind[i]==0){
            q.push(i);
            aa[i]=1;
        }
    }
    while(!q.empty()){
        int tmp=q.front();
        q.pop();
        ans.push_back(tmp);
        for(int k=head[tmp];~k;k=edge[k].next){
            int v=edge[k].v;
            if(--ind[v]==0)q.push(v);
        }
    }
    for(int i=1;i<=n;i++)
        if(ind[i])return 0;
    return 1;
}
int ma[maxn][maxn];
int main()
{
    int nc;
   // re;
    cin>>nc;
    while(nc--){
        int n;
        cin>>n;
        string s;
        memset(head,-1,sizeof(head));
        cnt=0;
        memset(ind,0,sizeof(ind));
        memset(ma,0,sizeof(ma));
        memset(aa,0,sizeof(aa));
        for(int i=1;i<=n;i++){
            cin>>s;
            for(int j=1;j<=n;j++)
                if(s[j-1]=='O')ma[i][j]=1;//white
                else ma[i][j]=2;//black
        }
        for(int i=n;i>=1;i--){
            for(int j=n;j>=1;j--){
                if(ma[j][i]==1)continue;
                else add(i,j+n);
            }
        }
        for(int i=n;i>=1;i--)
        for(int j=n;j>=1;j--){
            if(ma[i][j]==2)continue;
            else add(i+n,j);
        }
        if(!toposort(2*n)||ans.size()==0)puts("No solution");
        else{
            int len=ans.size();
            for(int i=0;i<len-1;i++){
                if(aa[ans[i]])continue;
                printf("%c%d ",ans[i]>n?'R':'C',ans[i]>n?ans[i]-n:ans[i]);
            }
            //puts("");
            printf("%c%d\n",ans[len-1]>n?'R':'C',ans[len-1]>n?ans[len-1]-n:ans[len-1]);

        }
    }
    return 0;
}

抱歉!评论已关闭.