• poj 2226 Muddy Fields (转化成二分图的最小覆盖)


    http://poj.org/problem?id=2226

    Muddy Fields
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7078   Accepted: 2622

    Description

    Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat. 
    To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field. 
    Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other. 
    Compute the minimum number of boards FJ requires to cover all the mud in the field.

    Input

    * Line 1: Two space-separated integers: R and C 
    * Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

    Output

    * Line 1: A single integer representing the number of boards FJ needs.

    Sample Input

    4 4
    *.*.
    .***
    ***.
    ..*.
    

    Sample Output

    4
    

    Hint

    OUTPUT DETAILS: 
    Boards 1, 2, 3 and 4 are placed as follows:  1.2.  .333  444.  ..2.  Board 2 overlaps boards 3 and 4.

    Source

     
    【题解】:
      

    题意:给定一个矩阵,其中有一些地方有水,用一些长度任意,宽度为1的木板盖住这些有水的地方,问至少需要几块板子。

    分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼(在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。

    对这个图分区域
    poj 2226 抽象模型建立,此篇将对此详细说明 - Hydrogenelf - Always-HELLO WORLD
     把行区域的编号和列区域的编号写在两边(相当于二分图的两个点集),在有公共格子的区域内连线
    比如2号横区域和4号纵区域(分别在两个图中),第一行第三列的格子有重叠,所以两个连线。
    poj 2226 抽象模型建立,此篇将对此详细说明 - Hydrogenelf - Always-HELLO WORLD
       这样就是在这个二分图中求最小顶点覆盖。
       不知道读者有没有发现,二分图的边数=需要覆盖的格子数,而横一列和纵一列至多相交于一个格子,那么可以理解为,每一条边代表一个格子,然后就是覆盖每一条边,这就成了一个最小顶点覆盖问题。和之前那道题目是类似的。而之前那个题目没有障碍物,显得比较简单,一行一列就是一个区。
       这个理解是把模型建立了以后才这么想,可不知道第一个这么做的人是谁呢?真有才,如果要单纯自己想,而且没有做过类似题目,还真的很难。至于具体还真的感到很玄。
    【code】:
      1 /**
      2 Judge Status:Accepted    Memory:36596K
      3 Time:329MS        Language:G++
      4 Code Lenght:2165B   Author:cj
      5 */
      6 
      7 #include<iostream>
      8 #include<stdio.h>
      9 #include<string.h>
     10 #include<math.h>
     11 #include<algorithm>
     12 
     13 #define N 55
     14 #define M N*N
     15 using namespace std;
     16 
     17 int n,ny,nx,m;
     18 int map[M][M];
     19 int cx[M],cy[M],mark[M];
     20 
     21 char str[N][N];
     22 
     23 int map_x[N][N],map_y[N][N];
     24 
     25 int path(int u)
     26 {
     27     int j;
     28     for(j=1;j<=ny;j++)
     29     {
     30         if(map[u][j]&&!mark[j])
     31         {
     32             mark[j]=1;
     33             if(cy[j]==-1||path(cy[j]))
     34             {
     35                 cx[u] = j;
     36                 cy[j] = u;
     37                 return 1;
     38             }
     39         }
     40     }
     41     return 0;
     42 }
     43 
     44 int maxMatch()  //求最小覆盖
     45 {
     46     memset(cx,-1,sizeof(cx));
     47     memset(cy,-1,sizeof(cy));
     48     int i;
     49     int res = 0;
     50     for(i=1;i<=nx;i++)
     51     {
     52         if(cx[i]==-1)
     53         {
     54             memset(mark,0,sizeof(mark));
     55             res+=path(i);
     56         }
     57     }
     58     return res;
     59 }
     60 
     61 void getMap()
     62 {
     63     memset(map,0,sizeof(map));
     64     memset(map_x,0,sizeof(map_x));
     65     memset(map_y,0,sizeof(map_y));
     66     int i,j;
     67     int flag;
     68     nx=ny=0;
     69     for(i=0;i<n;i++)  //横向分区域
     70     {
     71         for(j=0;j<m;j++)
     72         {
     73             flag = 0;
     74             while(j<m&&str[i][j]=='*')
     75             {
     76                 if(!flag)   ++nx;
     77                 map_x[i][j] = nx;
     78                 j++;
     79                 flag = 1;
     80             }
     81         }
     82     }
     83 
     84     for(j=0;j<m;j++)  //纵向分区
     85     {
     86         for(i=0;i<n;i++)
     87         {
     88             flag = 0;
     89             while(i<n&&str[i][j]=='*')
     90             {
     91                 if(!flag)   ++ny;
     92                 flag = 1;
     93                 map_y[i][j] = ny;
     94                 i++;
     95             }
     96         }
     97     }
     98 
     99     for(i=0;i<n;i++)  //横向纵向区域连接
    100     {
    101         for(j=0;j<m;j++)
    102         {
    103             int x = map_x[i][j];
    104             int y = map_y[i][j];
    105             if(x&&y)
    106             {
    107                 map[x][y] = 1;  //得到二分map图,然后进行最小覆盖
    108             }
    109         }
    110     }
    111 }
    112 
    113 
    114 int main()
    115 {
    116     int i;
    117     scanf("%d%d",&n,&m);
    118     for(i=0;i<n;i++)
    119     {
    120         scanf("%s",str[i]);
    121     }
    122     getMap();
    123     printf("%d
    ",maxMatch());
    124     return 0;
    125 }
  • 相关阅读:
    websphere、weblogic上JConsole的配置
    JMX连接WebSphere和WebLogic
    通过JMX监控管理weblogic的运行
    Manage, Administrate and Monitor GlassFish v3 from Java code usingAMX & JMX
    Harbor 简要安装说明
    VCSA 添加新管理用户的方法
    Oracle 修改参数
    CentOS8 设置开机自动登录账户的方法
    MYSQL使用mydumper备份恢复操作简介
    浅析网站上设置的广告位不显示问题:如何避免Adblock广告拦截
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3268143.html
Copyright © 2020-2023  润新知