zoukankan      html  css  js  c++  java
  • 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

    解题思路:
    1.设可以放cou个碉堡,最多可以放置max个碉堡
    2.放置碉堡的策略是在地图上从左至右,从上至下扫描(从左上角开始扫描)
    3.如果找到下一个碉堡可以放置的位置,此时cou加一,在地图上做上标记,并且调到步骤2
    4.如果没有找到(扫描到右下角),清除标记,比较max是否大于cou,若不是,将cou赋给max,然后跳到步骤3(从上一个标记之后开始扫描)
    5.最后输入max

    代码:
      1 #include <iostream>
      2 #include <string.h>
      3 using namespace std;
      4 
      5 //定义全局变量
      6 int n;    //    地图的规模n*n
      7 int cou;    // 最多可以放的碉堡数
      8 int map[5][5];    //地图中0代表可以放置,1代表放置过了,2代表是墙
      9 
     10 //函数声明
     11 void dfs(int depth);
     12 bool find(int row, int cloumn);
     13 
     14 int main(){
     15     char ch;
     16     while ((cin >> n) && n){
     17         cou = 0;
     18         memset(map, 0, sizeof(map));        //初始化地图
     19         
     20         //读取地图
     21         for (int i = 0; i < n; i++){
     22             for (int j = 0; j < n; j++){
     23                 cin >> ch;
     24                 if (ch == 'X'){
     25                     map[i][j] = 2;
     26                 }
     27             }
     28         }
     29         dfs(0);        //开始搜索
     30         cout << cou << endl;
     31         
     32     }
     33 }
     34 
     35 void dfs(int depth){
     36     //depth表示当前搜索的深度及表示当前已经安置的碉堡个数
     37     //深度搜索,实现找到安放位置
     38     for (int i = 0; i < n; i++){
     39         for (int j = 0; j < n; j++){
     40             if (find(i, j)){
     41                 map[i][j]++;
     42                 
     43                 dfs(depth + 1);
     44                 map[i][j]--;
     45             }
     46         }
     47     }
     48     cou = (cou > depth) ? cou : depth;
     49 }
     50 
     51 bool find(int row, int cloumn){
     52     //检测当前位置是否可以放置碉堡
     53     if (map[row][cloumn] != 0){
     54         return false;
     55     }
     56     if (row > 0){
     57         //向上检查
     58         for (int i = row - 1; i >= 0; i--){
     59             if (map[i][cloumn] == 2){
     60                 break;
     61             }
     62             else if (map[i][cloumn] == 1){
     63                 return false;
     64             }
     65         }
     66     }
     67     //向下检查
     68     if (row < n - 1){
     69         for (int i = row + 1; i < n; i++){
     70             if (map[i][cloumn] == 2){
     71                 break;
     72             }
     73             else if (map[i][cloumn] == 1){
     74                 return false;
     75             }
     76         }
     77     }
     78     
     79 
     80     if (cloumn > 0){
     81         //向左检查
     82         for (int j = cloumn - 1; j >= 0; j--){
     83             if (map[row][j] == 2){
     84                 break;
     85             }
     86             else if (map[row][j] == 1){
     87                 return false;
     88             }
     89         }
     90     }
     91 
     92     if (cloumn < n - 1){
     93         //向右检查
     94         for (int j = cloumn + 1; j < n; j++){
     95             if (map[row][j] == 2){
     96                 break;
     97             }
     98             else if (map[row][j] == 1){
     99                 return false;
    100             }
    101         }
    102     }
    103     return true;
    104 }
    View Code
  • 相关阅读:
    布局神器display:table-cell
    解决IE兼容总汇【转】
    jQuery Validate【强大的表单验证】
    使用信号SIGALRM 为 recvfrom 设置超时,防止死等待
    并发服务器(IO多路复用)
    并发服务器(多进程版本)
    TCP listen()函数内幕
    c++ 重载运算符规则
    内核定时器struct timer_list
    C和arm汇编的相互调用(看书笔记)
  • 原文地址:https://www.cnblogs.com/wmllz/p/4858613.html
Copyright © 2011-2022 走看看