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;
}
ZOJ 1654--Place the Robots【二分匹配 && 经典建图】的更多相关文章
- ZOJ 1654 Place the Robots (二分匹配 )
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...
- BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...
- TTTTTTTTTTTT POJ 2112 奶牛与机器 多重二分匹配 跑最大流 建图很经典!!
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 15682 Accepted: 5597 ...
- ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图 ...
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
- zoj 3460 Missile【经典建图&&二分】
Missile Time Limit: 2 Seconds Memory Limit: 65536 KB You control N missile launching towers. Ev ...
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖
题意:图没什么用 给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖 最小路径覆盖=|G|-最大匹配数 ...
- POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图
题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多 ...
随机推荐
- python 7:del 列表指定元素、list.pop(索引)、list.remove(元素值)(删除列表指定元素,且不可再使用;默认索引-1,弹出指定列表元素,可再使用;移除列表指定第一个元素)
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) del bicycles[0] #删除指定列表元 ...
- String,创建对象问题
String str=new String("aaa"); 这行代码究竟创建了几个String对象呢?答案是2个,而不是3个.由于new String("aaa" ...
- 好用的Cache辅助工具类
话不多说,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- Laravel5.1 学习笔记2, 路由
安装的说明请看文档, laravel 安装 #基本路由 你将在 app/Http/routes.php 文件定义大部分路由, 这些路由将被App\Providers\RouteServiceProvi ...
- avaScript中变量的声明和赋值
变量是指程序中一个已经命名的存储单元,它的主要作用就是为数据操作提供存放信息的容器.变量是相对常量而言的.常量是一个不会改变的固定值,而变量的值可能会随着程序的执行而改变.变量有两个基本特征,即变量名 ...
- AndroidStudio怎样导入library项目库
先打开一个Project,然后将libraryr的项目作为module进行导入: File菜单->import module菜单 以上只是导入进来,还没有作为与project真正有效的一部分.需 ...
- 图片懒加载插件echo.js——改造
今天做一个列表项需要用到懒加载,搜到网友推荐的echo.js,试用了一下,还不错.除了懒加载,还提供了throttle——节流,即用户快速滑动列表时,很快滑过的项的图片不会加载,只会加载最后停下来的位 ...
- Emmet(Zen Coding)语法规则简介
———Emmet(Zen Coding)语法规则简介——— [Zen Coding可谓快速开发HTML和CSS的利器,主要采用仿css类选择器方式编写代码,以下是该利器的基本语法规则和代码示例] 基础 ...
- Java_Web之俱乐部会员信息管理系统
使用 Jsp实现俱乐部会员信息管理功能,orac1e11g作为后台数据库,该系统包括查看俱乐部会员信息列表和修改俱乐部会员信息两人功能,具体耍求如下: 打开俱乐部会员信息列表页面,以列表方式显示所有俱 ...
- JavaScript+CSS交互
当鼠标移动到小图片上时,小图片显示红色边框并在上面大图片显示相应大图片,效果如图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...