首页 技术 正文
技术 2022年11月14日
0 收藏 412 点赞 3,041 浏览 3042 个字

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

USACO 2005 January Gold 【题解】:  

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

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

对这个图分区域poj 2226 Muddy Fields (转化成二分图的最小覆盖) 把行区域的编号和列区域的编号写在两边(相当于二分图的两个点集),在有公共格子的区域内连线比如2号横区域和4号纵区域(分别在两个图中),第一行第三列的格子有重叠,所以两个连线。poj 2226 Muddy Fields (转化成二分图的最小覆盖)   这样就是在这个二分图中求最小顶点覆盖。   不知道读者有没有发现,二分图的边数=需要覆盖的格子数,而横一列和纵一列至多相交于一个格子,那么可以理解为,每一条边代表一个格子,然后就是覆盖每一条边,这就成了一个最小顶点覆盖问题。和之前那道题目是类似的。而之前那个题目没有障碍物,显得比较简单,一行一列就是一个区。   这个理解是把模型建立了以后才这么想,可不知道第一个这么做的人是谁呢?真有才,如果要单纯自己想,而且没有做过类似题目,还真的很难。至于具体还真的感到很玄。【code】:

 /**
Judge Status:Accepted Memory:36596K
Time:329MS Language:G++
Code Lenght:2165B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm> #define N 55
#define M N*N
using namespace std; int n,ny,nx,m;
int map[M][M];
int cx[M],cy[M],mark[M]; char str[N][N]; int map_x[N][N],map_y[N][N]; int path(int u)
{
int j;
for(j=;j<=ny;j++)
{
if(map[u][j]&&!mark[j])
{
mark[j]=;
if(cy[j]==-||path(cy[j]))
{
cx[u] = j;
cy[j] = u;
return ;
}
}
}
return ;
} int maxMatch() //求最小覆盖
{
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
int i;
int res = ;
for(i=;i<=nx;i++)
{
if(cx[i]==-)
{
memset(mark,,sizeof(mark));
res+=path(i);
}
}
return res;
} void getMap()
{
memset(map,,sizeof(map));
memset(map_x,,sizeof(map_x));
memset(map_y,,sizeof(map_y));
int i,j;
int flag;
nx=ny=;
for(i=;i<n;i++) //横向分区域
{
for(j=;j<m;j++)
{
flag = ;
while(j<m&&str[i][j]=='*')
{
if(!flag) ++nx;
map_x[i][j] = nx;
j++;
flag = ;
}
}
} for(j=;j<m;j++) //纵向分区
{
for(i=;i<n;i++)
{
flag = ;
while(i<n&&str[i][j]=='*')
{
if(!flag) ++ny;
flag = ;
map_y[i][j] = ny;
i++;
}
}
} for(i=;i<n;i++) //横向纵向区域连接
{
for(j=;j<m;j++)
{
int x = map_x[i][j];
int y = map_y[i][j];
if(x&&y)
{
map[x][y] = ; //得到二分map图,然后进行最小覆盖
}
}
}
} int main()
{
int i;
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
{
scanf("%s",str[i]);
}
getMap();
printf("%d\n",maxMatch());
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,945
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,471
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,284
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,100
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,732
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,767