ZOJ 1654--Place the Robots【二分匹配 && 经典建图】

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

题意:有个 n * m地图,地图由方格组成,#代表墙壁, *代表草地,o代表空地。

如今要在地图上放置机器人,每一个机器人都配备了激光枪,能够向上下左右四个方向开枪。发射的激光能够穿透草地。但不能穿透墙壁,机器人仅仅能放置在空地上。两个机器人不能放置在同一行同一列。除非他们之间有一堵墙隔开,问地图上能够放置的机器人的最大数目。

解题:这题和HDU5093基本一模一样。题中有草地,我们根本不用管,具体思路请看 : HDU5093

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

int map[51 * 50][51 * 50];
char str[51][51];
int x[51][51], x1;
int y[51][51], y1;
int used[51 * 51];
int link[51 * 51];
int n, m;

void init(){
    memset(map, 0, sizeof(map));
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
}

void creat_x(){
    x1 = 1;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(str[i][j] == 'o')
                x[i][j] = x1;
            if(str[i][j] == '#')
                x1++;
        }
        x1++;
    }
}

void creat_y(){
    y1 = 1;
    for(int j = 0; j < m; ++j){
        for(int i = 0; i < n; ++i){
            if(str[i][j] == 'o')
                y[i][j] = y1;
            if(str[i][j] == '#')
                y1++;
        }
        y1++;
    }
}

void getmap(){
    creat_x();
    creat_y();
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j)
            if(str[i][j] == 'o')
            map[x[i][j]][y[i][j]] = 1;
    }
}

bool dfs(int x){
    for(int i = 1; i < y1; ++i){
        if(map[x][i] && !used[i]){
            used[i] = 1;
            if(link[i] == -1 || dfs(link[i])){
                link[i] = x;
                return true;
            }
        }
    }
    return false;
}

int hungary(){
    int ans = 0;
    memset(link, -1, sizeof(link));
    for(int j = 1; j < x1; ++j){
        memset(used, 0, sizeof(used));
        if(dfs(j))
            ans++;
    }
    return ans;
}
int main (){
    int T;
    int k = 1;
    scanf("%d", &T);
    while(T--){
        init();
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; ++i)
            scanf("%s", str[i]);
        getmap();
        printf("Case :%d\n", k++);
        int sum = hungary();
        printf("%d\n", sum);
    }
    return 0;
}
时间: 2024-11-05 01:57:07

ZOJ 1654--Place the Robots【二分匹配 &amp;&amp; 经典建图】的相关文章

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

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

Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖

题意:图没什么用  给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖       最小路径覆盖=|G|-最大匹配数                   证明:https://blog.csdn.net/qq_34564984/article/details/52778763 证明总的来说就是尽可能多得连边 边越多 可以打包一起处理得点就越多(这里题中打包指连续得两个点只需要一条线段就能覆盖) 拆点思想   :匈牙

POJ-3020 Antenna Placement---二分图匹配&amp;最小路径覆盖&amp;建图

题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多少雷达能把所有的*覆盖 解题思路: 把每个*城市编号,然后每相邻两个城市之间连线.这里求最少多少个雷达可以覆盖完*,就是二分图匹配中的最小路径覆盖数,但是这里的图的边是双向的.举个例子 o*o **o ooo 这里可以编号成 010 230 000 那么有边<1,3><3,1><

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 3640--Missile【二分查找 &amp;&amp; 最大流dinic &amp;&amp; 经典建图】

Missile Time Limit: 2 Seconds      Memory Limit: 65536 KB You control N missile launching towers. Every tower has enough missiles, but for each tower only one missile can be launch at the same time. Before the launching, every missile needT1 seconds