集训队专题(7)1004 Leapin' Lizards

来源:互联网 发布:unity3d中人物模型 编辑:程序博客网 时间:2024/06/05 13:30

Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1969    Accepted Submission(s): 811


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
43 1111111111111LLLLLLLLLLLL3 2000000111000000......LLL......3 1000000111000000......LLL......5 20000000002000000003211000200000000000000..................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.
 

Source
Mid-Central USA 2005
 

此题在题意上不是很好懂,作为同样是英语渣的小编在这里先翻译一下题意,在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外。 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不变),如果该石柱原来高度为1,则蜥蜴离开后消失。以后其他蜥蜴不能落脚。任何时刻不能有两只蜥蜴在同一个石柱上。

肯定很多童鞋开始和小编一样,读了题完全没想出和题和网络流是怎么扯上关系的,也对,网络流的问题难就难在建图上,此题思考一下,我们可以先得出建图的策略,首先我们找出能一步跳出迷宫的柱子,并将其与汇点连接,再将所有的蜥蜴所在的位置与源点连接,流量即设为1,接下来就是相互之间嗯那个跳到的建边。最后就是求出最大流后找出剩下的蜥蜴就好。(还有就是要注意坑爹的单复数)。

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int maxn = 1005;const int oo = 1e9+7;struct point{int x,y,flow;bool Lizards;}p[maxn];struct Edge{int v,flow,next;bool Lizards;}edge[maxn];int head[maxn],cnt,Layer[maxn];void init(){cnt = 0;memset(head,-1,sizeof(head));}bool canLine(point a,point 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;else return false;}void addedge(int u,int v,int flow){edge[cnt].v = v;edge[cnt].flow = flow;edge[cnt].next = head[u];head[u] = cnt++;edge[cnt].v = u;edge[cnt].flow = 0;edge[cnt].next = head[v];head[v] = cnt++;}bool BFS(int start,int End){memset(Layer,0,sizeof(Layer));queue<int> Q;Q.push(start);Layer[start] = 1;while(!Q.empty());{int u=Q.front();Q.pop();if(u == End) return true;for(int j=head[u]; j!=-1; j=edge[j].next){int v = edge[j].v;if(!Layer[v] && edge[j].flow){Layer[v] = Layer[u]+1;Q.push(v);}}}return false;}int DFS(int u,int maxFlow,int End){if(u == End) return maxFlow;int uflow = 0;for(int j=head[u]; j!=-1; j=edge[j].next){int v = edge[j].v;if(Layer[v]==Layer[u]+1 && edge[j].flow){int flow = DFS(v, flow, End);edge[j].flow -= flow;edge[j^1].flow += flow;uflow += flow;if(uflow == maxFlow) break;}}if(uflow == 0) Layer[u] = 0;return uflow;}int Dinic(int start,int End){int maxFlow = 0;while( BFS(start,End) )maxFlow += DFS(start,oo,End);return maxFlow;}int main(){int T,t=1;scanf("%d",&T);while(T--){int i,j,N,M,d,k=0,sum=0;char s[maxn];init();scanf("%d%d",&M,&d);for(i=0; i<M; i++){scanf("%s",s);N = strlen(s);for(i=0; i<M; i++){k++;p[k].x = i;p[k].y = j;p[k].flow = s[j] - '0';}}for(i=k=0; i<M; i++){scanf("%s",s);for(j=0; s[j]; j++){k++;if(s[j] == 'L') p[k].Lizards = true;else p[k].Lizards = false;}}int start = k*2+1,End = start+1;for(i=1; i<=k; i++){if(p[i].flow > 0){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,End,oo);//能从这点跳出去,与汇点相连if(p[i].Lizards){addedge(start,i,1);//如果这点是一个蜥蜴,与源点相连,流量是1sum++;}for(j=1; j<=k; j++){if(i == j) continue;if(p[j].flow && canLine(p[i],p[j],d))addedge(i+k,j,oo);}}}int Flow = Dinic(start,End);Flow = sum-Flow;if(Flow == 0)printf("Case #%d: no lizard was left behind.\n", t++);else if(Flow == 1)printf("Case #%d: 1 lizard was left behind.\n", t++);elseprintf("Case #%d: %d lizards were left behind.\n", t++, Flow);}}


0 0