题目链接

Problem Description

Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room’s floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below… Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety… but there’s a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.

Input

The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an ‘L’ for every position where a lizard is on the pillar and a ‘.’ for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance isalways 1 ≤ d ≤ 3.

Output

For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.

Sample Input

4
3 1
1111
1111
1111
LLLL
LLLL
LLLL
3 2
00000
01110
00000
.....
.LLL.
.....
3 1
00000
01110
00000
.....
.LLL.
.....
5 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........

Sample Output

Case #1: 2 lizards were left behind.
Case #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.

AC

  • 建图

    1. 源点到蜥蜴所在的位置编号建边,流量为1
    2. 遍历图上每个点,把所有可以相互跳跃的点建边,流量为inf
    3. 途中的每个点拆点建边,流量为可以跳跃的次数
    4. 边缘的点到汇点建边,流量为inf
  • 注意输出:were,was,lizard,lizards

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 1005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;
struct ac{int v, c, pre;
}edge[800001];
int head[N], dis[N], curedge[N], cnt;
int inf = 0x3f3f3f3f;
void addedge(int u, int v, int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c = 0;edge[cnt].pre = head[u];head[u] = cnt++;
}
bool bfs(int s, int e) {queue<int> que;que.push(s);memset(dis, 0, sizeof(dis));dis[s] = 1;while (!que.empty()) {int t = que.front();que.pop();for (int i = head[t]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] || edge[i].c == 0)   continue;dis[edge[i].v] = dis[t] + 1;que.push(edge[i].v);} }return dis[e] != 0;
}
int dfs(int s, int e, int flow) {if (s == e) return flow;for (int &i = curedge[s]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] == dis[s] + 1 && edge[i].c) {int d = dfs(edge[i].v, e, min(flow, edge[i].c));if (d > 0) {edge[i].c -= d;edge[i ^ 1].c += d;return d; }}}return 0;
}
int n, d, m;
int solve(int s, int e) {int sum = 0;while (bfs(s, e)) {for (int i = 0; i <=  e; ++i) {curedge[i] = head[i];}int d;while (d = dfs(s, e, inf)) {sum += d;}}return sum;
}
char map1[40][40], map2[40][40];bool vis_map[40][40];
bool judge_pos(int x, int y) {if (x < 0 || y < 0 || x >= n || y >= m) return false;return true;
}
int dist(int x1, int y1, int x2, int y2) {return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
bool judge_edge(int x, int y) {if (x < d)  return true;if (n - x - 1 < d)  return true;if (y < d)  return true;if (m - y - 1 < d)  return true;return false;
}
int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint t;scanf("%d", &t);int Case = 1;while (t--) {memset(head, -1, sizeof(head));cnt = 0;// 读入两个地图 scanf("%d%d", &n, &d);for (int i = 0; i < n; ++i) {scanf("%s", map1[i]);}for (int i = 0; i < n; ++i) {scanf("%s", map2[i]);}m = strlen(map1[0]);// 源点到蜥蜴建边,流量为1 int start = 0, end = n * m * 2 + 1, sum = 0;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (map2[i][j] == 'L') {sum++;addedge(start, i * m + j + 1, 1);}}}// 对 map1 拆点建图, 权值为可以跳跃的次数 for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {int spos = i * m + (j + 1);int epos = spos + n * m;int num = map1[i][j] - '0';addedge(spos, epos, num);}}// 枚举地图中的位置,将所有可以相互跳跃的点建边, 权值为inf for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {for (int k = -d; k <= d; ++k) {for (int z = -d; z <= d; ++z) {int x = i + k;int y = j + z; if (x == i && y == j)   continue;// fabs(i - x) + fabs(j - y) <= d // 判断下个位置是否能跳过去 if (judge_pos(x, y) && dist(x, y, i, j) <= d * d)   {int s = i * m + j + 1;int e = x * m + y + 1;addedge(s + n * m, e, inf);}}}}}// 边缘的点到汇点建边 for (int i = 0; i  < n; ++i) {for (int j = 0; j < m; ++j) {if (judge_edge(i, j)) {addedge(i * m + j + 1 + n * m, end, inf);}}}int ans = solve(start, end);// 注意输出,were,was,lizard,lizards if (sum - ans == 0) printf("Case #%d: no lizard was left behind.\n", Case++);else if (sum - ans == 1) printf("Case #%d: %d lizard was left behind.\n", Case++, sum - ans);else printf("Case #%d: %d lizards were left behind.\n", Case++, sum - ans);}   return 0;
}

HDU Problem - 2732 Leapin' Lizards(最大流,拆点建边)相关推荐

  1. HDU - 2732 Leapin' Lizards(最大流+思维建边)

    题目链接:点击查看 题目大意:给出两个n*m的迷宫,第一个迷宫中表示每个柱子的耐久度,第二个迷宫表示的是每一只蜥蜴的位置,现在给出每只蜥蜴可以跳跃的最大曼哈顿距离,规定跳出迷宫边界就可以逃离迷宫,现在 ...

  2. HDU Problem - 4289 Control(最大流)

    题目链接 Problem Description You, the head of Department of Security, recently received a top-secret inf ...

  3. hdu 2732 Leapin' Lizards (经典网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2732 Your platoon of wandering lizards has entered a ...

  4. HDU 2732 Leapin' Lizards

    传送门 这道题应用了网络流,主要考的是怎么转化为网络流求解,重点是怎么建图. 题意是给你一个n*m网格,每个格子可能有一个柱子,也可能没柱子,每个柱子上有一个最大跳出次数,用完了这个柱子就废了,每个柱 ...

  5. HDU-2732 Leapin' Lizards 最大流

    题目意思是有一些蜥蜴在一个迷宫里面,求这些蜥蜴还有多少是无论如何都逃不出来的.题目只给定一个行数,一个最远能够跳跃的距离,列数是不确定的(题目告知列数小于等于20),但是数据一定会是一个矩阵.每只蜥蜴 ...

  6. HDU Problem - 4292 Food(最大流, 建边)

    题目链接 Problem Description You, a part-time dining service worker in your college's dining hall, are n ...

  7. 【HDOJ】2732 Leapin' Lizards

    贪心+网络流.对于每个结点,构建入点和出点. 对于每一个lizard>0,构建边s->in position of lizard, 容量为1. 对于pillar>0, 构建边in p ...

  8. HDU Problem - 3338 Kakuro Extension (最大流,建图)

    题目链接 Problem Description If you solved problem like this, forget it.Because you need to use a comple ...

  9. BZOJ-1066 蜥蜴 最大流+拆点+超级源超级汇

    1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2582 Solved: 1272 [Submit][Status] ...

最新文章

  1. 分布式系统 一致性模型的介绍 以及 zookeeper的 “线性一致性“ 讨论
  2. 从现在到未来50年,传感器将如何改变世界?
  3. php内打开网址,网站内部跳转外部网站go.php
  4. android viewgroup 事件,android中viewgroup的事件传递分析
  5. 大话设计模式读书笔记2----单一职责原则(SRP)
  6. Kaggle: House Prices: Advanced Regression Techniques
  7. AttributeError: ‘float‘ object has no attribute ‘exp‘
  8. CCNP精粹系列之十八--路由映射实战二,博主推荐文章
  9. 因为难看的签名尴尬?Python爬虫制作艺术签名软件!
  10. 华为跨域bgp_通知:2019华为认证体系全新升级!
  11. 让R与Python共舞
  12. java中String、StringBuffer和StringBuilder的区别(简单介绍)
  13. html怎么读取2进制视频,IE 中如何读取二进制文件的内容?
  14. STM32L051测试 (四、Flash和EEPROM的读写)
  15. 计算机术语中bug指的是,你知道电脑漏洞为什么叫bug吗?
  16. 【mcuclub】红外测温-MLX90614
  17. mac软件推荐(适用m1)
  18. 为什么循环队列要浪费一个存储空间
  19. 大学必须掌握的软件基础课程
  20. 【python ++ opencv + pytorch 】车牌提取、分割、识别

热门文章

  1. Vue中src属性绑定的问题
  2. 10.8.5如何升级(app store 出错 请稍后重试 100)
  3. oracle复制sequence,oracle sequence语句重置方介绍
  4. [网络安全自学篇] 八.Web漏洞及端口扫描之Nmap、ThreatScan和DirBuster原理详解
  5. OpenGL之利用矩阵的“平移”“旋转”“综合变换”等实现矩形的移动
  6. 18万,是特斯拉的底线,是马斯克的目标!
  7. 7.3.1 阻塞IO(blocking IO)
  8. BASIC-3 字母图形
  9. 1133:输出亲朋字符串
  10. Java常用API(三)Pattern 正则表达式