博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BNUOJ 2345 Muddy Fields
阅读量:4331 次
发布时间:2019-06-06

本文共 3240 字,大约阅读时间需要 10 分钟。

Muddy Fields

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on 
PKU. Original ID: 
64-bit integer IO format: %lld      Java class name: Main
 
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 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 char mp[60][60];16 int row,col;17 int r[60][60],c[60][60];18 vector
g[6000];19 int lik[6000],cnt;20 bool used[6000];21 void init(){22 int i,j;23 char pre = '-';24 cnt = 1;25 memset(r,0,sizeof(r));26 memset(c,0,sizeof(c));27 for(i = 0; i < row; i++){28 for(j = 0; j < col; ){29 if(mp[i][j] == '*'){30 while(j < col && mp[i][j] == '*') {r[i][j] = cnt;j++;}31 cnt++;32 }else j++;33 }34 }35 for(i = 0; i < col; i++){36 for(j = 0; j < row; ){37 if(mp[j][i] == '*'){38 while(j < row && mp[j][i] == '*'){c[j][i] = cnt;j++;}39 cnt++;40 }else j++;41 }42 }43 }44 bool dfs(int u){45 for(int i = 0; i < g[u].size(); i++){46 if(!used[g[u][i]]){47 used[g[u][i]] = true;48 if(lik[g[u][i]] == -1 || dfs(lik[g[u][i]])){49 lik[g[u][i]] = u;50 return true;51 }52 }53 }54 return false;55 }56 int main(){57 int i,j,ans;58 while(~scanf("%d%d",&row,&col)){59 for(i = 0; i < row; i++)60 scanf("%s",mp[i]);61 for(i = 0; i < 6000; i++){62 g[i].clear();63 lik[i] = -1;64 }65 init();66 for(i = 0; i < row; i++){67 for(j = 0; j < col; j++){68 if(mp[i][j] == '*'){69 g[r[i][j]].push_back(c[i][j]);70 g[c[i][j]].push_back(r[i][j]);71 }72 }73 }74 for(ans = 0,i = 1; i < cnt; i++){75 memset(used,false,sizeof(used));76 if(dfs(i)) ans++;77 }78 cout<<(ans>>1)<
View Code

 

 

转载于:https://www.cnblogs.com/crackpotisback/p/3888196.html

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>