Hdu2732leapin ' lizards (max stream SAP, Build---vertex method)

Source: Internet
Author: User

Leapin ' lizardsTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1531 Accepted Submission (s): 623


Problem Descriptionyour platoon of wandering Lizards has entered a strange the The labyrinth. As you is looking around for hidden treasures, one of the rookies steps in an innocent-looking stone and the the "s floor Suddenly disappears! Each lizard in your platoon are 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 the-the-same, and report the number of casualties.
The pillars in the class is aligned as a grid, with each pillar one unit away from the pillars to it east, west, north an D South. Pillars at the edge of the grid is one unit away from the edge of the The (safety). Not all pillars necessarily has a lizard. A Lizard is able to leap onto any unoccupied pillar the is within D units of he current one. A lizard standing on a pillar within leaping distance of the edge of the the-the-the-all leap to safety ... but there ' s a C Atch:each pillar becomes weakened after each jump, and would 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-pillar at any given time.
Inputthe input file would begin with a line containing a single integer representing the number of test cases, which are at Most 25. Each test case would begin with a line containing a single positive an integer n representing the number of rows in the map, F Ollowed by a single non-negative integer d representing the maximum leaping distance for the lizards. Both maps would follow, each as a map of characters with one row per line. The first map would contain a digit (0-3) in each position representing the number of jumps the pillar in that position wil L Sustain before collapsing (0 means there is no pillar there). The second map would follow, with a ' L ' for every position where a lizard are on the pillar and a '. ' For every empty Pilla R. There'll never be a lizard to a position where there is no pillar. Each input map was 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.
Outputfor each input case, print a single line containing the number of lizards this could not escape. The format should follow the samples provided below.
Sample Input
1111111111111llllllllllll3 2000000111000000......LLL ... 3 1000000111000000......LLL ... 5 20000000002000000003211000200000000000000..................LLLL ......... .....

Sample Output
Case #1:2 lizards were left behind. Case #2: No lizard is left behind. Case #3:3 lizards were left behind. Case #4:1 lizard is left behind.

Sourcemid-central USA 2005  Test instructions: Gives N<=20,d, next gives two graphs, each with n rows, m column (m not given). The first figure shows which positions have pillars, 0 means no pillars, 0 are not, 0 numbers (range: 0~3) represent the maximum number of times that can be jumped from the current column, and the column disappears when the number of hops equals the current position. One can jump from one pillar to another, and the distance cannot be skipped beyond D. The next figure shows the location of the person, L: it means that someone is guaranteed to have a pillar in place. Q: How many people in the figure did not jump out of bounds. Solution: Max stream SAP, key is a map: The vertex method, the position of each column itself as an edge (point (i*m+j+1)---> (i*m+j+1+ n*m)), the capacity of the side of the number of columns can be jumped, and then from each column point (I,J) A point (Ti, TJ) with a column in the range can be jumped to build an edge, the capacity can be set to >= Point (i,j) in the figure of the number of sub-size, why this can be built: because a person jumps from one point to another point will only jump once, so one person at a time. Build to the edge of the meeting point T=2*n*m+1, that is, from a point can jump out of the boundary, then this point can be built with the T-point edge, the same capacity. Consider the s=0 point as the source point, then build an edge from the source to someone's location, with a capacity of 1. So the number of people in the entire graph from the source point to the sink point is (the number of people asked). The maximum amount of outflow is the number of people who can jump to the boundary.
#include <stdio.h> #include <string.h> #include <queue>using namespace std; #define Captype Intconst   int MAXN = 1010;    Total number of points const int MAXM = 2000010;    Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap,flow;}  Edg[maxm];int Eid,head[maxn];int GAP[MAXN];  The number of points for each distance (or can be considered a height) int DIS[MAXN];  The shortest distance from each point to the end Enode int CUR[MAXN];    Cur[u] indicates that from the U point can flow through the cur[u] number side int pre[maxn];void init () {eid=0; memset (head,-1,sizeof (Head));}    There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) without a forward edge {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0;    head[u]=eid++; Edg[eid].to=u;    EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0; head[v]=eid++;}    Captype maxflow_sap (int snode,int enode, int n) {//n is the total number of points including the source and sink points, this must be noted memset (Gap,0,sizeof (GAP));    memset (dis,0,sizeof (dis));    memcpy (cur,head,sizeof (head));    Pre[snode] =-1;    Gap[0]=n;  Captype ans=0;    Max Stream int U=snode; while (dis[snode]<n) {//Judging from SNode point there is noThere is a flow to the next adjacent point if (u==enode) {//Find a path captype min=inf;            int inser;                for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to])//To find the maximum amount of traffic Min if (min>edg[i].cap-edg[i].flow) {                Min=edg[i].cap-edg[i].flow;            Inser=i;                } for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=min;  Edg[i^1].flow-=min;            Flow of the side that can be recycled} ans+=min;            u=edg[inser^1].to;        Continue  } bool flag = FALSE;        It is possible to determine whether an int v can flow toward the neighboring point from the U point;            for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to;                if (edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue        }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of +1 int mind= n for the adjacent flow point; for (int i=Head[u]; I!=-1;            I=edg[i].next) if (edg[i].cap-edg[i].flow>0 && mind>dis[edg[i].to]) {mind=dis[edg[i].to];        Cur[u]=i;        } gap[dis[u]]--;  if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find        Can flow to the point, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++;  if (U!=snode) u=edg[pre[u]^1].to; Return an Edge} return ans; int abs (int a) {return a>0?a:-a;}    int main () {int n,d;    Char g1[50][50],g2[50][50];    int t,_cas=0;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&d);        for (int i=0; i<n; i++) scanf ("%s", G1[i]);        for (int i=0; i<n; i++) scanf ("%s", G2[i]);        Init ();                int M=strlen (g1[0]); Point itself as an edge, point i*m+j+1 into the crossing only from the outside, out of only one, that is, the other point of their own i*m+j+1+n*m, point I*m+j+1+n*m only out of the degree for (int i=0; i<n; i++) foR (int j=0; j<m; J + +) if (g1[i][j]!= ' 0 ') addedg (i*m+j+1, i*m+j+1+n*m, g1[i][j]-' 0 '); int s=0, t=2*n*m+1; Source point, Meeting point for (int i=0; i<n; i++)//Start position (i,j), reachable position (TI,TJ) for (int j=0; j<m; J + +) if (G1[i]  [j]!= ' 0 ') {if (I+d>=n | | i-d<0 | | j+d>=m | | j-d<0) {//can go out of bounds, that is to reach the meeting point ADDEDG (i*m+j+1+n*m , T, g1[i][j]-' 0 ');            Continue } for (int ti=i-d, ti<=i+d; ti++) if (ti>=0&&ti<n) for (int tj= j-d; tj<=j+d; tj++) if (tj>=0&&tj<m && (ti!=i | | tj!=j)) {if (ABS (ti-i) +abs (tj-j) >d| |                g1[ti][tj]== ' 0 ') continue;        ADDEDG (I*m+j+1+n*m, ti*m+tj+1, g1[i][j]-' 0 ');//The capacity of the edge as long as the >=g1[i][j]-' 0 ' can}} int ans=0; for (int i=0; i<n; i++) for (int j=0; j<m; J + +) if (g2[i][j]== ' L ') {ADDEDG (s), I*m+j+1, 1), ans++;        } ans-= Maxflow_sap (S, T, t+1);        if (ans>1) printf ("Case #%d:%d lizards were left behind.\n", ++_cas, ans);        else if (ans==1) printf ("Case #%d:%d lizard is left behind.\n", ++_cas, ans);    else printf ("Case #%d:no lizard is left behind.\n", ++_cas); }}


Hdu2732leapin ' lizards (max stream SAP, Build---vertex method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.