• Fire Net (check ➕ dfs)


    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
     

    题意:

    给出n * n 规模的地图,其中.代表空白区域,X代表墙,求出在满足以下规则的情况下,最多能建立多少座炮楼。 
    规则: 
    1.假定炮楼可以四向发射炮弹,要求2个炮楼不能互相打到。(射程无限制) 
    2.墙可以拦截住炮弹。

     
    思路:

    不难从地图中看出,每一个空白的格子均有可能建炮楼(题目中也说了最大个数一定,但位置有可能有多解)。所以可以确定要遍历整张地图,即判断每个格子是否满足建炮楼的条件,会用到双重for循环遍历整张地图。

    继续看如何实现。按照规则,水平/竖直不能有其他的炮楼出现,不难想到,用for循环分别对上下左右四个方向检查,如果遇到炮楼,则说明这个位置不符合规则,返回false,或者是超越了地图边界,退出循环,再或者是遇到了墙 退出当前循环。 为什么遇到墙就跳出循环了呢? 也不难想到,就算墙后面是炮楼,也是符合规则的,所以干脆遇到墙就跳出循环。

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <set>
     7 #include <queue>
     8 #include <math.h>
     9 #include <stdbool.h>
    10 
    11 #define LL long long
    12 #define inf 0x3f3f3f3f
    13 using namespace std;
    14 const int MAXN=1000005;
    15 
    16 char map[5][5];
    17 int vis[5][5];
    18 int n;
    19 int ans;
    20 
    21 bool check(int x,int y){
    22     if(x<0 || x>=n || y<0 || y>=n) return false;
    23     else return true;
    24 }
    25 
    26 bool recheck(int x,int y){
    27     if (map[x][y] == 'X')
    28         return false;
    29     for (int i=x;check(i,y);i++){
    30         if (map[i][y] == 'X')
    31             break;
    32         if (vis[i][y] == 1)
    33             return false;
    34     }
    35     for (int i=x;check(i,y);i--){
    36         if (map[i][y] == 'X')
    37             break;
    38         if (vis[i][y] == 1)
    39             return false;
    40     }
    41     for (int i=y;check(x,i);i++){
    42         if (map[x][i] == 'X')
    43             break;
    44         if (vis[x][i] == 1)
    45             return false;
    46     }
    47     for (int i=y;check(x,i);i--){
    48         if (map[x][i] == 'X')
    49             break;
    50         if (vis[x][i] == 1)
    51             return false;
    52     }
    53     return true;
    54 }
    55 
    56 void dfs(int num){
    57     if (num>ans)
    58         ans = num;
    59     for (int i=0;i<n;i++){
    60         for (int j=0;j<n;j++){
    61             if (recheck(i,j)){
    62                 vis[i][j] = 1;
    63                 dfs(num+1);
    64                 vis[i][j] = 0;
    65             }
    66         }
    67     }
    68 }
    69 
    70 int main() {
    71     //freopen("../in.txt", "r", stdin);
    72     while (~scanf("%d", &n)){
    73         if (n==0)
    74             break;
    75         ans = 0;
    76         memset(vis,0, sizeof(vis));
    77         for (int i=0;i<n;i++){
    78             scanf("%s",&map[i]);
    79         }
    80         dfs(0);
    81         printf("%d
    ",ans);
    82     }
    83 }
     
  • 相关阅读:
    专门针对初学者的Node.js教程
    windows版的node.js简单示例
    bzoj 1003: [ZJOI2006]物流运输【spfa+dp】
    bzoj 3573: [Hnoi2014]米特运输【树形dp+瞎搞】
    bzoj 1082: [SCOI2005]栅栏【二分+dfs】
    bzoj 2440: [中山市选2011]完全平方数【莫比乌斯函数+二分】
    bzoj 1049: [HAOI2006]数字序列【dp+二分+瞎搞】
    bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】
    bzoj 4551: [Tjoi2016&Heoi2016]树【并查集】
    bzoj 4310: 跳蚤【后缀数组+st表+二分+贪心】
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11221591.html
Copyright © 2020-2023  润新知