POJ1315 UVA639 UVALive5325 Don't Get Rooked【DFS】

Don't Get Rooked
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2631 Accepted: 1682

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 4x4) 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 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.
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

Source

Mid-Central USA 1998

Regionals 1998 >> North America - Mid-Central USA

问题链接POJ1315 UVA639 UVALive5325 Don't Get Rooked
问题简述:(略)
问题分析
    棋盘上可以放多少个车互相吃不到的问题。'X'是障碍物,车是不能越过障碍物。
    暴力回溯。感觉这个题解算法不是最优,太暴力了,搜索的组合数太多。应该有更好的算法,暂时这样。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1315 UVA639 UVALive5325 Don't Get Rooked */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int DN = 4;       // 4个方向
const int drow[] = {0, 0, 1, -1};
const int dcol[] = {1, -1, 0, 0};

const char FLAG = '$';

const int N = 4;
char board[N][N + 1];
int n, ans;

bool judge(int row, int col)
{
    if(board[row][col] != '.') return false;
    else {
        for(int i = 0; i < DN; i++) {
            int nextrow = row + drow[i];
            int nextcol = col + dcol[i];
            while(nextrow >= 0 && nextrow < n && nextcol >= 0 && nextcol < n) {
                if(board[nextrow][nextcol] == 'X') break;
                else if(board[nextrow][nextcol] == FLAG) return false;
                else
                    nextrow += drow[i], nextcol += dcol[i];
            }
        }
    }
    return true;
}

void dfs(int lvl)
{
    ans = max(ans, lvl);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(judge(i, j)) {
                board[i][j] = FLAG;
                dfs(lvl + 1);
                board[i][j] = '.';
            }
}

int main()
{
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; i++)
            scanf("%s", board[i]);
        ans = 0;
        dfs(0);
        printf("%d\n", ans);
    }

    return 0;
}

posted on 2019-03-12 05:47  新海岛Blog  阅读(223)  评论(0编辑  收藏  举报

导航

// ... runAll: function() { this.resetPreCode(); hljs.initHighlightingOnLoad(); // 重新渲染,添加语法高亮 hljs.initLineNumbersOnLoad(); // 为代码加上行号 } // ...