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

如果某一行存在x,则这一行必然会被横着涂一遍,同时上一次可以为竖着涂这一列;如果是o也可以得到相似的结论,所以这样可以建立起一个有向图,用拓扑排序判断即可。

BFS拓扑排序

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=505;
vector<int>G[N<<1];
int n;
char a[N];
queue<int>que;
bool del[N<<1],vis[N<<1];
int in[N<<1];
bool bfs(){
memset(del,false,sizeof(del));
memset(vis,false,sizeof(vis));
priority_queue<int,vector<int>,greater<int> >q;
for(int i=1;i<=(n<<1);i++) if(in[i]==0)
q.push(i),vis[i]=true;
if(q.size()==0) return false;
for (int i=1;i<=(n<<1);i++){
int cur = q.top();
q.pop();
if(del[cur]) return false;
del[cur]=true;que.push(cur);
for (int j=0;j<G[cur].size();j++){
int nxt=G[cur][j];
if(del[nxt]) return false;
in[nxt]--;
if(in[nxt]==0)
q.push(nxt);
}
}
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(in,0,sizeof(in));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[j].push_back(i+n);
in[i+n]++;
}
else{
G[i+n].push_back(j);
in[j]++;
}
}
}
if(!bfs()||que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
if(vis[x]) continue;
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(!que.empty()) printf(" ");
}
puts("");
}
return 0;
}

dfs

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=515;
vector<int>G[N<<1];
int id[N<<1],n;
char a[N];
int vis[N<<1];
queue<int>que; bool dfs(int u)
{
vis[u]=-1;
sort(G[u].begin(),G[u].end());
for(int i=0; i<(int)G[u].size(); i++){
int v=G[u][i];
if(vis[v]<0) return false;
if(!vis[v]&&!dfs(v)) return false;
}
vis[u]=1;
if(id[u]!=n) que.push(u);
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(id,0,sizeof(id));
memset(vis,0,sizeof(vis));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[i+n].push_back(j); //横着,大于n的为横着的标号
id[j]++;
}
else{
G[j].push_back(i+n); //竖着,小于n为竖着的标号
id[i+n]++;
}
}
}
for(int i=1; i<=(n<<1); i++) if(!id[i]&&!vis[i]) if(!dfs(i)){
//无前驱的节点可以作为开始节点
while(!que.empty()) que.pop();
break;
}
if(que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(que.empty()) puts("");
else printf(" ");
}
}
return 0;
}

【ZOJ - 3780】 Paint the Grid Again (拓扑排序)的更多相关文章

  1. ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

    Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cel ...

  2. ZOJ 3780 E - Paint the Grid Again 拓扑排序

    https://vjudge.net/problem/49919/origin 题意:给你n*n只出现O和X的字符阵.有两种操作,一种操作Ri将i行全变成X,一种操作Ci将i列全变成O,每个不同的操作 ...

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

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  4. ZOJ 3780 Paint the Grid Again

    拓扑排序.2014浙江省赛题. 先看行: 如果这行没有黑色,那么这个行操作肯定不操作. 如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行 如果不全是 ...

  5. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  6. ZOJ - 3780-Paint the Grid Again-(拓扑排序)

    Description Leo has a grid with N × N cells. He wants to paint each cell with a specific color (eith ...

  7. ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)

    Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows ...

  8. ZOJ 3781 Paint the Grid Reloaded(BFS)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...

  9. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

随机推荐

  1. php安装的扩展php -m可以看到,但是phpinfo()看不到,php-fpm关闭了重新打开还是不行?

    问答 问答详情   php安装的扩展php -m可以看到,但是phpinfo()看不到,php-fpm关闭了重新打开还是不行? centos apache linux html php 3.2k 次浏 ...

  2. InputFilter在过滤空格时重复输入的问题

    正确做法:editText.setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filte ...

  3. 在Python解释器运行程序

    在解释器中运行  ***.py文件的方法:使用import添加模块 ***.py,然后调用 ***.py中的函数 例:在zoo.py中定义hours函数 运行方法: >>> impo ...

  4. 用for循环实现的菱形图案

    package com.wh.lingxing; import java.util.Scanner; public class LingXing { public static void main(S ...

  5. [转]F# Samples 101 - Visual Studio 2010

    http://code.msdn.microsoft.com/F-Samples-101-0576cb9f/sourcecode?fileId=18956&pathId=1045958806 ...

  6. [BZOJ4815][CQOI2017]小Q的表格 数论+分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4815 题目中所给条件中的$(a,a+b)$和$(a,b)$的关系很瞩目. 然后大家都知道$ ...

  7. Nginx反向代理node,实现让静态文件在同一域

    Nginx反向代理node,实现让静态文件在同一域 原文https://github.com/zhuangZhou/Blog/issues/4 不管是Vue还是React,还是传统的网站,与node服 ...

  8. web安全后渗透--XSS平台搭建及使用

     xss平台搭建 1.申请一个云主机来进行建站:149.28.xx.xx 2.安装lnmp: wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO ln ...

  9. deallocvt - 释放未使用的虚拟终端

    SYNOPSIS(总览) deallocvt [ N1 N2 ... ] DESCRIPTION(描述) 如果不指定参数, deallocvt 程序会释放所有未使用的虚拟终端的核心内存和数据结构. 如 ...

  10. 移动端1px线适配问题-------适配各种编译CSS工具 stylus sass styled-componet实现方法

    其实在stylus与sass中实现移动端1像素线各个手机设备的适配问题的原理是一样的, 首先我还是先介绍一下原理和所依赖的方法 原理:其实他们都是通过css3的媒体查询来实现的 步骤思路: 1.给目标 ...