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

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1654

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

题目大意:Robert 是一个著名的工程师。一天,他的老板给他分配了一个任务。任务的背景是:给定一 个m×n 大小的地图,图由方格组成,在地图中有3 种方格-墙、草地和空地,他的老板希望 能在地图中放置尽可能多的机器人。每个机器人都配备了激光枪,可以同时向四个方向(上、下、 左、右)开枪。机器人一直待在最初始放置的方格处,不可移动,然后一直朝四个方向开枪。激 光枪发射出的激光可以穿透草地,但不能穿透墙壁。机器人只能放置在空地。当然,老板不希望 机器人互相攻击,也就是说,两个机器人不能放在同一行(水平或垂直),除非他们之间有一堵墙 格开。 
给定一张地图,你的程序需要输出在该地图中可以放置的机器人的最大数目。

思路:详细解答请戳这里

这题建图比较有意思,将同一行或同一列中连续的空地或者中间没有隔墙的空地当做一整个块来处理。对地图按行分块作为二分图左侧的点,对地图按列分块作为二分图右侧的点,若地图中某个空地既是左侧的点又是右侧的点,就把这两个点连一条边。当两条边有公共点时就会发生冲突,因此问题转换成了求二分图的最大匹配。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=2550;

int t,m,n;
int cnt1,cnt2;
char s[105][105];
int x[105][105];
int y[105][105];
int G[maxn][maxn];
int pre[maxn];
int vis[maxn];

bool dfs(int l)
{
    for(int i=1;i<=cnt2;i++)
    {
        if(vis[i]==0&&G[l][i])
        {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i]))
            {
                pre[i]=l;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int times=0;
    scanf("%d",&t);
    while(t--)
    {
        cnt1=cnt2=0;
        memset(pre,-1,sizeof(pre));
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        memset(G,0,sizeof(G));
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            int flag=1;
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='o')
                {
                    if(flag)
                    {
                        x[i][j]=++cnt1;
                        flag=0;
                    }
                    else
                        x[i][j]=cnt1;
                }
                else if(s[i][j]=='#')
                    flag=1;
            }
        }
        for(int i=0;i<m;i++)
        {
            int flag=1;
            for(int j=0;j<n;j++)
            {
                if(s[j][i]=='o')
                {
                    if(flag)
                    {
                        y[j][i]=++cnt2;
                        flag=0;
                    }
                    else
                        y[j][i]=cnt2;
                }
                else if(s[j][i]=='#')
                    flag=1;
            }
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(x[i][j]&&y[i][j])
                    G[x[i][j]][y[i][j]]=1;
        int ans=0;
        for(int i=1;i<=cnt1;i++)
        {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("Case :%d\n",++times);
        printf("%d\n",ans);
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值