UVA - 639 Don't Get Rooked (回溯)

本文介绍了一个类似于N皇后问题的算法挑战,目标是在包含墙壁的小型棋盘上放置尽可能多的“车”,使得任意两个“车”都无法互相攻击。通过使用回溯法解决了这一问题,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Don't Get Rooked 

In chess, the rook is a piece that can move any number of squaresvertically or horizontally. In this problem we will consider smallchess boards (at most 4$\times$4) that can also contain walls through whichrooks cannot move. The goal is to place as many rooks on a board aspossible so that no two can capture each other. A configuration ofrooks is legal provided that no two rooks are on the samehorizontal row or vertical column unless there is at least one wallseparating them.


The following image shows five pictures of the same board. Thefirst picture is the empty board, the second and third pictures show legalconfigurations, and the fourth and fifth pictures show illegal configurations.For this board, the maximum number of rooks in a legal configurationis 5; the second picture shows one way to do it, but there are severalother ways.

Your task is to write a program that, given a description of a board,calculates the maximum number of rooks that can be placed on theboard in a legal configuration.

Input 

The input file contains one or more board descriptions, followed bya line containing the number 0 that signals the end of the file. Eachboard description begins with a line containing a positive integer nthat is the size of the board; n will be at most 4. The next nlines each describe one row of the board, with a `.' indicating anopen space and an uppercase `X' indicating a wall. There are nospaces in the input file.

Output 

For each test case, output one line containing themaximum number of rooks that can be placed on the boardin a legal configuration.

Sample Input 

4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0

Sample Output 

5
1
5
2
4


题目大意:
这是一道类似于N皇后的问题,叫我们将车放到小棋盘上面去,规则也类似,让每个车都不能攻击到其他车,
然后有墙阻隔,车是不能攻击到墙后的车的。

解析:用回溯法来解决这个问题。
定义一个vis数组,先将棋盘的情况读入,如果该点未被访问过,且不能攻击到4个方向的车,而且该点为'.',
该点访问就将vis[i][j]置为1,然后递归且将sum+1,因为是回溯法最后别忘了把vis[i][j]置为0。

#include <stdio.h>
#include <string.h>
const int INF = -0x3f3f3f;
const int N = 10;
int vis[N][N];
char grid[N][N];
int n;
int max;

bool judge(int r,int c) {
	bool flag = true;
	for(int i = r-1; i >= 0; i--) {
		if(vis[i][c]) {
			flag = false;
		}else if(grid[i][c] == 'X') {
			break;
		}
	}
	for(int i = r+1; i < n; i++) {
		if(vis[i][c]) {
			flag = false;
		}else if(grid[i][c] == 'X') {
			break;
		}
	}
	for(int i = c-1; i >=0; i--) {
		if(vis[r][i]) {
			flag = false;
		}else if(grid[r][i] == 'X') {
			break;
		}
	}
	for(int i = c+1; i < n; i++) {
		if(vis[r][i]) {
			flag = false;
		}else if(grid[r][i] == 'X') {
			break;
		}
	}
	return flag;
}

void dfs(int r,int c,int sum) {
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			if(grid[i][j] == '.' && judge(i,j)&& !vis[i][j]) {
				vis[i][j] = true;
				dfs(i,j,sum+1);
				vis[i][j] = false;
			}
		}
	}
	if(max < sum) {
		max = sum;
	}
}

int main() {
	while(scanf("%d",&n) != EOF && n) {
		memset(grid,0,sizeof(grid));
		memset(vis,0,sizeof(vis));
		for(int i = 0; i < n; i++) {
			scanf("%s",grid[i]);
		}
		max = INF;
		dfs(0,0,0);
		printf("%d\n",max);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值