hdu 棋盘游戏(最大匹配)

棋盘游戏

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。 
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么?

Input

输入包含多组数据, 
第一行有三个数N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盘的高、宽,以及可以放“车”的格子数目。接下来的K行描述了所有格子的信息:每行两个数X和Y,表示了这个格子在棋盘中的位置。

Output

对输入的每组数据,按照如下格式输出: 
Board T have C important blanks for L chessmen.

Sample Input

3 3 4
1 2
1 3
2 1
2 2
3 3 4
1 2
1 3
2 1
3 2

Sample Output

Board 1 have 0 important blanks for 2 chessmen.
Board 2 have 3 important blanks for 3 chessmen.

Author

Gardon

Source

杭电ACM集训队训练赛(VI)

该题有两处值得留意
A. 如何建图
B. 找重要点的方法是依次删除每个点,看所得最大匹配数是否减少

#include <stdio.h>
#include <string.h>

int n,m;
int match[105][105];
bool vist[105];
int link[105];

bool Crossroad(int x){
    for(int i = 1;i <= match[x][0];i++){
        int tem = match[x][i];
        if(tem && !vist[tem]){
            vist[tem] = 1;
            if(!link[tem] || Crossroad(link[tem])){//注意是link[tem],不是tem
                link[tem] = x;
                return 1;
            }
        }
    }
    return 0;
}

int Hugary(){
    int ans = 0;
    for(int i = 1;i <= n;i++){
        memset(vist,0,sizeof(vist));
        if(Crossroad(i))
            ans++;
    }
    return ans;
}

int main(){
    int k,t = 0,c;
    int i,j;
    int a,b;
    int maxn;

    while(scanf("%d%d%d",&n,&m,&k) != EOF){
        t++;
        c = 0;
        memset(link,0,sizeof(link));
        memset(match,0,sizeof(match));
        while(k--){
            scanf("%d%d",&a,&b);
            match[a][0]++;
            int num = match[a][0];
            match[a][num] = b;
        }
        maxn = Hugary();
        for(i = 1;i <= n;i++)
            if(match[i][0]){
                for(j = 1;j <= match[i][0];j++){
                    int tem = match[i][j];
                    match[i][j] = 0;
                    memset(link,0,sizeof(link));//别忘了初始化
                    if(Hugary() < maxn)
                        c++;
                    match[i][j] = tem;//别忘了还原
                }
            }
        printf("Board %d have %d important blanks for %d chessmen.\n",t,c,maxn);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值