ZOJ 1654 Place the Robots (二分匹配 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654

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

PS:

http://blog.csdn.net/acdreamers/article/details/8654005

这图要用邻接表! 否则会爆内存!

50*50/2! 开1500的二维就OK拉!

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAXN 1547
int LN, RN;//L,R数目
int g[MAXN][MAXN], linker[MAXN];
bool used[MAXN];
char ma[57][57];
int r[MAXN][MAXN], c[MAXN][MAXN];
int head[MAXN];
struct node
{
    int x;
    int next;
} edge[MAXN<<1];
int cnt = 1;
void addEdge(int x, int y)
{
    edge[cnt].x = y;
    edge[cnt].next = head[x];
    head[x] = cnt++;
}
int dfs(int L)//从左边开始找增广路径
{
    int R;
    //for(R = 1; R <= RN; R++)
    for(int i = head[L]; ~i; i = edge[i].next)
    {
        R = edge[i].x;
        if(!used[R])
        {
            //找增广路,反向
            used[R]=true;
            if(linker[R] == -1 || dfs(linker[R]))
            {
                linker[R] = L;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    int res = 0 ;
    int L;
    memset(linker,-1,sizeof(linker));
    for( L = 1; L <= LN; L++)
    {
        memset(used,0,sizeof(used));
        if(dfs(L) != 0)
            res++;
    }
    return res;
}

int check(int x, int y)
{
    if(ma[x][y]=='o' || ma[x][y]=='*')
    {
        return 1;
    }
    return 0;
}
void init()
{
    cnt = 0;
    memset(head,-1,sizeof(head));
    memset(g,0,sizeof(g));
    memset(c,0,sizeof(c));
    memset(r,0,sizeof(r));
}
int main()
{
    int t;
    int n, m;
    int k, L, R;

    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++)
        {
            scanf("%s",ma[i]+1);
        }

        int cnt1 = 1, cnt2 = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(check(i, j))
                {
                    if(r[i][j] == 0)//行
                    {
                        for(int k = j; k <= m; k++)
                        {
                            if(check(i, k))
                            {
                                r[i][k] = cnt1;
                            }
                            else
                            {
                                break;
                            }
                        }
                        cnt1++;
                    }
                    if(c[i][j] == 0)//列
                    {
                        for(int k = i; k <= n; k++)
                        {
                            if(check(k, j))
                            {
                                c[k][j] = cnt2;
                            }
                            else
                            {
                                break;
                            }
                        }
                        cnt2++;
                    }
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(r[i][j] && c[i][j] && ma[i][j]=='o')
                {
                    //g[r[i][j]][c[i][j]] = 1;
                    //g[c[i][j]][r[i][j]] = 1;
                    addEdge(r[i][j], c[i][j]);
                }
            }
        }
        LN = cnt1;
        RN = cnt2;
        int ans = hungary();
        printf("Case :%d\n",++cas);
        printf("%d\n",ans);
    }
    return 0 ;
}
/*
2
4 4
o***
*###
oo#o
***o
4 4
#ooo
o#oo
oo#o
***#
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-29 07:16:50

ZOJ 1654 Place the Robots (二分匹配 )的相关文章

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

题意:在一个m*n的地图上,有空地,草和墙,其中空地和草能穿透攻击光线,而墙不能.每个机器人能够上下左右攻击,问在地图上最多能放多少个不互相攻击的机器人. 这个题和HDU 1045 -  Fire Net很像.很容易联想到对每个点编号然后互相攻击的点连边再求图的最大独立集,但是这个题数据量太多,超时. 换个思路. 将每个机器人可以攻击到的区域在横和竖方向上分开,这样当横和竖攻击方向都确定时就可以确定一个机器人的放置位置,而在同一个横或竖的攻击区域内不可以再放置机器人. 这样我们把原图上的空地按照

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

唉,又是神一样的建模,表示完全想不到. 题意是给你一块地,上面有空地,草地,障碍三种地形,然后让你在上面放机器人,机器人只能放在空地上.机器人会向上下左右四个方向发出攻击,机器人的攻击可以穿过草地但是无法穿过障碍.问你在不会是机器人相互攻击的前提下,最多能放多少个机器人. 我觉得大致的思路应该是这样的,首先会想当然的想到把能够相互攻击到的空地连边,然后求最大独立集,但最大独立集不好求,所以要想办法把它转化成最大匹配问题. 所以要想办法把空地变成边,首先一块空地肯定是有他的横坐标和纵坐标,可以表示

ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图.*代表草地,#代表墙,o代表空地,要再图中的o处放机器人,机器人能够攻击(上下左右)4个方向,攻击范围无限长,并且机器人不能相互攻击,草地不能放置机器人,且机器人的攻击能够穿过草地,可是机器人的攻击不能穿过墙,比方 "   *o#o  "这一行就能够放两个机器人," o*oo

ZOJ 1654 Place the Robots

题目大意: 在空地上放置尽可能多机器人,机器人朝上下左右4个方向发射子弹,子弹能穿过草地,但不能穿过墙, 两个机器人之间的子弹要保证互不干扰,求所能放置的机器人的最大个数 每个机器人所在的位置确定了,那么对应的横向和竖向子弹能到达的空地就全部被覆盖了 我们将横向所能连接在一块的空地区域标上同一个标号 比如o*o#o , 就可标号为10102因为1,3空地中间的草地不影响子弹的穿越 同理,我们将竖向的所能连接在一块的空地区域标上同一个标号 那么就可以建立一个横向到达竖向的二部图匹配 每次成功匹配一

ZOJ 1654 Place the Robots(最大匹配)

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 ro

ZOJ 3156 Taxi (二分匹配+二分查找)

题目链接:Taxi Taxi Time Limit: 1 Second      Memory Limit: 32768 KB As we all know, it often rains suddenly in Hangzhou during summer time.I suffered a heavy rain when I was walking on the street yesterday, so I decided to take a taxi back school. I foun

ZOJ 1516 Uncle Tom&#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncl

poj 2594 Treasure Exploration (二分匹配)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2644 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>