zoukankan      html  css  js  c++  java
  • Fire Net(dfs)

    Fire Net

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


    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
     题解:
    二维数组的坐标用一个数字表示,这个点从0到n*n每次选或不选,加一个判断;
    代码:
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAX(x,y) x>y?x:y
     4 const int INF=-0xfffffff;
     5 char map[5][5];
     6 int n,maxnum;
     7 bool judge(int t){
     8     int w,h,p,q;
     9     h=t/n;w=t%n;
    10     p=h;q=w;
    11     while(h>=0){
    12         if(map[h][w]=='@')return false;
    13         if(map[h][w]=='X')break;
    14         h--;
    15     }
    16     w=q;h=p;
    17     while(w>=0){
    18         if(map[h][w]=='@')return false;
    19         if(map[h][w]=='X')break;
    20         w--;
    21     }
    22     w=q;h=p;
    23     while(w<n){
    24         if(map[h][w]=='@')return false;
    25         if(map[h][w]=='X')break;
    26         w++;
    27     }
    28     w=q;h=p;
    29     while(h<n){
    30         if(map[h][w]=='@')return false;
    31         if(map[h][w]=='X')break;
    32         h++;
    33     }
    34     return true;
    35 }
    36 void dfs(int s,int num){int x,y;
    37     if(s==n*n){
    38         maxnum=MAX(maxnum,num);
    39         return;
    40     }
    41     else{x=s/n;y=s%n;
    42         if(map[x][y]=='.'&&judge(s)){
    43             map[x][y]='@';
    44             dfs(s+1,num+1);
    45             map[x][y]='.';
    46         }
    47         dfs(s+1,num);
    48     }
    49 }
    50 int main(){
    51     while(scanf("%d",&n),n){
    52         maxnum=INF;
    53         for(int i=0;i<n;i++)scanf("%s",map[i]);
    54         dfs(0,0);
    55         printf("%d
    ",maxnum);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    @slf4j 使用方法
    spark入门简单介绍
    spring boot 的简单实用和spring cloud 概念
    nginx与Tomcat
    python27+百度文字识别api
    python27+opencv2.4.10+pytesseract0.2.0图片识别
    学习vue的核心功能
    使用vscode +vue的初始环境搭建
    excel的vlookup,第一次用
    pyautogui键盘鼠标控制,python27
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4709033.html
Copyright © 2011-2022 走看看