ZOJ 1654 Place the Robots 二分图 最大匹配

本文探讨了一种机器人放置问题,即在给定的棋盘地图上,如何在空地上放置尽可能多的机器人,使得任意两台机器人不在同一行或列,除非中间有墙壁阻隔。通过将地图划分成多个“块”,并利用二分图最大匹配算法,求解最多可放置的机器人数量。

$ \rightarrow $ 戳我进ZOJ原题

Place the Robots

Time Limit: 5 Seconds $ \quad $ Memory Limit: 32768 KB

 

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:
 
Given a map consisting of square blocks.
There were three kinds of blocks: Wall, Grass, and Empty.
His boss wanted to place as many robots as possible in the map.
Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously.
A robot had to stay at the block where it was initially placed all the time and to keep firing all the time.
The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall.
A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another.
In other words,
two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.
 
Now that you are such a smart programmer and one of Robert's best friends,
He is asking you to help him solving this problem.
That is, given the description of a map, compute the maximum number of robots that can be placed in the map.
 

Input

The first line contains an integer $ T (\le 11) $ which is the number of test cases.
 
For each test case, the first line contains two integers $ m $ and $ n (1 \le m, n \le 50) $
which are the row and column sizes of the map.
Then $ m $ lines follow, each contains $ n $ characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.
 

Output

For each test case, first output the case number in one line, in the format:
"Case :id" where id is the test case number, counting from 1.
In the second line just output the maximum number of robots that can be placed in that map.
 

Sample Input

 2
 4 4
 o***
 *###
 oo#o
 ***o
 4 4
 #ooo
 o#oo
 oo#o
 ***#

Sample Output

 Case :1
 3 
 Case :2
 5

Author: XU, Luchuan

Source: ZOJ Monthly, October 2003
 

题目大意

  • 有一个 $ N \times M (N,M \le 50) $ 的棋盘

  • 棋盘的每一格是三种类型之一:空地、草地、墙。

  • 机器人只能放在空地上。

  • 在同一行或者同一列的两个机器人,若他们之间没有墙,则他们可以互相攻击。

  • 问给定的棋盘,最多可以放置多少个机器人,使它们不能互相攻击。

pic1

题解

  • 把每一行、每一列被墙隔开,并且包含空地的连续区域称为“块”。

  • 显然,在一个块之中,最多只能放一个机器人,把这些块编上号。

  • 把每个横向块看作左部的点,竖向块看作右部的点

  • 若两个块有公共的空地,则在它们之间连边

  • 求二分图的最大匹配,即可得到答案

pic2

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define N 55
#define M 2505
vector<int>e[M];
int T,n,m,ans;
bool f;
char mp[N][N];
int cntx,cnty,x[N][N],y[N][N]; 
int link[M]; bool vis[M];
bool dfs(int u){
    for(int i=0;i<e[u].size();++i)
        if(!vis[e[u][i]]){
            int v=e[u][i];
            vis[v]=1;
            if(!link[v]||dfs(link[v])){
                link[v]=u;
                return 1;
            }
        }
    return 0;
}
int main(){
    scanf("%d",&T);
    for(int cases=1;cases<=T;++cases){
        memset(x,0,sizeof(x)); 
        memset(y,0,sizeof(y));
        memset(link,0,sizeof(link));
        cntx=cnty=ans=0;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;++i){
            f=0; scanf("%s",mp[i]+1);
            for(int j=1;j<=m;++j)
                if(mp[i][j]=='o'){
                    if(!f) ++cntx; f=1;
                    x[i][j]=cntx;
                }else if(mp[i][j]=='#') f=0;
        }
        for(int j=1;j<=m;++j){
            f=0;
            for(int i=1;i<=n;++i)
                if(mp[i][j]=='o'){
                    if(!f) ++cnty; f=1;
                    y[i][j]=cnty;
                }else if(mp[i][j]=='#') f=0;
        }
        for(int i=1;i<=cntx;++i) e[i].clear();
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
                if(x[i][j]&&y[i][j]) 
                    e[x[i][j]].push_back(y[i][j]);
        for(int i=1;i<=cntx;++i){
            memset(vis,0,sizeof(vis)); 
            if(dfs(i)) ++ans;
        }
        printf("Case :%d\n%d\n",cases,ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/PotremZ/p/ZOJ1654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值