zoukankan      html  css  js  c++  java
  • HDU1045 Fire Net

    题目链接

    Fire Net

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 14938    Accepted Submission(s): 9029


     

    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

    比较常规的dfs题,可以从左上角开始确定建不建碉堡,到右下角时统计可以建造碉堡的最大数目并更新。因此在确定某处建造碉堡是否合法是只要看同行的左边和同列的上边的情况就行。

    AC代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<string>
     4 #include<iomanip>
     5 #include<vector>
     6 #include<stack>
     7 using namespace std;
     8 char map[5][5];
     9 bool islegal(int r,int c)// 碉堡建在地图上的第r行第c列是否合法 
    10 {
    11     if(map[r][c]=='X') return 0;//墙壁处不合法 
    12     for(int j=c-1;j>=1;j--)//该行的左边 
    13     {
    14         if(map[r][j]=='h') return 0;//两碉堡间没有墙,不合法 
    15         else if(map[r][j]=='X') break;//有墙,该行没有冲突 
    16     }
    17     for(int i=r-1;i>=1;i--)//该列的上边 
    18     {
    19         if(map[i][c]=='h') return 0;//两碉堡间没有墙,不合法
    20         else if(map[i][c]=='X') break;//有墙,该列没有冲突 
    21     }
    22     return 1;//行和列都没冲突,合法 
    23 } 
    24 int n,max_n=0,total=0;//max_n为所有方案最大数目,total为当前方案最大数目 
    25 void dfs(int r,int c)//dfs
    26 {
    27     if(r==n&&c==n) 
    28     {
    29         if(islegal(n,n)) max_n=max(max_n,total+1);
    30         else max_n=max(max_n,total);
    31             return ;//遍历完全
    32     } 
    33     
    34     if(!islegal(r,c)) ;//不合法时不能建碉堡
    35     {  
    36         if(c==n) dfs(r+1,1);
    37             else dfs(r,c+1);
    38     }
    39         
    40     if(islegal(r,c)) //合法的时候可建可不建 
    41     {   //
    42         map[r][c]='h';total++;
    43         if(c==n) dfs(r+1,1);
    44         else dfs(r,c+1);
    45         map[r][c]='.';total--;//回溯 
    46         //不建 
    47         if(c==n) dfs(r+1,1);
    48         else dfs(r,c+1);
    49     }
    50 }
    51 int main()
    52 {
    53     while(cin>>n,n>0)
    54     {   
    55         for(int i=1;i<=n;i++)
    56         cin>>map[i]+1;//从1开始计数要加1 
    57         dfs(1,1);//从左上角开始确定 
    58         cout<<max_n<<endl;
    59         total=0;max_n=0;
    60     }
    61 }
    62  
    View Code
  • 相关阅读:
    springboot之静态资源路径配置
    window使用结束进程
    MyBatis动态sql语句归纳
    Mybatis——实体类属性名和数据库字段名不同时的解决方案
    oracle 查看被锁表 及解除锁定
    Eclipse设置软tab(用4个空格字符代替)及默认utf-8文件编码(unix)
    navicat和 plsql 连接oracle数据库 总结
    eclipse 快捷键Open Implementation 直接退出
    linux centos 安装mysql
    拦截器 过滤器 监听器 的区别
  • 原文地址:https://www.cnblogs.com/wangzhebufangqi/p/12796195.html
Copyright © 2011-2022 走看看