棋盘游戏 HDU - 1281

本文通过一个具体的编程实例,详细介绍了如何使用二分匹配算法解决特定类型的问题,并提供了完整的代码实现,包括图的构建和匹配过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

中文题面

注意建图方式:

  1. 将行与列看作顶点 
  2. 棋子所在的位置的行与列所对应的顶点建边
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int N,M,K;
bool G[105][105];
int V;
int match[205];
bool used[205];
int col[10010],row[10010];
bool dfs(int u){
  for (int i = 1;i <=N;i++){
    if (!used[i] && G[u][i]){
      used[i] = true;
      if (match[i] < 0 || dfs(match[i])){
        match[i] = u;
        return true;
      }
    }
  }
  return false;
}
int bipartitie_matching(){
  int res = 0;
  memset(match,-1,sizeof match);
  for (int v = 1;v<=N;v++){
      memset(used,0,sizeof used);
      if (dfs(v)) res++;
  }
  return res;
}

int main()
{
  int t = 0;
  while(scanf("%d%d%d",&N,&M,&K) != EOF){
    memset(G,0,sizeof G);
    for (int i = 0;i<K;i++){
      int x,y;
      scanf("%d%d",&col[i],&row[i]);
      G[col[i]][row[i]]  = true;
    }
    int max_match = bipartitie_matching();
    int l = 0,ans;
    for (int i = 0;i<K;i++){
      G[col[i]][row[i]] = false;
      ans = bipartitie_matching();
      if (ans < max_match) l++;
      G[col[i]][row[i]] = true;
    }
    printf("Board %d have %d important blanks for %d chessmen.\n",++t,l,max_match) ;
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值