UVa 639 - Don't Get Rooked 类皇后问题 递归回溯

Don't Get Rooked 

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


The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of rooks in a legal configuration is 5; the second picture shows one way to do it, but there are several other 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 the board in a legal configuration.

Input 

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

Output 

For each test case, output one line containing the maximum number of rooks that can be placed on the board in 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



Miguel A. Revilla 
2000-01-17


在西洋棋中,城堡(rook)可以水平或垂直的移動任意格。在這個問題中,我們想要知道在一個小棋盤(最多 4x4)最多可以放置多少個城堡,並且這些城堡不會處於可以互相攻擊的狀態。比較特別的是在棋盤中有些格子放有牆,這些牆會阻擋城堡的行進路線。所以我們可以說一個棋盤若處於一種「合法」的狀態,那表示不會有2個城堡處於同一列或同一行,除非他們之間至少存在一個牆把他們隔開。

以下以下的圖顯示了五個相同的棋盤。第一個棋盤是空的。第二、三個棋盤是「合法」的狀態。第四、五個棋盤是「不合法」的狀態。對這個棋盤而言,最多可以放置 5 個城堡,並且棋盤仍處於「合法」的狀態(第二個圖是一種安排城堡的方式,但是還有其他的方式)。


你的任務是寫一個程式,給你一個棋盤的描述,請你算出最多可以放置多少個城堡,並且棋盤仍處於「合法」的狀態。

Input

輸入含有多組測試資料。每組測試資料的第一列有 1 個整數 n,代表棋盤的大小(1 <= n <= 4),接下來的 n 列描述這個棋盤。'.' 代表空白格,'X'代表牆。輸入中不會有空白存在。

當 n=0 時代表輸入結束。請參考Sample Input。

Output

對每一組測試資料,輸出最多可以放置多少個城堡,並且棋盤仍處於「合法」的狀態。



#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;

const int MAX =4+1;

char g[MAX][MAX];
int vis[MAX][MAX];
int n;
int ans=0;

void init()
{
	ans = 0;
	memset(g, '\0', sizeof(g));
	memset(vis, 0, sizeof(vis));
}

void read()
{
	for(int i=0; i < n; i++)
		cin >> g[i];
}
// 对放入的值 的左右上下四个方向进行标记 
void alVis(int u, int v, int count)
{
	char value = 'a'+count;
	g[u][v]=value;
	// 往左 
	for(int i=v-1; i >= 0; i--)
	{
		if(g[u][i]=='X') break;
		else if(g[u][i]=='.') g[u][i]=value; 
	}
	// 往右 
	for(int i=v+1; i < n; i++)
	{
		if(g[u][i]=='X') break;
		else if(g[u][i]=='.') g[u][i]=value; 
	}
	// 往上 
	for(int i=u-1; i >= 0; i--)
	{
		if(g[i][v]=='X') break;
		else if(g[i][v]=='.') g[i][v]=value;
	}
	// 往下 
	for(int i=u+1; i < n; i++)
	{
		if(g[i][v]=='X') break;
		else if(g[i][v]=='.') g[i][v] =value;
	}	
}
// 对放入的值的四个方向的标记进行撤销 
void InAlVis(int u, int v, int count)
{
	char value = 'a'+count;
	g[u][v]='.';
	for(int i=v-1; i >= 0; i--)
	{
		if(g[u][i]=='X') break;
		else if(g[u][i]==value) g[u][i]='.'; 
	}
	for(int i=v+1; i < n; i++)
	{
		if(g[u][i]=='X') break;
		else if(g[u][i]==value) g[u][i]='.'; 
	}
	for(int i=u-1; i >= 0; i--)
	{
		if(g[i][v]=='X') break;
		else if(g[i][v]==value) g[i][v]='.';
	}
	for(int i=u+1; i < n; i++)
	{
		if(g[i][v]=='X') break;
		else if(g[i][v]==value) g[i][v] ='.';
	}	
}

void dfs(int u, int count)
{
	if(count>ans) ans = count;
	for(int i=u; i < n; i++)
	for(int v=0; v < n; v++)
	{
		if(g[i][v]=='.')
		{
			// 对填入的数的行列进行标记,利用count 的值是在回溯的时候可以指定位置回溯 
			alVis(i, v, count);
			++count ;
			dfs(v, count);
			--count;
			InAlVis(i, v, count);
		}
	}
}

void solve()
{
	dfs(0, 0);
	cout << ans << endl;
}

int main()
{
//	freopen("in.txt","r",stdin);
	while(1)
	{
		cin >> n;
		if(n==0) break;
		init();
		read();
		solve();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值