ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 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 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

xxxo先涂行,在涂列

x

o

x

x 先涂列在涂行。 因此可以构造一个图,进行topsort。如果要col先可以给col 小的标号,每次用无入度点的最小点。

记住,刚开始的无入度点可以不涂。

#include #include #include #include #include #include #include #include #include #include #include #define FOR(i,a,b) for(int i=a;i=b;--i) #define clr(f,z) memset(f,z,sizeof(f)) #define ll(x) (1 #define PB push_back #define LL long long using namespace std; const int mm = 508; class Node { public: int v,next; }e[mm*mm*2]; int head[mm+mm],edge,id[mm+mm]; bool first[mm+mm]; vectorans; priority_queue,greater > Q; void init() { clr(head,-1); edge = 0; clr(id,0); clr(first,0); } void add(int u,int v) { e[edge].v = v; e[edge].next = head[u]; head[u] = edge++; ++id[v]; } int N; char s[mm][mm]; bool topsort(int n) { for(int i=0;i

点击复制链接 与好友分享!回本站首页
您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力
上一篇:leetcode第一刷_Symmetric Tree
下一篇:HDOJ 1385 Minimum Transport Cost
相关文章

ZOJ1543 Stripies

ZOJ2425

ZOJ 1205 Martian Addition

ZOJ2722Head-to-Head Match

ZOJ2723 Semi_Primer

ZOJ 3508 The War 贪心

ZOJ 3622 Magic Number 月赛水题

ZOJ 3627 Treasure Hunt II

ZOJ 3631 Watashi's BG

ZOJ 2587 最小割的唯一性

图文推荐

ZOJ 3780 Paint the Grid Again(topsort)
ZOJ 3640 Help Me
ZOJ 3780 Paint the Grid Again(topsort)
CF 518C(Anya and
ZOJ 3780 Paint the Grid Again(topsort)
hdu 1016 Prime R
UVA - 11987 - A

分类:默认分类 时间:2015-02-26 人气:7
本文关键词:
分享到:

相关文章

Copyright (C) quwantang.com, All Rights Reserved.

趣玩堂 版权所有 京ICP备15002868号

processed in 0.060 (s). 10 q(s)