zoukankan      html  css  js  c++  java
  • Fire Net(hdu 1045)

    题目描述:

    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..
    ....
    2
    XX
    .X
    3
    .X.
    X.X
    .X.
    3
    ...
    .XX
    .XX
    4
    ....
    ....
    ....
    ....
    0
     
    Sample Output
    5
    1
    5
    2
    4

     代码如下:

     1 #include<iostream>
     2 using namespace std;
     3 int m;
     4 char map[4][4];
     5 int MaxCount;
     6 bool CanSet(int x,int y);
     7 void dfs(int n,int count);
     8 int main()
     9 {
    10     //int m;
    11     while((cin >> m) && m)
    12     {
    13         for(int i = 0;i < m;i++)
    14             for(int j = 0;j < m;j++)
    15                 cin >> map[i][j];
    16         MaxCount = 0;
    17         dfs(0,0);
    18         cout << MaxCount << endl;
    19     }
    20     return 0;
    21 }
    22 
    23 bool CanSet(int x,int y)//坐标x,y是否可以放置碉堡
    24 {
    25     for(int i = x - 1;i >= 0;i--)
    26     {
    27         if(map[i][y] == '0') return false;
    28         if(map[i][y] == 'X') break;
    29     }
    30     for(int i = y - 1;i >= 0;i--)
    31     {
    32         if(map[x][i] == '0') return false;
    33         if(map[x][i] == 'X') break;
    34     }
    35     return true;
    36 }
    37 
    38 void dfs(int n,int count)
    39 {
    40     //if(count > MaxCount)
    41        // MaxCount = count;
    42     if(n < m * m)
    43     {
    44     int x = n / m;
    45     int y = n % m;
    46     if(CanSet(x,y) && map[x][y] == '.')
    47     {
    48         map[x][y] = '0';
    49         dfs(n+1,count + 1);
    50         map[x][y] = '.';//回溯,考虑在可以放置碉堡的前提下,放置和不放置两种情况。
    51     }
    52     dfs(n+1,count);
    53     }
    54     else //到达最后一个坐标,保存最大值
    55     {
    56         if(count > MaxCount)
    57             MaxCount = count;
    58         return;
    59     }
    60     //return;
    61 }

    代码分析:

    深度优先搜索法+回溯法

    参考博客:http://www.cnblogs.com/phinecos/archive/2008/09/18/1293017.html

  • 相关阅读:
    Zookeeper系列二:分布式架构详解、分布式技术详解、分布式事务
    Zookeeper系列一:Zookeeper介绍、Zookeeper安装配置、ZK Shell的使用
    Mysql系列九:使用zookeeper管理远程Mycat配置文件、Mycat监控、Mycat数据迁移(扩容)
    Mysql系列八:Mycat和Sharding-jdbc的区别、Mycat分片join、Mycat分页中的坑、Mycat注解、Catlet使用
    Mysql系列七:分库分表技术难题之分布式全局唯一id解决方案
    Mysql系列五:数据库分库分表中间件mycat的安装和mycat配置详解
    学习Mysql过程中拓展的其他技术栈:Docker入门介绍
    学习Mysql过程中拓展的其他技术栈:设置linux虚拟机的固定ip和克隆linux虚拟机
    Mysql系列四:数据库分库分表基础理论
    Mysql系列三:Centos6下安装Mysql和Mysql主从复制的搭建
  • 原文地址:https://www.cnblogs.com/linxiaotao/p/3442184.html
Copyright © 2011-2022 走看看