zoukankan      html  css  js  c++  java
  • zoj 1002 Fire Net (DFS搜索)

    1002  Fire Net

    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
    

    解题代码: 

    View Code
     1 // File Name: Fire Net 1002.cpp
     2 // Author: sheng
     3 // Created Time: 2013年04月22日 星期一 17时07分28秒
     4 
     5 #include <stdio.h>
     6 #include <iostream>
     7 #include <string.h>
     8 using namespace std;
     9 
    10 const int maxn = 5;
    11 int m_ans, n;
    12 char map[maxn][maxn];
    13 
    14 bool Build (int row, int col)
    15 {
    16     int i;
    17     for (i = row; i >= 0; i --)//检查之前的状态
    18     {
    19         if (map[i][col] == 'O') // 表明在这一列已经存在一个碉堡了 && 中间没有墙
    20             return false;
    21         if (map[i][col] == 'X') // 表明这一列中间有一堵墙,说明可以放个碉堡
    22             break;
    23     }
    24     for (i = col; i >= 0; i --)    //同上
    25     {
    26         if (map[row][i] == 'O')
    27             return false;
    28         if (map[row][i] == 'X')
    29             break;
    30     }
    31     return true;
    32 }
    33 
    34 void Dfs(int s, int ans)
    35 {
    36     int row, col;
    37     if (s == n * n)
    38     {
    39         if (ans > m_ans)
    40             m_ans = ans;
    41         return;
    42     }
    43     row = s/n;
    44     col = s%n;
    45     if (map[row][col] == '.' && Build (row, col))
    46     {
    47         map[row][col] = 'O';
    48         Dfs (s+1, ans + 1);
    49         map[row][col] = '.'; //使用完之后还原继续递归
    50     }
    51     Dfs (s+1, ans);
    52 }
    53 
    54 int main()
    55 {
    56     while (scanf ("%d", &n) && n)
    57     {
    58         m_ans = 0;
    59         for (int i = 0; i < n; i ++)
    60             scanf ("%s", map[i]);
    61         Dfs(0, 0);
    62         printf ("%d\n", m_ans);
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    二、静、动态代理(9~10)~~~~
    luogu P3572 [POI2014]PTA-Little Bird 单调队列优化dp
    luogu P6113 【模板】一般图最大匹配 带花树
    Codeforces Round #646 (Div. 2) C——Game On Leaves 思维
    Codeforces Round #646 (Div. 2) E——Tree Shuffling 思维
    luogu P2979 [USACO10JAN]Cheese Towers S 变形dp背包
    luogu P6577 【模板】二分图最大权完美匹配
    Educational Codeforces Round 88 (Rated for Div. 2) D
    Codeforces Round #645 (Div. 2) E
    Codeforces Round #645 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3036636.html
Copyright © 2011-2022 走看看