ZOJ 3780 - Paint the Grid Again - [Simulation] [The 11th Zhejiang Provincial Competition E Question]

Topic link: 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

 

Title:

Given an N*N square matrix, it is stipulated that only one row or one column can be painted at a time, the row can only be painted with X (black), and the column can only be painted with O (white);

Now give the final color of each square, and ask for at least a few smears to achieve it.

(If there are multiple schemes, the output lexicographical order is the smallest, and it is stipulated that the first column C is smaller than the row R. In the case of both R or the same C, the smaller the index, the better the priority)

 

answer:

Simulate the smearing process and push back each smear in reverse.

The last smear will inevitably result in a row with all Xs or a column with all Os. Find out, clear the color of this row/column, and repeat.

 

AC code:

#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;
}years[ 505 * 505 ];

intmain ()
{
    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");
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726369&siteId=291194637
Recommended