﻿ UVA 639 (13.08.25)

### UVA 639 (13.08.25)

 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 44) 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.

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

## Sample Output

```5
1
5
2
4
```

AC代码:

```#include<stdio.h>

int max;
int vis[10][10];
char str[10];

int isOK(int x, int y) {
int l, u;
for(l = y-1; l >= 0; l--) {
if(vis[x][l] == -1)
break;
if(vis[x][l] == 0)
return 0;
}
for(u = x-1; u >= 0; u--) {
if(vis[u][y] == -1)
break;
if(vis[u][y] == 0)
return 0;
}
return 1;
}

void DFS(int x, int y, int nline, int count) {
while(x < nline) {
if(vis[x][y] == 1 && isOK(x, y)) {
vis[x][y] = 0;
count++;
DFS(x, y+1, nline, count);
vis[x][y] = 1;
count--;
}
if(y >= nline - 1) {
y = 0;
x++;
}
else
y++;
}
if(count >= max)
max = count;
}

int main() {
int n;
while(scanf("%d", &n) != EOF && n) {
max = 0;
for(int i = 0; i < n; i++) {
scanf("%s", str);
for(int j = 0; j < n; j++) {
if(str[j] == '.')
vis[i][j] = 1;
else
vis[i][j] = -1;
}
}

DFS(0, 0, n, 0);
printf("%d
", max);
}
return 0;
}```