Uvalive 2147push!! (Queue implementation DP)

Source: Internet
Author: User

As far as I understand it, the problem is essentially a DP question, not a search, because I do this by finding all the data in the table and using the queue violence to traverse all the states, because the data range in the topic is small and time-consuming is small.

First analysis box is a passive object, people are active objects, the movement of the box depends on the movement of people, so in the BFS only need to let people move, and then drive the box on it. We use Dp[x1][y1][x2][y2] to record status, representing the location of people and chests, respectively. In the process of implementing DP in the queue, we have to mark the current situation as not traversed , which is important. One might question that this could lead to an infinite queue, leading to a dead loop, so a trick is needed, first whether the new state is updated with whether the state is flagged or not, so it is possible to get this point into the queue and need to first satisfy the condition that the status is worth updating. So it is impossible to go back without meaning, and it is impossible to walk past and lead to a cycle of death. then it is necessary to satisfy this point is not marked , in order to enter the queue, mark this point.

Of course, some people will ask, as long as the status of the update can be, why do you have to enter the queue? If not only the queue, updated only this state, the other state can not be transferred through him, unable to obtain the optimal sub-structure, it is not consistent with the idea of DP, is wrong. (Of course, this place may be just that I do not understand, but this tagging method is still very important), I give an example of it.

0 0 0 0 0 0

0 0 1 1 1 0

0 0 1 4 0 0

0 0 1 2 1 0

0 0 0 0 3 0

0 0 0 0 1 0

0 0 0 0 0 0

I believe you can quickly find two ways to go up and take more steps, in the process of BFS is also later reached point, so the first time BFS to two bold 0, the cost of fewer steps, but the number of pushes, and the title of the minimum number of pushes, and the number of steps to walk regardless, So this must be updated and into the queue to update the other points, if we do not eliminate the previous token, causing the incoming queue to fail, the update fails, and the answer goes wrong.

The last sentence summarizes, in the process of implementing DP in the queue, must remember to eliminate the previous mark, let the new point into the queue, update the other state, in order to get the final optimal solution.

The code is as follows:

#include <iostream>#include<cstdio>#include<queue>#include<cmath>#include<cstring>using namespacestd;#defineMAXN 10#defineINF 999999999intDP[MAXN][MAXN][MAXN][MAXN],N,M,MAPS[MAXN][MAXN];intHot4][2] = {{1,0},{-1,0},{0,1},{0,-1}};intVis[maxn][maxn][maxn][maxn],endx,endy;structnode{intX1,y1,x2,y2;    Node () {}; Node (intXX1,intYy1,intXX2,intyy2) {X1=xx1; Y1=yy1; X2=xx2; Y2=Yy2; }};voidMark (Node a) {Vis[a.x1][a.y1][a.x2][a.y2]=1;}BOOLOkintXinty) {    return(x>=0&&x<n && y>=0&&y<m && maps[x][y]!=1);}intBFS (Node start) {Queue<Node>que; memset (Vis,0,sizeof(VIS));  while(!que.empty ()) Que.pop ();    Que.push (start); Dp[start.x1][start.y1][start.x2][start.y2]=0; //mark (start);     while(!Que.empty ()) {Node now=Que.front ();        Que.pop (); intX1 = Now.x1,y1 =now.y1; intx2 = Now.x2,y2 =Now.y2; Vis[x1][y1][x2][y2]=0;/// Enter queue, eliminate tagsintXx1,xx2,yy1,yy2;  for(inti =0; I <4; i++) {xx1= x1 + go[i][0]; Yy1= y1 + go[i][1]; if(OK (xx1,yy1)) {if(xx1 = = x2 && yy1 = =y2) {xx2= x2 + go[i][0]; Yy2= y2 + go[i][1]; if(OK (xx2,yy2)) {if(dp[xx1][yy1][xx2][yy2]==-1|| dp[xx1][yy1][xx2][yy2]>dp[x1][y1][x2][y2]+1)/// First level inspection {Dp[xx1][yy1][xx2][yy2]= dp[x1][y1][x2][y2]+1;/// update current point statusif(!Vis[xx1][yy1][xx2][yy2])                                {Node NXT (xx1,yy1,xx2,yy2);                            Mark (NXT);////Enter queue, update other point Que.push (NXT); }                        }                    }                }                Else                {                    if(dp[xx1][yy1][x2][y2]==-1|| Dp[xx1][yy1][x2][y2]>Dp[x1][y1][x2][y2]) {Dp[xx1][yy1][x2][y2]=Dp[x1][y1][x2][y2]; if(!Vis[xx1][yy1][x2][y2])                            {Node NXT (xx1,yy1,x2,y2);                            Mark (NXT);                        Que.push (NXT); }                    }                }            }        }    }    intAns =INF;  for(inti =0; I < n; i++)    {         for(intj =0; J < M; J + +)        {            //printf ("dp[%d][%d] =%d\n", I,j,dp[i][j][endx][endy]);            if(Dp[i][j][endx][endy]! =-1) ans=min (Ans,dp[i][j][endx][endy]); }    }    if(ans = INF)returnans; return-1;}intMain () { while(~SCANF ("%d%d",&m,&N)) {if(N+m = =0) Break;        Node start;  for(inti =0; I < n; i++)        {             for(intj =0; J < M; J + +) {scanf ("%d",&Maps[i][j]); if(Maps[i][j] = =2) {start.x2=i; Start.y2=J; }                if(Maps[i][j] = =4) {start.x1=i; Start.y1=J; }                if(Maps[i][j] = =3) {EndX=i; Endy=J; }}} memset (DP,-1,sizeof(DP)); intAns =BFS (start); printf ("%d\n", ans); }    return 0;}

Uvalive 2147push!! (Queue implementation DP)

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.