ZOJ - 3780 (拓扑排序+思维)

ZOJ - 3780 (拓扑排序+思维)

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

题目大意:

刷墙游戏,只允许两种操作:
1.Ra 就是把a这一行全涂成X
2.Ca 就是把a这一列全涂成O
涂的颜色是可以覆盖的。最初墙上啥也没有,问最少需要多少操作能把这个墙涂成给定的模样,把所有的操作按字典序排序(其实就是可以同时进行R 和 C 操作的时候先进行R操作)输出。


解题思路:

一开始没看出这是拓扑排序来,后来仔细想了想才想明白是用拓扑排序。
问题的关键点就是在于怎么建边,只要边建出来了,那问题就基本上解决了。

我们把每一种操作抽象成为一个点,比如R1操作,就是把第一行全涂成X 把这个操作抽象为一个点吗,标号为1。这样的操作一共有多少个呢?R1 R2 R3…Rn, C1,C2,C3…Cn。一共有2n个操作,所以就有2n个点。标号怎么给呢?因为R操作的优先级高于C 所以我们把R操作标记为1–n C操作标记为n+1–2n;

点解决了,那边呢??

我们考虑每一个格子,如果这个格子(i,j)里是X,那么这个格子的最终操作一定是Ri 所以说,Ri操作一定会在C(j+n)这个操作之后,所以我们建边是 (j+n)->i,同样如果这个格子是O,那么这个格子的最终操作一定是C(j+n),所以Ri一定在C(j+n)之前 所以我们建边 i->(j+n);
建边代码如下:

	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(mp[i][j]=='O'){
				add(i,j+n);
				in[j+n]++;
				vis[i]++;
			}
			else{
				add(j+n,i);
				in[i]++;
				vis[j+n]++;
			}
		}
	}

in数组存的是这个点的入度,拓扑排序时需要用到。vis数组存的从这个点出发的边有多少条。如果从这个点出发的边有n条的话,那么这个操作就是非必须的操作,在答案中就不用存它了(这是一个需要注意的小细节)。
还有一个细节是,拓扑排序的时候要用优先队列,因为输出顺序有优先级嘛~

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int maxn=3e5;
int head[maxn];
int cnt=0;
int n;
int in[maxn];
int vis[maxn];
char mp[550][550];
int ans[maxn];
struct node{
	int to;
	int next;
}side[maxn*2];
void add(int x,int y){
	side[cnt].to=y;
	side[cnt].next=head[x];
	head[x]=cnt++;
}
int ss=0;
void init(){
	memset(head,-1,sizeof(head));
	memset(vis,0,sizeof(vis));
	memset(in,0,sizeof(in));
	cnt=0;
	ss=0;
}

int tupu(){
	priority_queue<int,vector<int>,greater<int> > q;
	for(int i=1;i<=n*2;i++){
		if(in[i]==0){
			q.push(i);
		}
	}
	while(q.size()){
		int u=q.top();
		q.pop();
		if(vis[u]!=n) 
			ans[ss++]=u;
		for(int i=head[u];i!=-1;i=side[i].next){
			int to=side[i].to;
			in[to]--;
			if(in[to]==0){
				q.push(to);
			}
		}
	}
	int flag=1;
	for(int i=1;i<=2*n;i++){
		if(in[i]){
			flag=0;
			break;
		}
	}
	return flag;
}
int main()
{
	int t;
	cin>>t;
	while(t--){
		scanf("%d",&n);
		init();
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				cin>>mp[i][j];
			}
		}	
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(mp[i][j]=='O'){
					add(i,j+n);
					in[j+n]++;
					vis[i]++;
				}
				else{
					add(j+n,i);
					in[i]++;
					vis[j+n]++;
				}
			}
		}
		if(!tupu()) printf("No solution\n");
		else{
			for(int i=0;i<ss;i++){
				if(ans[i]<=n){
					cout<<"R"<<ans[i];
				}
				else{
					cout<<"C"<<ans[i]-n;
				}
				if(i==ss-1) cout<<endl;
				else cout<<" ";
			}
		}

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43179892/article/details/89449301