zoukankan      html  css  js  c++  java
  • HDU1045--Fire Net

    Fire Net
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4849 Accepted Submission(s): 2725


    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

    思想跟把矩阵中的1消去那题思想一样,如果某行中间隔了一块障碍,那么后半段就是另起一行,与障碍前的一段无关。

    最小点覆盖

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<cmath>
      6 using namespace std;
      7 
      8 int Link[1001][1001];
      9 int cy[1001];
     10 int mk[1001];
     11 char str[1001][1001];
     12 int n;
     13 int xs[1001][1001];
     14 int ys[1001][1001];
     15 int x,y;
     16 
     17 void init()
     18 {
     19     memset(Link,0,sizeof(Link));
     20     memset(cy,0xff,sizeof(cy));
     21     memset(mk,0,sizeof(mk));
     22     memset(str,0,sizeof(str));
     23     memset(xs,0,sizeof(xs));
     24     memset(ys,0,sizeof(ys));
     25 }
     26 
     27 int path(int u)
     28 {
     29     int v;
     30     for(v=1;v<=y;v++)
     31     {
     32         if(Link[u][v]&&!mk[v])
     33         {
     34             mk[v]=1;
     35             if(cy[v]==-1||path(cy[v]))
     36             {
     37                 cy[v]=u;
     38                 return 1;
     39             }
     40         }
     41     }
     42     return 0;
     43 }
     44 
     45 int maxmatch()
     46 {
     47     int sum=0;
     48     int i;
     49     for(i=1;i<=x;i++)
     50     {
     51         memset(mk,0,sizeof(mk));
     52         sum+=path(i);
     53     }
     54     return sum;
     55 }
     56 
     57 int main()
     58 {
     59     while(scanf("%d",&n)!=EOF)
     60     {
     61         if(!n)break;
     62         init();
     63         int i,j;
     64         for(i=0;i<n;i++)
     65         {
     66             scanf("%s",str[i]);
     67         }
     68         int number=0;
     69         for(i=0;i<n;i++)
     70         {
     71             int flag=0;
     72             for(j=0;j<n;j++)
     73             {
     74                 if(str[i][j]=='.'){
     75                     if(flag==0)number++;
     76                     xs[i][j]=number;
     77                     flag=1;
     78                 }
     79                 else {
     80                     flag=0;
     81                 }
     82             //    printf("%d ",xs[i][j]);
     83             }
     84         //    printf("
    ");
     85         }
     86         x=number;
     87         number=0;
     88         for(i=0;i<n;i++)
     89         {
     90             int flag=0;
     91             for(j=0;j<n;j++)
     92             {
     93                 if(str[j][i]=='.'){
     94                     if(flag==0)number++;
     95                     ys[j][i]=number;
     96                     flag=1;
     97                 }
     98                 else {
     99                     flag=0;
    100                 }
    101             }
    102         }
    103         y=number;
    104     /*    for(i=0;i<n;i++)
    105         {
    106             for(j=0;j<n;j++)
    107             {
    108                 printf("%d ",ys[i][j]);
    109             }
    110             printf("
    ");
    111         }*/
    112         for(i=0;i<n;i++)
    113         {
    114             for(j=0;j<n;j++)
    115             {
    116                 if(xs[i][j])
    117                 {
    118                     Link[xs[i][j]][ys[i][j]]=1;
    119                 }
    120             }
    121         }
    122         int ans=maxmatch();
    123         printf("%d
    ",ans);
    124     }
    125     return 0;
    126 }
    View Code
  • 相关阅读:
    Unable to load the specified metadata resource
    Web开发人员速查卡
    vs 中大括号之间垂直虚线显示
    第4届华为编程大赛决赛试题解答(棋盘覆盖)
    assert()函数用法总结
    Win7安装VC++6.0已知的兼容性问题的解决方法
    VC6打开一个文件或工程的时候,会导致VC6崩溃而关闭
    浮点数取整.
    1.4 VC6.0在win7下安装的兼容性问题以及解决办法
    华为编程大赛_将字符数组内的数字排序
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3222055.html
Copyright © 2011-2022 走看看