hdu 2732 Leapin' Lizards (最大流)
2015-08-13 15:19
399 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2732
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 is
always 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
Sample Output
题意:在一个迷宫里面有一些蜥蜴,这个迷宫有一些柱子组成的,并且这些柱子都有一个耐久值,每当一只蜥蜴跳过耐久值就会减一,当耐久值为0的时候这个柱子就不能使用了,每个蜥蜴都有一个最大跳跃值d,现在想知道有多少蜥蜴不能离开迷宫(跳出迷宫就可以离开了。)
输入描述:输入矩阵的行M和跳跃值D,接着输入两个矩阵(列数自己求),第一个矩阵是每个柱子的耐久值,下面一个矩阵有'L'的表示这个柱子上有一只蜥蜴。
分析:题目明白还是比较容易的,矩阵的点数是20*20,也就400个点,对每个有耐久值的柱子进行拆点,然后连接两个柱子(判断是否在跳跃范围内),源点和有蜥蜴的柱子相连,汇点和所有能一下跳出来的柱子相连。注意输出的时候单词的复数单数。 由于没注意单复数,WA了两次
Leapin' Lizards
Problem DescriptionYour 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 is
always 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.
题意:在一个迷宫里面有一些蜥蜴,这个迷宫有一些柱子组成的,并且这些柱子都有一个耐久值,每当一只蜥蜴跳过耐久值就会减一,当耐久值为0的时候这个柱子就不能使用了,每个蜥蜴都有一个最大跳跃值d,现在想知道有多少蜥蜴不能离开迷宫(跳出迷宫就可以离开了。)
输入描述:输入矩阵的行M和跳跃值D,接着输入两个矩阵(列数自己求),第一个矩阵是每个柱子的耐久值,下面一个矩阵有'L'的表示这个柱子上有一只蜥蜴。
分析:题目明白还是比较容易的,矩阵的点数是20*20,也就400个点,对每个有耐久值的柱子进行拆点,然后连接两个柱子(判断是否在跳跃范围内),源点和有蜥蜴的柱子相连,汇点和所有能一下跳出来的柱子相连。注意输出的时候单词的复数单数。 由于没注意单复数,WA了两次
#include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <stack> #include <vector> #include <map> using namespace std; #define N 1350 #define INF 0xfffffff #define PI acos (-1.0) #define EPS 1e-8 struct node1 { int v, next, flow; }edge[N*N]; struct node2 { int x, y, flow; bool lizards; }p ; int n, m, d, cnt, head , layer ; char s ; void Init ();///初始化 void addedge (int u, int v, int flow);///正反向加边 bool outjump (node2 a, node2 b, int d);///判断能否从a点跳到b点 bool BFS (int sa, int en); int DFS (int sa, int maxflow, int en); int dinic (int sa, int en); int main () { int t, flag = 1; scanf ("%d", &t); while (t--) { Init (); scanf ("%d %d", &m, &d); int sum = 0, k = 0; for (int i=0; i<m; i++) { scanf ("%s", s[i]); n = strlen (s[i]); for (int j=0; j<n; j++) { p[++k].x = i, p[k].y = j; p[k].flow = s[i][j] - '0'; } } k = 0; for (int i=0; i<m; i++) { scanf ("%s", s[i]); for (int j=0; j<n; j++) { k++; if (s[i][j] == 'L') p[k].lizards = true;///true说明此点是蜥蜴 else p[k].lizards = false; } } int sa = 2*k+1, en = sa+1;///不太清楚为什么要这样定义源点和汇点 for (int i=1; i<=k; i++) { if (p[i].flow) { addedge (i, i+k, p[i].flow); if (p[i].x-d<0 || p[i].x+d>=m || p[i].y-d<0 || p[i].y+d>=n) addedge (i+k, en, INF);///能从这点跳出去,与汇点相连 if (p[i].lizards) { addedge (sa, i, 1);///如果这点是一个蜥蜴,与源点相连,流量是1 sum++; } for (int j=1; j<=k; j++) { if (i == j) continue; if (p[j].flow && outjump (p[i], p[j], d))///如果能从i跳到j那么让他们相连,注意是拆点i与j相连 addedge (i+k, j, INF); } } } int flow = dinic (sa, en);///能跳出的青蛙数 flow = sum - flow;///不能跳出的青蛙数 if (!flow) printf ("Case #%d: no lizard was left behind.\n", flag++); else if (flow == 1) printf ("Case #%d: 1 lizard was left behind.\n", flag++); else printf ("Case #%d: %d lizards were left behind.\n", flag++, flow); } return 0; } void Init () { memset (head, -1, sizeof (head)); cnt = 0; } void addedge (int u, int v, int flow) { edge[cnt].v = v; edge[cnt].next = head[u]; edge[cnt].flow = flow; head[u] = cnt++; swap (u, v); edge[cnt].v = v; edge[cnt].next = head[u]; edge[cnt].flow = 0; head[u] = cnt++; } bool outjump (node2 a, node2 b, int d) { int len = (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y); if (d*d >= len) return true; return false; } bool BFS (int sa, int en) { memset (layer, 0, sizeof (layer)); queue <int> que; que.push (sa); layer[sa] = 1; while (que.size ()) { sa = que.front (); que.pop (); if (sa == en) return true; for (int i=head[sa]; i!=-1; i=edge[i].next) { int v = edge[i].v; if (edge[i].flow && !layer[v]) { layer[v] = layer[sa] + 1; que.push (v); } } } return false; } int DFS (int sa, int maxflow, int en) { if (sa == en) return maxflow; int uflow = 0; for (int i=head[sa]; i!=-1; i=edge[i].next) { int v = edge[i].v; if (edge[i].flow && layer[v] == layer[sa] + 1) { int flow = min (maxflow - uflow, edge[i].flow); flow = DFS (v, flow, en); edge[i].flow -= flow; edge[i^1].flow += flow; uflow += flow; if (uflow == maxflow) break;///当达到最大流时不再遍历 } } if (!uflow) layer[sa] = 0; return uflow; } int dinic (int sa, int en) { int maxflow = 0; while (BFS (sa, en)) maxflow += DFS (sa, INF, en); return maxflow; }
相关文章推荐
- bzoj2843极地旅行社题解
- 利用Java正则表达式验证手机号、邮箱等
- Collection ArrayList接口
- Android UI 之 Tab类型界面总结
- [codeigniter 五]、查询缓存
- JavaScript的事件代理(转)
- Centos 7 控制台 调整 分辨率 : Mac Virtualbox 下的 centos 命令模式 控制台 分辨率 修改
- eclipse svn添加过滤文件
- POJ 1979 Red and Black
- 文件锁的玩法
- python模块的打包
- React Native通信机制详解
- git代理设置
- [lua]判断nginx收到的是否json
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- ASP.NET MVC文件上传简单示例
- WPF下如何去除WebBrowser的滚动条和捕获关闭事件
- Highmaps网页图表教程之Highmaps第一个实例与图表构成
- DNS简介及配置
- MKNetworkKit 使用