您的位置:首页 > 其它

uva 639 Don't Get Rooked(回溯)

2013-07-20 13:10 387 查看


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

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

题目大意:求一个棋盘上最多能摆多少个車。
解题思路:深搜+回溯。

#include<iostream>
#include<string.h>
using namespace std;

#define M 8

char map[M][M];
int vis[M][M];
int n;
int max_deep;

int judge (int x, int y)
{
for (int i = 1; ; i++)
{
if (x - i < 0)
break;
else if (map[x - i][y] == 'X')
break;
else if (vis[x - i][y])
return 0;
}

for (int i = 1; ; i++)
{
if ( x + i >= n)
break;
else if (map[x + i][y] == 'X')
break;
else if (vis[x + i][y])
return 0;
}

for (int i = 1; ; i++)
{
if (y - i < 0)
break;
else if (map[x][y - i] == 'X')
break;
else if (vis[x][y - i])
return 0;
}

for (int i = 1; ; i++)
{
if (y + i >= n)
break;
else if (map[x][y + i] =='X')
break;
else if (vis[x][y + i])
return 0;
}
return 1;
}

void DFS(int x, int y, int deep)
{
while (1)
{
if (y >= n)
{
y = 0;
x++;
}

if (x >= n)
{
if (deep > max_deep)
max_deep = deep;
return;
}

if (map[x][y] == '.' && judge(x, y))
{
vis[x][y] = 1;
DFS(x, y + 1, deep + 1);
vis[x][y] = 0;
}

y++;
}
}

int main()
{
while (cin >> n, n)
{
// Init.
memset(vis, 0, sizeof(vis));
memset(map, 0, sizeof(map));
max_deep = 0;

// Read.
for (int i = 0; i < n; i++)
for (int j = 0;j < n; j++)
cin >> map[i][j];

DFS(0, 0, 0);

cout << max_deep << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: