HDU1045-Fire Net

题目链接:点击打开链接

Problem Description

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

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 blockhouses 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 map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

Input

The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, 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 blockhouses that can be placed in the city in a legal configuration.

Sample Input

 

4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0

Sample Output

 

51524

思路:以为是皇后问题,后来发现不是,数据比较小,dfs可以过。  看到有二分图的做法,理解不了。。

AC代码:

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 10010;
const int INF = 0X3f3f3f;

char G[6][6];
int n, vis[6][6];
int maxx;

bool check(int i, int j) {//涉及到排 和 列 , 四个方向
    if(vis[i][j] != 0)//不是空地
        return false;
    for(int x = i - 1; x >= 0; x--) {
        if(vis[x][j] == 1)//遇到墙就break 很好的想法。说明可以
            break;
        else if(vis[x][j] == 2)//遇到炮塔说明不行
            return false;
    }
    for(int x = i + 1; x < n; x++) {
        if(vis[x][j] == 1)
            break;
        else if(vis[x][j] == 2)
            return false;
    }
    for(int y = j - 1; y >= 0; y--) {
        if(vis[i][y] == 1)
            break;
        else if(vis[i][y] == 2)
            return false;
    }
    for(int y = j + 1; y < n; y++) {
        if(vis[i][y] == 1)
            break;
        else if(vis[i][y] == 2)
            return false;
    }
    return true;//四个方向都没问题 就true
}

int dfs(int deep) {
    for(int i = 0; i < n; i++) {//遍历
        for(int j = 0; j < n; j++) {
            if(check(i, j)) {//判断这个位置是否可以
                vis[i][j] = 2;//置为2  2为炮塔
                dfs(deep+1);//继续搜,表示放了一个了
                vis[i][j] = 0;//从这个点往后搜完后, 回溯到这个点,要把它消掉。不影响其他放置方法
            }
        }
    }
    if(deep > maxx)//取最大的,  注意用max()和 :?  都会出错
        maxx = deep;
    return maxx;
}

int main() {
    while(~scanf("%d", &n), n) {
        memset(vis, 0, sizeof(vis));//初始标记数组
        getchar();//吸回车
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                scanf("%c", &G[i][j]);
                if(G[i][j] == 'X') //如果是墙就标记为1
                    vis[i][j] = 1;
            }
            getchar();//吸回车
        }
        maxx = 0;
        printf("%d
", dfs(0));//dfs
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ACMerszl/p/9572990.html