2017省组队训练 zoj3780 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 Tindicating 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
题目大意:

有一个空白矩阵,然后画出最终的状态,问你画的步骤(步骤输出字典序最小的‘R’>'C').刷的规则为:要么刷一行,要么刷一列,刷一行时,这一行全是黑色(‘X’),刷一列的时候这一列全是白色(‘O’).新的颜色会覆盖旧的颜色。如果不能刷出所给的这种状态,则输出No solution.

题解:

因为一刷刷一行或一列,那么最后一刷要么是一行要么是一列。而且题目说了每行每列最多刷一次。那么就倒着往前推就行了。先进行行在进行列(字典序最小)。找出一行没有‘O’的就表示走到这一步了。同理,找出一列没有‘X’。那么当找出这样的行和列的时候就把他变为空白或者变为相反的颜色,但是变为相反的颜色会有缺点。因每一点最多被刷两次,如果变为相反的颜色容易混论而且还不知道该点被刷了几次。所以不如直接变成空白。就这样直到全是空白为止。或者没有符合要求的行列为止。


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;

struct node
{
    char ch;
    int id;
}q[255555];
char s[555][555];
int visc[555],visr[555];

bool work(int n){
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(s[i][j]!='.')return true;
        }
    }
    return false;
}
int main()
{
    int t,top,n;
    scanf("%d",&t);
    while(t--)
    {
        memset(visc,0,sizeof(visc));
        memset(visr,0,sizeof(visr));
        top=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
        }
        int flag=1;
        int sum,num;
        sum=num=1;
        while(work(n))
        {
            if(sum==0&&num==0)
            {
                flag=0;
                break;
            }
            num=0;
            for(int i=n-1;i>=0;i--)
            {
                if(visr[i])continue;
                int ok=1;
                for(int j=0;j<n;j++)
                {
                    if(s[i][j]=='O')
                    {
                        ok=0;
                        break;
                    }
                }
                if(ok)
                {
                    num++;
                    for(int j=0;j<n;j++)
                    {
                        s[i][j]='.';
                    }
                    q[top].ch='R';
                    q[top++].id=i+1;
                    visr[i]=1;
                }
            }
            if(!work(n))break;
            sum=0;
            for(int j=n-1;j>=0;j--)
            {
                if(visc[j])continue;
                int ok=1;
                for(int i=0;i<n;i++)
                {
                    if(s[i][j]=='X')
                    {
                        ok=0;
                        break;
                    }
                }
                if(ok)
                {
                    sum++;
                    for(int i=0;i<n;i++)
                    {
                        s[i][j]='.';
                    }
                    q[top].ch='C';
                    q[top++].id=j+1;
                    visc[j]=1;
                }
            }
        }
        if(!flag)printf("No solution\n");
        else
        {
            for(int i=top-1;i>=0;i--)
            {
                if(i==0)
                    printf("%c%d\n",q[i].ch,q[i].id);
                else
                    printf("%c%d ",q[i].ch,q[i].id);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值