zoukankan      html  css  js  c++  java
  • HDU1045 ZOJ1002 Fire Net【DFS】

    Fire Net

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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.

    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.

    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..
    ....
    2
    XX
    .X
    3
    .X.
    X.X
    .X.
    3
    ...
    .XX
    .XX
    4
    ....
    ....
    ....
    ....
    0
    

    Sample output:

    5
    1
    5
    2
    4
    

    Source: Zhejiang University Local Contest 2001


    问题链接HDU1045 ZOJ1002 Fire Net

    问题简述:(略)

    问题分析

    城市被分为若干个块,可以用矩阵来表示,每个块可能是墙(黑色方块,或"X"),可能是空地(黑色圈,或"."),空地上可以放炮台(blockhouses),但是不能互相攻击得到,即同一行或同一列,如果没有隔着墙就不能同时摆放炮台。

    虽然说这个题有点像八皇后问题,但是还是有所不同的。问题还是需要用DFS方法来解决。 

    程序中,对于访问过的块,没有使用额外的存储来存放,只是暂时置为"P",回溯时进行复原设置。

    程序说明:(略)


    AC通过的C语言程序如下:

    /* HDU1045 ZOJ1002 Fire Net */
    
    #include <stdio.h>
    
    #define MAX(a, b) (((a)>(b))?(a):(b))
    
    char grid[4][4];
    int maxbh;
    int n, count;
    
    int nextrow(int row, int col)
    {
        return ((col + 1) % n == 0) ? row + 1 : row;
    }
    
    int nextcol(int col)
    {
        return (col + 1) % n;
    }
    
    // 检查:检查grid[row][col]位置是否可以放置,可以返回1,不可以返回0
    int check(int row, int col)
    {
        int lastrow;
        int lastcol;
    
        // 检查行
        lastrow = row - 1;
        for(;;) {
            if(lastrow < 0)
                break;
    
            if(grid[lastrow][col] == 'X')
                break;
    
            if(grid[lastrow][col] == 'P')
                return 0;
    
            lastrow--;
        }
    
        // 检查列
        lastcol = col - 1;
        for(;;) {
            if(lastcol < 0)
                break;
    
            if(grid[row][lastcol] == 'X')
                break;
    
            if(grid[row][lastcol] == 'P')
                return 0;
    
            lastcol--;
        }
    
        return 1;
    }
    
    // DFS:试探row行,col列
    void dfs(int row, int col)
    {
        for(;;) {
            if(row >= n)
                break;
    
            if(grid[row][col] == '.') {
                if(check(row, col)) {
                    grid[row][col] = 'P';   // 放置时,暂时置为'P'
                    count++;                // 放置时,炮台数量+1
    
                    maxbh = MAX(maxbh, count);  // 计算炮台最大数量
    
                    dfs(nextrow(row,col), nextcol(col));
    
                    count--;                // 回溯时,复原
                    grid[row][col] = '.';   // 回溯时,复原
                }
            }
    
            // 计算下一个位置
            row = nextrow(row,col);
            col = nextcol(col);
        }
    }
    
    int main(void)
    {
        int i;
    
        while(scanf("%d", &n) != EOF) {
            getchar();
            // 判定结束条件
            if(n == 0)
                break;
    
            // 读入数据
            for(i=0; i<n; i++)
                gets(grid[i]);
    
            // 深度优先搜索
            maxbh = 0;
            count = 0;
            dfs(0, 0);
    
            // 输出结果
            printf("%d
    ", maxbh);
        }
    
        return 0;
    }


  • 相关阅读:
    解决WPF中异常导致的程序Crash
    Python---pep8规范
    Python---13面向对象编程
    Python---12函数式编程------12.3匿名函数&装饰器&偏函数
    Python---12函数式编程------12.2返回函数
    Python中*args和**kwargs的区别
    测试干货-接口测试(接口测试工具+接口测试用例)
    Python---13靠谱的Pycharm安装详细教程
    Python---12函数式编程------12.1高阶函数
    Python---11模块
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564615.html
Copyright © 2011-2022 走看看