ZOJ 3780 Paint the Grid Again

本文介绍了一个有趣的算法问题——使用有限次数的行和列涂色操作将初始无色的N×N网格涂成指定的黑白模式。文章详细阐述了解题思路,并提供了一段基于广度优先搜索(BFS)实现的示例代码。

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

Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest

 

题目大意:

 

你有一把魔力刷子,它可以把一个矩阵的任意一行涂成黑色,也可以把任意一列涂成白色。旧颜色会被新颜色覆盖,且每行每列最多涂一次。

 

现在给你一个n×n的矩阵,矩阵上各元素要么为黑要么为白,要你求如何把一个原先默认为无色的矩阵刷成给定矩阵,要求输出字典序最小的步骤。

 

大致思路:

 

刚开始看感觉这道题很复杂,后来仔细一想发现其实挺简单的…

 

思考一下我们发现,如果某一个元素为O(白色),那么它肯定有一次C[j],且在R[i]的后面。也就是说,如果所在元素那一行有黑色的话,说明它肯定比那一列先涂,这样那个元素才是白色。以此类推,我们就可以发现里面行列的顺序是可以确定的,接下来就是拓扑排序了。

 

因为要求字典序最小,那我们肯定优先画列的,既然如此我们建边的时候可以把i放大,然后搜的时候从小的开始搜就可以了。

 

这里还有一个小插曲,刚开始一直觉得深搜是不行的,觉得这样并不能保证找到字典序最小的序列,后来才发现是自己想错了,其实深搜是可以的…还是太弱了呀…

 

示例代码:(基于BFS)

 

 

//Paint the Grid Again.cpp -- zoj 3780
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 500 + 10;
int n, t, ans;
vector<int> G[maxn*2];
int indegree[maxn*2];
int topo[maxn*2];
char maze[maxn][maxn];
bool fx[maxn*2];
void toposort()
{
	t = 0, ans = 0;
	queue<int> que;
	for( int i=1; i<=2*n; i++ )
		if( !indegree[i] ) que.push(i);

	while( !que.empty() )
	{
		int v = que.front();
		que.pop();
		ans++;
		if( !fx[v] ) topo[t++] = v;
		for( int i=0; i<G[v].size(); i++ )
		{
			int u = G[v][i];
			indegree[u]--;
			if( !indegree[u] ) que.push(u);
		}
	}
	if( ans!=2*n )
		printf("No solution\n");
	else
	{
		for( int i=0; i<t-1; i++ )
		{
			if( topo[i]>=1 && topo[i]<=n )
				printf("C%d ", topo[i]);
			else
				printf("R%d ", topo[i]-n);				
		}
		if( topo[t-1]>=1 && topo[t-1]<=n )
			printf("C%d\n", topo[t-1]);
		else
			printf("R%d\n", topo[t-1]-n);
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while( T-- )
	{
		memset(indegree, 0, sizeof(indegree));
		memset(fx, 0, sizeof(fx));
		scanf("%d", &n);
		for( int i=1; i<=2*n; i++ ) G[i].clear();
		for( int i=1; i<=n; i++ )
			scanf("%s", maze[i]+1);
		for( int i=1; i<=n; i++ )
		{
			int f1 = 0, f2 = 0;
			for( int j=1; j<=n; j++ )
			{
				if( maze[i][j]=='O' )
				{
					G[i+n].push_back(j);
					indegree[j]++;
					f1++;
				}
				if( maze[j][i]=='X' )	// 这里i、j的用法很巧妙嘛…
				{
					G[i].push_back(j+n);
					indegree[j+n]++;
					f2++;
				}
				if( f1==n ) fx[i+n] = 1;	// 如果某一行全为白色,那说明黑色不用涂。
				if( f2==n ) fx[i] = 1;		// 同理。
				// 这种做法的好处是可以保证节点数总为2*n,便于判断是否有解。
			}
		}
		toposort();
	}
	return 0;
}

基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB代码 动态避障路径规划:基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB 融合DWA的青蒿素优化算法(AOA)求解无人机三维动态避障路径规划,MATLAB代码 基于动态环境下多智能体自主避障路径优化的DWA算法研究,MATLAB代码 融合DWA的青蒿素优化算法AOA求解无人机三维动态避障路径规划,MATLAB代码 基于DWA的多智能体动态避障路径规划算法研究,MATLAB代码 融合动态窗口法DWA的粒子群算法PSO求解无人机三维动态避障路径规划研究,MATLAB代码 基于粒子群算法PSO融合动态窗口法DWA的无人机三维动态避障路径规划研究,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于DWA的动态环境下无人机自主避障路径优化,MATLAB代码 基于DWA的动态环境下机器人自主避障路径规划,MATLAB代码 基于城市场景下RRT、ACO、A*算法的无人机三维路径规划方法研究,MATLAB代码 基于城市场景下无人机三维路径规划的导航变量的多目标粒子群优化算法(NMOPSO),MATLAB代码 导航变量的多目标粒子群优化算法(NMOPSO)求解复杂城市场景下无人机三维路径规划,MATLAB代码 原创:5种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 原创:4种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 高维超多目标优化:基于导航变量的多目标粒子群优化算法(NMOPSO)的无人机三维
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值