ZOJ1654-Place the Robots(二分图匹配)

63 篇文章 0 订阅
30 篇文章 0 订阅

Place the Robots

Time Limit: 5 Seconds       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 (<= 11) which is the number of test cases. 

For each test case, the first line contains two integers m and n (1<= m, n <=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*m的地图,上面有空地、草坪和墙,现在让你在空地上放机器人,机器人可以向其四周发射激光,激光不能穿过墙,问如何才能放最多的机器人使其不会相互攻击

解题思路:二分图,将每一行或每一列被墙隔开且包含空地的连续区域称作“块”,每个横块看作二分图中顶点集合X中的顶点,竖向快看作集合Y中的顶点,若两个块有公共的空地,则在他们之间连边


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int n,m;
char ch[55][55];
int x[2509],y[2509];
int xs[55][55],ys[55][55];
int xn,yn;
int s[2509],nt[2509],e[2509];
int visit[2509];

bool path(int k)
{
    for(int i=s[k];~i;i=nt[i])
    {
        int ee=e[i];
        if(!visit[ee])
        {
            visit[ee]=1;
            if(y[ee]==-1||path(y[ee]))
            {
                y[ee]=k;
                x[k]=ee;
                return 1;
            }
        }
    }
    return 0;
}

void MaxMatch()
{
    int ans=0;
    memset(x,-1,sizeof x);
    memset(y,-1,sizeof y);
    for(int i=1;i<=xn;i++)
    {
        if(x[i]==-1)
        {
            memset(visit,0,sizeof visit);
            if(path(i)) ans++;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case :%d\n",++cas);
        scanf("%d %d",&n,&m);
        int flag=0,cnt=0;
        for(int i=0;i<n;i++)
            scanf("%s",ch[i]);
        memset(xs,0,sizeof xs);
        memset(ys,0,sizeof ys);
        for(int i=0;i<n;i++)
        {
            flag=0;
            for(int j=0;j<m;j++)
            {
                if(ch[i][j]=='o')
                {
                    if(!flag) cnt++;
                    xs[i][j]=cnt;flag=1;
                }
                else if(ch[i][j]=='#') flag=0;
            }
        }
        xn=cnt;cnt=0;
        for(int i=0;i<m;i++)
        {
            flag=0;
            for(int j=0;j<n;j++)
            {
                if(ch[j][i]=='o')
                {
                    if(!flag) cnt++;
                    ys[j][i]=cnt;flag=1;
                }
                else if(ch[j][i]=='#') flag=0;
            }
        }
        yn=cnt,cnt=1;
        memset(s,-1,sizeof s);
        memset(nt,-1,sizeof nt);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(xs[i][j]&&ys[i][j])
                    nt[cnt]=s[xs[i][j]],s[xs[i][j]]=cnt,e[cnt++]=ys[i][j];
            }
        }
        MaxMatch();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值