HDU 5093 Battle ships(二分图匹配)

Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1238    Accepted Submission(s): 457


Problem Description
Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.

Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.

But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.

The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:

A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg

Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
 

Input
There is only one integer T (0<T<12) at the beginning line, which means following T test cases.

For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
 

Output
For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.
 

Sample Input
  
  
2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#
 

Sample Output
  
  
3 5
 

Source
 
题意:给出nxm地图,*代表可以放飞船,o不能放飞船,#代表障碍。同一行和同一列只能允许有一架飞船,但是如果中间有障碍割开即可放多架,给定地图问最多可以放多少飞船。
思路:当时没有思路,后来看了题解发现是二分图匹配的经典题型-行列匹配。即把所有横向相连和纵向相连的可以放飞船并且中间没有障碍的位置合并在一起,给定一个编号。之后对于每个格子,将其横向编号和纵向编号连边,求最大匹配数即是结果。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
char c[55][55];
int x[55][55];
int y[55][55];
int ma[2550][2550];
int book[2550];
int match[2550];
int cnt,ans;

int dfs(int u){
	for(int i=1;i<ans;i++){
		if(book[i]==0&&ma[u][i]==1){
			book[i]=1;
			if(match[i]==0||dfs(match[i])){
				match[i]=u;
				return 1;
			}
		}
	}
	return 0;
}

int main(){
	
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m;
		scanf("%d %d",&n,&m);
		for(int i=0;i<n;i++){
			scanf("%s",c[i]);
		}
		
		cnt=1;
		memset(x,0,sizeof(x));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(c[i][j]=='*'){
					while(j<m&&c[i][j]!='#'){
						if(c[i][j]=='*'){
							x[i][j]=cnt; 
						}
						j++;
					}
					cnt++;
				}
			}
			cnt++;
		}
		
		memset(y,0,sizeof(y));
		ans=1;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				if(c[j][i]=='*'){
					while(j<m&&c[j][i]!='#'){
						if(c[j][i]=='*'){
							y[j][i]=ans; 
						}
						j++;
					}
					ans++;
				}
			}
			ans++;
		}
		
		memset(ma,0,sizeof(ma));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(x[i][j]&&y[i][j]){
					ma[x[i][j]][y[i][j]]=1;
				}
			} 
		}
		
		memset(match,0,sizeof(match));
		int sum=0;
		for(int i=1;i<cnt;i++){
			for(int j=1;j<ans;j++)
				book[j]=0;
			if(dfs(i))
				sum++;
		}
		printf("%d\n",sum);
	}
	return 0;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值