zoukankan      html  css  js  c++  java
  • [HDOJ1045]Fire Net

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045

    Fire Net

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


    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,今天水一个。。。并不是很顺利,因为卡在了fflush(stdin)上…应该用getchar()的。。完全不知道这是为什么……
     
     
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 
    13 using namespace std;
    14 
    15 const int maxn = 10;
    16 int n;
    17 char G[maxn][maxn];
    18 int ans;
    19 int dd[4][2] = {1,0,0,1,-1,0,0,-1};
    20 
    21 int ok(int x, int y) {
    22     int dx, dy;
    23     if(G[x][y] != '.') {
    24         return 0;
    25     }
    26     for(int i = 0; i < 4; i++) {
    27         dx = x + dd[i][0];
    28         dy = y + dd[i][1];
    29         while(1) {
    30             if(dx < 0 || dx >= n || dy < 0 || dy >= n || G[dx][dy] == 'X' || G[dx][dy] == 'x') {
    31                 break;
    32             }
    33             else if(G[dx][dy] == 'B') {
    34                 return 0;
    35             }
    36             dx += dd[i][0];
    37             dy += dd[i][1];
    38         }
    39     }
    40     return 1;
    41 }
    42 
    43 void dfs(int dep) {
    44     if(dep > ans) {
    45         ans = dep;
    46     }
    47     for(int i = 0; i < n; i++) {
    48         for(int j = 0; j < n; j++) {
    49             if(ok(i, j)) {
    50                 G[i][j] = 'B';
    51                 dfs(dep+1);
    52                 G[i][j] = '.';
    53             }
    54         }
    55     }
    56 }
    57 
    58 int main() {
    59     while(~scanf("%d", &n) && n) {
    60         getchar();
    61         ans = 0;
    62         for(int i = 0; i < n; i++) {
    63             gets(G[i]);
    64         }
    65         dfs(0);
    66         printf("%d
    ", ans);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    char/byte/short类型的加法和类型转换问题
    Java四种基本数据类型
    Git知识集锦
    解决给自己的博客添加百度统计不能验证的问题
    C++静态代码分析工具推荐——PVS-Studio
    Qt在控件未显示时如何获取正确的控件尺寸
    C#程序如何捕捉未try/catch的异常——不弹“XXX已停止工作”报错框
    win10下vs2015编译的程序如何运行在win7等系统(无需安装Redistributable)
    Qt分页导航控件
    win server 2008配置ftp无法登陆问题的解决办法
  • 原文地址:https://www.cnblogs.com/kirai/p/4753459.html
Copyright © 2011-2022 走看看