ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780

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

题意:

给出N*N的方格阵,规定一次只能涂抹一行或者一列,行只能涂X(黑色),列只能涂O(白色);

现给出最终每个方格的颜色,求最少几次涂抹可以达成。

(若有多种方案,输出字典序最小的,规定首先列C小于行R,在同为R或者同为C的情况下index越小越优先)

题解:

模拟涂抹过程,反着把每一次涂抹倒推回去。

最后一次涂抹必然导致一行全为X或者一列全为O,找出来,把这一行/列的颜色给清清除,反复如此即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;

int n;
int cell[505][505];
bool rvis[505],cvis[505];
int cntr,cntc;

struct ANS{
    char pos;
    int idx;
}ans[505*505];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        char tmp[505];
        for(int i=1;i<=n;i++)
        {
            scanf("%s",tmp+1);
            for(int j=1;j<=n;j++)
            {
                if(tmp[j]=='X') cell[i][j]=1;
                else cell[i][j]=0;
            }
        }


        cntr=cntc=n;
        memset(rvis,0,sizeof(rvis));
        memset(cvis,0,sizeof(cvis));
        int cnt=0;
        bool haveSOL=0;
        while(1)
        {
            int rowOK=0;
            for(int r=n;r>=1;r--)
            {
                if(rvis[r]) continue;

                bool ok=1;
                for(int j=1;j<=n;j++)
                {
                    if(cvis[j] || cell[r][j]==1) continue;
                    ok=0; break;
                }

                if(ok)
                {
                    //printf("R%d\n",r);
                    rvis[r]=1;
                    cntr--;
                    ans[cnt++]=(ANS){'R',r};
                    rowOK=1;
                    break;
                }
            }

            int colOK=0;
            if(!rowOK)
            {
                for(int c=n;c>=1;c--)
                {
                    if(cvis[c]) continue;

                    bool ok=1;
                    for(int i=1;i<=n;i++)
                    {
                        if(rvis[i] || cell[i][c]==0) continue;
                        ok=0; break;
                    }

                    if(ok)
                    {
                        //printf("C%d\n",c);
                        cvis[c]=1;
                        cntc--;
                        ans[cnt++]=(ANS){'C',c};
                        colOK=1;
                        break;
                    }
                }
            }

            if(cntr==0 || cntc==0)
            {
                haveSOL=1;
                break;
            }
            if(colOK+rowOK==0)
            {
                printf("No solution\n");
                break;
            }
        }

        if(haveSOL)
        {
            for(int i=cnt-1;i>=0;i--)
            {
                if(i!=cnt-1) printf(" ");
                printf("%c%d",ans[i].pos,ans[i].idx);
            }
            printf("\n");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/dilthey/p/8919550.html