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. htm 与 html 的区别

    htm 与 html 的区别 前者是超文本标记(Hypertext Markup) 后者是超文本标记语言(Hypertext Markup Language) 可以说 htm = html 同时,这两 ...

  2. 手机端实现6位短信验证码input输入框效果(样式及代码方法)

    微信移动端4位.6位.多位验证码密码输入框功能的实现代码,实现思路: 方案1: 写一个简单的input框. 评估:样式不好看,待定. 方案2: 就是用6个input框,每输入一个数字之后,切换到下一个 ...

  3. [转]广义正交匹配追踪(gOMP)

    广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...

  4. andorid IOS 判断APP下载

    <?phpif(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad' ...

  5. [Luogu1345][USACO5.4]Telecowmunication 最大流

    题目链接:https://www.luogu.org/problem/show?pid=1345 求最小割点集的大小,直接拆点转化成最小割边.把一个点拆成出点入点,入点向出点连一条容量为1的边,其他的 ...

  6. Thinkphp删除缓存

    控制器代码   public function delcache(){ //当找到有Runtime的文件夹时,进入if if(is_dir(RUNTIME_PATH)){ delDir(RUNTIME ...

  7. Toast解析

    课程Demo public class MainActivity extends AppCompatActivity { private Button bt1; private Button bt2; ...

  8. Android学习笔记(九) SeekBar和RatingBar

    一.SeekBar的主要属性 -max -progress -secondaryProgress 二.onSeekBarChangeListener -onProgressChanged(SeekBa ...

  9. springmvc 获取数组

    spingmvc 获取数据有这几种方式:1.通过HttpRequestServlet的方法获取数据.2.form表单传递对象字段,springmvc自动获取.3.ajax 请求通过注解的方式直接获取数 ...

  10. RDO、SAD、SATD、λ相关概念【转】

    率失真优化概述: 率失真优化(Rate D isto r t i on Op t i m ized)策略是在率失真理论[3 ]的基础上提出的一种代价函数方案, RDO 的主要思想是, 在计算代价函数时 ...