ZOJ 3780 - Paint the Grid Again

本文深入探讨了如何解决特定编程挑战,通过实例演示了利用有限资源完成目标的方法,包括优化算法步骤和确保操作顺序的策略,以实现高效且有序的编程流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E - Paint the Grid Again
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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的空矩阵,最终要刷成输入的样子。有两种操作:1、把其中一行扫成X。2、把其中一列扫成O。注意的地方:1、后一次扫的会覆盖前一次扫的。2、每一列和每一行最多只能扫一次,也就是说总操作数不会超过2N。

最终输出操作步骤,先保证步数最少,再保证字典序最小。如果不可能,输出No solution。

倒推,从结果出发,清除某一行或某一列的必要条件是这一行仅有X或这一列仅有O,为空的格子可以忽略,倒过来则为从字典序大的开始操作。

题目中限制每行或每列只能刷一次,也就是说如果存在解,最优解步数一定等于(存在X的行数+存在O的列数)。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <algorithm>
#include <queue>
using namespace std;

#define N 505
#define INF 0x3f3f3f3f
#define LL long long

struct node{
    int op,num; //op=2 row    ; op=1 col;

    node(){}
    node(int o,int n):op(o),num(n){}

    bool operator<(const node &a)const{
        return op==a.op?num<a.num:op<a.op;
    }
};

int row[N],col[N];
int crow[N],ccol[N];
char c;
priority_queue<node> q;
int cz[N<<1],id[N<<1];
int ans,n;

void pr(int a,int b){
    if (a==1)
        printf("C");
    else
        printf("R");
    printf("%d",b);
}

int main(){
    int T;
    scanf("%d",&T);
    while (T--){
        while (!q.empty())
            q.pop();
        ans=0;
        memset(row,0,sizeof(row));
        memset(col,0,sizeof(col));
        memset(crow,0,sizeof(row));
        memset(ccol,0,sizeof(col));
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
            getchar();
            for (int j=1;j<=n;j++){
                c=getchar();
                if (c=='X'){
                    crow[i]++;
                    col[j]++;
                }
                if (c=='O'){
                    ccol[j]++;
                    row[i]++;
                }
            }
        }

        for (int i=1;i<=n;i++){
            if (crow[i]>0)
                ans++;
            if (ccol[i]>0)
                ans++;
            if (crow[i]==n)
                q.push(node(2,i));
            if (ccol[i]==n)
                q.push(node(1,i));
        }
        //cout<<ans<<endl;

        int cnt=0,temp=0;
        while (!q.empty()){
            temp++;
            node cur=q.top();
            q.pop();
            int op=cur.op;
            int num=cur.num;
            cz[cnt]=op;
            id[cnt++]=num;

            if (op==1){
                for (int i=1;i<=n;i++){
                    row[i]--;
                    if (row[i]==0)
                        q.push(node(2,i));
                }
            }else{
                for (int i=1;i<=n;i++){
                    col[i]--;
                    if (col[i]==0)
                        q.push(node(1,i));
                }
            }
        }

        if (temp<ans)
            printf("No solution\n");
        else{
            pr(cz[ans-1],id[ans-1]);
            for (int i=ans-2;i>=0;i--){
                printf(" ");
                pr(cz[i],id[i]);
            }
            printf("\n");
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值