【ZOJ 3780】Paint the Grid Again —— 模拟拓扑排序

2 篇文章 0 订阅
1 篇文章 0 订阅

原题链接


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
2
2
XX
OX
2
XO
OX
Sample Output
R2 C1 R1
No solution



解题报告:

这道题主要是要理清题目的意思。最后染色的一行或一列肯定是同色的,所以找到那样的行或者列,而且要是同时存在多个的行或者列,说明它们可以部分先后,因而可以做拓扑排序,但题目要我们字典序,所以,遍历的时候要从后往前遍历(因为我们解题时是反模拟刷墙的顺序的),即 for(int i=n-1;i>=0;--i) 。




#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

const int MAX=501;
int n;
char ch[MAX][MAX];
bool row_flag[MAX];
bool col_flag[MAX];
char RC[2*MAX];
int  ID[2*MAX];

bool IsOK(){
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            if(ch[i][j]!='-') return false;
    return true;
}

bool work(int cnt){
    // row
    for(int i=n-1;i>=0;--i)
    {
        if(row_flag[i]) continue;
        int j,num=0;
        for(j=0;j<n;++j)
        {
            if(ch[i][j]=='O') break;
            //else if(ch[i][j]=='-') ++num;
        }

        //if(num==n){row_flag[i]=true;}

        if(j==n){
            RC[cnt]='R';
            ID[cnt]=i+1;
            row_flag[i]=true;
            for(int k=0;k<n;++k)
                ch[i][k]='-';
            return true;
        }
    }


    //column
    for(int i=n-1;i>=0;--i)
    {
        if(col_flag[i]) continue;
        int j,num=0;
        for(j=0;j<n;++j)
        {
            if(ch[j][i]=='X') break;
            //else if(ch[i][j]=='-') ++num;
        }

        //if(num==n){row_flag[i]=true;}

        if(j==n){
            RC[cnt]='C';
            ID[cnt]=i+1;
            col_flag[i]=true;
            for(int k=0;k<n;++k)
                ch[k][i]='-';
            return true;
        }
    }
    return false;
}

int main()
{
    int T,cnt;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;++i)
            scanf("%s",ch[i]);

        memset(row_flag,0,sizeof(row_flag));
        memset(col_flag,0,sizeof(col_flag));
        cnt=0; bool flag=true;
        while( !IsOK() ){
            if( !work(cnt++) ) {flag=false;break;}
        }

        if(!flag) printf("No solution\n");
        else{
            for(int i=cnt-1;i>=0;--i) //???
                if(i==0) printf("%c%d",RC[i],ID[i]);
                else     printf("%c%d ",RC[i],ID[i]);
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值