【ZOJ - 3780】Paint the Grid Again(拓扑排序,图论,证明性质)

题干:

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 Tindicating 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

题目大意:

对一个空白的矩阵进行两种操作:可以将一行涂黑,将一列涂白,每一行每一列只能涂一次,给最终的图案,要求用最少次数涂出要求的图案并输出步骤。如果有多种可能性满足,那就输出字典序最小解。定义字典序:前k-1个操作相同的话,比较第k个。列比行的优先级高,其次是行(列)号小的优先级高。

解题报告:

  首先需要证明的是行列之间的约束是唯一的。也就是不存在先某一行再某一列  或者  先某一列再某一行  同时成立的情况。猜出结论,然后尝试证明。对于每一个格子拆成n行,n列,这是一个二分图,对于每一个格子,相当于是一个约束,如果这个格子的最终值是X,说明列比行优先,反之说明行比列优先。假设A比B优先,那么我们就A->B连一条边。所以我们有n^2个约束关系。也就是说对于那个二分图,任意行列之间都有一个连线,那么他是个完全二分图,所以如果存在先行后列和先列后行都可以的情况,那么对于选中的那一行和那一列的那个交点来说,就是先行在列或者先列在行都可以。这就产生了矛盾,因为这显然是不符合我们刚刚构造的那个二分图的,因为刚开始是个空白图,也就是说如果这个格子所在的行列都在答案内,那么顺序是根据这个点的值是X还是O早就被确定了的,所以不可能有先行再列或者先列行都存在的情况这是不可能的。

所以我们拓扑排序的时候只需要维护那个下标之间的大小关系就可以了,对于行列之间的关系我们完全不需要管,因为完全不会存在这里的矛盾。这就大大简化了问题。

再其次就是那个vis数组的含义了,这一点也不难理解,因为我们要求的是字典序最小,所以没必要的操作我们肯定是希望做的越少越好,所以用vis来判断对这一行(列)的约束有几条了,如果约束很多,比如对某一行的约束到达了n条,说明我画不画这一行其实都差不了多少,因为最后这一行都要被覆盖成列的颜色(也就是都是O)那么我还要涂这一行干啥呢?

所以有这一个操作来减少没必要的操作,符合题意。有的时候题目给了很多限制但是其实都用不到,因为题目虽然没有说输出任意解,但是有的时候约束还是很少的,有的时候他只是这样迷惑你而已。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
char s[MAX];
int vis[MAX],in[MAX],n;
vector<int> vv[MAX],ans;//1~n代表行  n+1~2*n代表列。 
bool topu() {int tot = 0;ans.clear();priority_queue<int,vector<int> , greater<int> > pq;for(int i = 1; i<=2*n; i++) {if(in[i] == 0) pq.push(i);}while(pq.size()) {int cur = pq.top();pq.pop();tot++;if(vis[cur] != n) ans.pb(cur);int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];in[v]--;if(in[v] == 0) pq.push(v);  }}if(tot<2*n) return 0;else return 1;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=2*n; i++) vv[i].clear(),vis[i]=0,in[i]=0;for(int i = 1; i<=n; i++) {scanf("%s",s+1);for(int j = 1; j<=n; j++) {if(s[j] == 'X') vv[n+j].pb(i),vis[n+j]++,in[i]++;else vv[i].pb(n+j),vis[i]++,in[n+j]++;}}bool flag = topu(); if(flag == 0) {puts("No solution");continue;}int up = ans.size();for(int i = 0; i<up; i++) {if(ans[i]>n) printf("C%d%c",ans[i]-n,i == up-1?'\n':' ');else printf("R%d%c",ans[i],i == up-1?'\n':' ');}}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440414.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux bin su,linux – su:/ bin / bash:资源暂时不可用

无法将用户切换为postgres.postgres用户的ulimit设置设置了合理的限制.我们没有达到最高限度./ var / log / messages中没有错误.BETA -bash-4.2# sudo su - postgressu: /bin/bash: Resource temporarily unavailable设置&#xff1a;BETA -bash-4.2# ps -auxww | grep -i pos…

*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

题干&#xff1a; Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both…

c语言程序图片马赛克,关于c语言的图像均值滤波 请问大神为什么我的结果都是马赛克...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include #include #include #include #include #include "stdlib.h"#include "string.h"#define width 256#define higth 256//原图象的宽度和高度int lvbo(unsigned char D[]){int a;a(D[0]D[1]D[2…

【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

c语言程序设计现代方法快速排序,C语言实现快速排序改进版

利用三者取中法改进快速排序&#xff0c;具体内容如下实现取数组中第一个,中间和最后一个元素的中间元素作为划分元素(否则将这些元素排除在划分过程之外).大小为11或更小的数组在划分过程中被忽略,然后使用插入排序来完成排序.#include #include #include #include #include #…

c语言一个数组后添加元素append,jQuery 追加元素、拼接元素的方法总结(append、html、insertBefore、before等)...

1. append & appendTo 的功能均为&#xff1a;在被选元素结尾(仍在元素内部)插入指定内容&#xff0c;但是内容和选择器的位置不同(1) append()方法&#xff1a;$("#test").append("测试"); //在id为test元素内部末尾插入测试(2) appendTo()方法&…

【ZOJ - 4024】Peak(模拟,水题)

题干&#xff1a; A sequence of integers is called a peak, if and only if there exists exactly one integer such that , and for all , and for all . Given an integer sequence, please tell us if its a peak or not. Input There are multiple test cases. …

【ZOJ - 4029】Now Loading!!!(整除分块,思维,二分,前缀和)

题干&#xff1a; 其中 zi 是第i次询问后的z。 解题报告&#xff1a; 因为有取log运算&#xff0c;所以分母的取值肯定不会超过30种&#xff0c;所以分每一个分母的时候&#xff0c;用前缀和优化一个和&#xff0c;最后求乘积就行了。&#xff08;其实不需要快速幂&#xff0c…

迷宫小游戏c语言代码,C语言编写的迷宫小游戏-源代码

#include #define N 20/*迷宫的大小&#xff0c;可改变*/int oldmap[N][N];/*递归用的数组,用全局变量节约时间*/int yes0;/*yes是判断是否找到路的标志,1找到&#xff0c;0没找到*/int way[100][2],wayn0;/*way数组是显示路线用的,wayn是统计走了几个格子*/void Init(void);/*…

【ZOJ - 4032】Magic Points (思维,几何,构造)

题干&#xff1a; 解题报告&#xff1a; 想到了&#xff0c;这样绕圈构造。但是这样有个问题&#xff0c;最后一个点如何构造。 刚开始想的是n奇数 &#xff0c; 就8 10 这样的连一条&#xff0c;n偶数 就8 11 这样的连一条&#xff0c;随便构造一下就行&#xff0c;但是发…

s7-200 plc 梯形图转换成c语言的方法,S7-200PLC模拟量4-20mA转换成整数程序算法示例.pdf...

S7200PLC 模拟量4 &#xff0d;20 mA 转换成整数程序算法示例S7200PLC 模拟量4 &#xff0d;20 mA 转换成整数程序算法示例&#xff0c;以及模拟量数据滤波平缓处理&#xff0c;消除曲线的尖峰毛刺程序示例。连续采集10 个数&#xff0c;找出最大值&#xff0c;最小值&#xff…

【ZOJ - 4033】CONTINUE...? (思维,整体思想,分组思想)

题干&#xff1a; DreamGrid has classmates numbered from to . Some of them are boys and the others are girls. Each classmate has some gems, and more specifically, the -th classmate has gems. DreamGrid would like to divide the classmates into four group…

android 仿真翻页动画,Android 两Activity之间动画效果(1)---------翻页效果

用Android rotate动画实现翻页效果&#xff0c;效果如图&#xff1a;要实现上面动画&#xff0c;首先搞明白rotate动画原理&#xff1b;(1)Degrees坐标&#xff1a;0度(360度)270度90度 顺时针旋转 180(2)rotate 关键属性fromDegrees 开始旋转时角度 toDegrees 结束时的角…

android 存储不被垃圾清理,手机内存足够大,就不需要清理垃圾了?你错了!

原标题&#xff1a;手机内存足够大,就不需要清理垃圾了?你错了!中新网4月20日电今天,人们使用智能手机的时间已超过电脑,希望在任何时候、任何地方,一部手机搞定所有。对手机的流畅度、性能和安全的要求越来越高。新手机刚到手时非常流畅,用一段时间就出现各种卡顿,网民对猎豹…

【蓝桥杯 - 真题】六角幻方(dfs+剪枝)

标题&#xff1a;六角幻方 把 1 2 3 ... 19 共19个整数排列成六角形状&#xff0c;如下&#xff1a; * * * * * * * * * * * * * * * * * * * 要求每个直线上的数字之和必须相等。共有15条直线哦&#xff01; 再给点线索吧&#xff01;我们预先填好了2个数字&…

【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)

题干&#xff1a; The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be held in Moscow, Russia. To celebrate this annual event for the best competitive programmers around the world, it is decided to host a welcome pa…

app android de,Android Deobfuscation

Android Deobfuscation11/11/2019本文内容ProGuard、DexGuard 和 R8 是用于对 Android 应用程序的代码进行优化和模糊处理的工具。 它会删除未使用的代码、重命名具有语义模糊名称的类、字段和方法&#xff0c;使基本代码更小&#xff0c;更难反向工程。 若要在 Android 应用中…

【2019浙江省赛 - B】Element Swapping(思维,数学)

题干&#xff1a; DreamGrid has an integer sequence and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements and () in the sequence when DreamGrid wasnt at home. When DreamGrid comes back, he finds with dismay that his …

android 没有指令,android – 运行时没有命令输出:’am start -n

最近,当我试图在我的设备上运行我的Android应用程序时,我必须在实际启动之前从Eclipse运行它几次.我试图重新安装JRE,JDK和IDE,我试图切换工作区.我还让Eclipse为Java VM使用更多的RAM.我的IDE,JRE,JDK,ADT和ADT插件都是最新的.对此有任何建议非常感谢.No command output when …

【2019浙江省赛 - E】Sequence in the Pocket(思维)

题干&#xff1a; DreamGrid has just found an integer sequence in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move i…