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 4$	imes$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


题意:

类似八皇后的一道题, 但是这道题多了一个"墙"的概念, 使得一行放置多辆车变成可能的(对列也是)

而且, 同一条斜线是允许多辆车的~


这里写下我解题的心得, 算是记下笔记了:

这是一道回溯题, 刘汝佳算法书上对回溯的描述是"在递归构造中, 生成和检查过程可以有机的结合起来, 从而减少不必要的枚举, 这就是回溯法";

回溯法何时应用?

只要能把待求解的问题分成不太多的步骤, 每个步骤又只有不太多的选择, 都应该考虑使用回溯法~


做法:

每个点都有两种选择, 停车或者不停车, 所以可以根据这个递归

然后, 中途是要判断是否可以停车的, 不是每个点想停就能停, 所以我写了一个 isOK 的判断函数


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;
}
原文地址:https://www.cnblogs.com/riskyer/p/3281486.html