现在的位置: 首页 > 综合 > 正文

poj 2226 二分图最小点集覆盖

2013年08月20日 ⁄ 综合 ⁄ 共 2289字 ⁄ 字号 评论关闭

           这道题一直在纠结,,整整一天了,,,,,,,刚开始一直不知道如何建图,,纠结了好久,,,看了讨论区,,终于明白了如何建图,,接下来就是wr了,,wr了好久,,,,最后发现是一个小bug,,气的要死,,,,,,,一道不错的题,,题目:

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5134   Accepted: 1901

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

ac代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
char map[55][55];
int flag[1002],visted[1002];
vector<int> ss[1002];
int aa[1002][1002],bb[1002][1002];
int r,c;
void chushihua(){
	for(int icnt=1,i=1;i<=r;++i){
		for(int j=1;j<=c;++j){
		  if(map[i][j]=='*')
		  {aa[i][j]=icnt;}
		  if(map[i][j]=='*'&&map[i][j+1]!='*')
			  icnt++;
		}
	}
	for(int jcnt=1,i=1;i<=c;++i){
		for(int j=1;j<=r;++j){
		  if(map[j][i]=='*')
		  { bb[j][i]=jcnt;}
		  if(map[j][i]=='*'&&map[j+1][i]!='*')
			  jcnt++;
		}
	}
	for(int i=1;i<=r;++i){
		for(int j=1;j<=c;++j){
		  if(map[i][j]=='*')
		  {ss[aa[i][j]].push_back(bb[i][j]);}
		}
	}
}
bool dfs(int x){
	for(int i=0;i<ss[x].size();++i){
		if(!visted[ss[x][i]]){
		  visted[ss[x][i]]=1;
		  if(!flag[ss[x][i]]||dfs(flag[ss[x][i]])){
		    flag[ss[x][i]]=x;
			return true;
		  }
		}
	}
	return false;
}
int main(){
  //int r,c;
  while(~scanf("%d%d",&r,&c)){
    memset(map,'0',sizeof(map));
	memset(aa,0,sizeof(aa));
	memset(bb,0,sizeof(bb));
	memset(ss,0,sizeof(ss));
	for(int i=1;i<=r;++i){
	  for(int j=1;j<=c;++j)
		  cin>>map[i][j];
	}
	chushihua();
	memset(flag,0,sizeof(flag));
	int sum=0;
	for(int i=1;i<1002;++i){
	  memset(visted,0,sizeof(visted));
	  if(dfs(i))
		  sum++;
	}
	printf("%d\n",sum);
  }
  return 0;
}

抱歉!评论已关闭.