hdu 5093 Battle ships 二分图匹配

Battle ships

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


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
 

刚开始想偏了,想了很久才想到。。

/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/2 12:39:24
 |File Name: d.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
char ma[100][100];
int hang[100][100];
int lie[100][100];
#define len 2505
int l,r,u,d;
int n,m;
int nn,mm;
int map[len][len];
int used[len];
int lin[len];
int dfs(int a)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(!used[i]&&map[a][i])
        {
            used[i]=1;
            if(lin[i]==-1||dfs(lin[i]))
            {
                lin[i]=a;
                return 1;
            }
        }
    }
    return 0;
}

int main() {
	//freopen("input.txt","r",stdin); 
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d %d",&nn,&mm);
        rep(i,nn) scanf("%s",ma[i]);
        int ans = 0;
        memset(hang,0,sizeof(hang));
        memset(lie,0,sizeof(lie));
        int id = 1;
        int t1,t2;
        rep(i,nn) {
            rep(j,mm) {
                if(ma[i][j] != '#') hang[i][j] = id;
                else id ++;
            }
            id ++;
        }
        t1 = id;
        id = 1;
        
        rep(i,mm) {
            rep(j,nn) {
                if(ma[j][i] != '#') lie[j][i] = id;
                else id ++;
            }
            id ++;
        }
        t2 = id;
        n = max(t1,t2);
        memset(map,0,sizeof(map));
        memset(lin,-1,sizeof(used));
        rep(i,nn) {
            rep(j,mm) {
                if(ma[i][j] == '*') {
                    map[hang[i][j]][lie[i][j]] = 1;
                }
            }
        }
        
        int sum = 0;
        for(int i=1;i<=n;++i) {
            memset(used,0,sizeof(used));
            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、付费专栏及课程。

余额充值