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
  • 相关阅读:
    隐藏 MOSS 2007 页面版本工具栏
    用于显示原始XML形式的搜索结果的XSLT
    MOSS 2007 日志设置
    在布局页面“文章页面中”添加,自定义UserControl
    MOSS 2007 最简单的自定义搜索框 SearchBox
    Asp.net常用状态管理方案分析
    提高asp.net的性能的几种方法(转)
    VS2005下如何用预编译命令来发布站点
    asp.net控件设计时支持(1)
    解决Enterprise Library January 2006不能加密配置文件的方法(转)
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3222055.html
Copyright © 2011-2022 走看看