UVA - 639
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
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 44) 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
Source
Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 3. Problem Solving Paradigms :: Complete Search :: Iterative
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Complete Search :: Iterative (Fancy Techniques)
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms :: Complete Search :: Iterative (The More Challenging Ones)
思路:就是一个一个枚举啦,暴力都只要9ms,具体看代码
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N;
char str[10];
int map[10][10];
int ans;
int judge() { //递归边界,此时不能再放车
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
if(map[i][j] == 0) return 0;
return 1;
}
void sign(int i, int j) { //标记
map[i][j] ++;
for(int k = i + 1; k < N; k++) {
if(map[k][j] == -1) break;
map[k][j] ++; //注意这里要进行++,别赋值为1,回溯的时候--就好了,如果这里赋值为1,
} //回溯的时候再赋值为0,那么会影响别的车的攻击范围,从而进入死循环
for(int k = i - 1; k >= 0; k--) {
if(map[k][j] == -1) break;
map[k][j] ++;
}
for(int k = j + 1; k < N; k++) {
if(map[i][k] == -1) break;
map[i][k] ++;
}
for(int k = j - 1; k >= 0; k--) {
if(map[i][k] == -1) break;
map[i][k] ++;
}
}
void unsign(int i, int j) { //回溯
map[i][j] --;
for(int k = i + 1; k < N; k++) {
if(map[k][j] == -1) break;
map[k][j] --;
}
for(int k = i - 1; k >= 0; k--) {
if(map[k][j] == -1) break;
map[k][j] --;
}
for(int k = j + 1; k < N; k++) {
if(map[i][k] == -1) break;
map[i][k] --;
}
for(int k = j - 1; k >= 0; k--) {
if(map[i][k] == -1) break;
map[i][k] --;
}
}
void fun(int cnt) {
if(judge()) { //递归边界,此时不能再放车
ans = max(ans, cnt);
return;
}
for(int i = 0; i < N; i++) { //逐个枚举
for(int j = 0; j < N; j++) {
if(map[i][j] == 0) {
sign(i, j); //标记
fun(cnt+1); //递归
unsign(i, j); //回溯
}
}
}
}
int main() {
while(scanf("%d", &N), N) {
for(int i = 0; i < N; i++) {
scanf("%s", str);
for(int j = 0; str[j] != '\0'; j++) {
if(str[j] == '.') map[i][j] = 0; //空用0表示
else map[i][j] = -1; //墙用-1表示,和其他的区别开来
}
}
//system("pause");
ans = 0;
fun(0);
printf("%d\n", ans);
}
return 0;
}