我是靠谱客的博主 柔弱棒球,最近开发中收集的这篇文章主要介绍ZOJ3780-Paint the Grid Again,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

Author:  YU, Xiaoyao
Source:  The 11th Zhejiang Provincial Collegiate Programming Contest


题意:在一个n*n的矩阵中,一开始什么也没有填,可一次将一行全写成X,或者将一列全写成O。其中每一行每一列只可以写一次,给出一个目标状态,问最少多少步以后可以得到目标状态?如果有多个结果,按最少字典序输出,其中列优先,标号小的优先
解题思路:逆向自然求解:找到全’X'行 或 全'O'列 删除,从后往前找时应优先处理编号大的行/列


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
struct node
{
char dir;
int id;
} ans[1010];
int r[510],c[510],n;
char mp[510][510];
bool check()
{
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
if(mp[i][j]!='.') return 1;
return 0;
}
int main()
{
int t,flag,cnt;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%s",mp[i]);
memset(c,0,sizeof c);
memset(r,0,sizeof r);
cnt=0;
flag=1;
while(check())
{
if(flag==0) break;
for(int i=n-1; i>=0; i--)
{
if(r[i]) continue;
flag=1;
for(int j=0; j<n; j++)
{
if(c[j]) continue;
if(mp[i][j]!='X')
{
flag=0;
break;
}
}
if(flag)
{
for(int j=0; j<n; j++)
mp[i][j]='.';
r[i]=1;
ans[cnt].dir='R';
ans[cnt].id=i+1;
cnt++;
break;
}
}
if(flag) continue;
for(int i=n-1; i>=0; i--)
{
if(c[i]) continue;
flag=1;
for(int j=0; j<n; j++)
{
if(r[j]) continue;
if(mp[j][i]!='O')
{
flag=0;
break;
}
}
if(flag)
{
for(int j=0; j<n; j++)
mp[j][i]='.';
c[i]=1;
ans[cnt].dir='C';
ans[cnt].id=i+1;
cnt++;
break;
}
}
}
if(flag)
{
for(int i=cnt-1; i>0; i--)
printf("%c%d ",ans[i].dir,ans[i].id);
printf("%c%dn",ans[0].dir,ans[0].id);
}
else printf("No solutionn");
}
return 0;
}

最后

以上就是柔弱棒球为你收集整理的ZOJ3780-Paint the Grid Again的全部内容,希望文章能够帮你解决ZOJ3780-Paint the Grid Again所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部