NOIP2013 Huarong Road--the strongest in history

Source: Internet
Author: User




[Ideas for solving problems]
Thinking of solving problems
The data range of the problem is n≤30, so, we can see that the bare O (N4) of the wide search for the solution of Q is not pressure, but in the case of Q large, there is no doubt that the time overrun, obviously, in the case of Q large, we need to each wide search in the redundant information of repeated search to remove, So we can first analyze the nature of the topic: (this is said to move the pieces as the target pieces)
First of all, if you want to move the target piece, then we must first move the space to the top and bottom of the piece four parties
Up one of the adjacent positions before you can move the pawn. Then, we analyze the nature of the pawn movement: every time the pawn can move, only the empty lattice in its adjacent position, each time the piece is moved, the space will always be in chess
Sub-adjacent position, then we find that for a pawn in a certain position, and then a space in its four directions in a certain
Adjacent position, the piece to want to move in a certain direction when the number of steps to spend is certain, then, you can advanced
The number of steps required for each move of the target piece under the above conditions is preprocessed. Then, after the preprocessing is complete, we will find that each query becomes a shortest-circuiting problem, which can be resolved within the timeframe by Dijstra or SPFA. (SPFA better)
Realize:
Define an array f[x][y][k][h], indicating the target piece in position (x, y) and the space in the direction of the target piece in the K-orientation of the adjacent lattice, the target piece to H-direction to move 1 square steps required, and then use the state [x][y][k] as a node map, with each state of the relationship with the edge, Redefine a source point and end point each time you ask, and the shortest way to get the answer.
(The BFS that runs N2 times O (N2) is ready for processing)
[Reference Program]

ConstUArray[1..4] ofInteger= (-1,0,1,0);//Four methods of migrationV:Array[1..4] ofInteger= (0,1,0,-1); oo=100000000;typeNode=RecordX,y,cs,l:longint;End;varFt:text;    N,m,q,ii,i,i1,j1,j,l,w,x,y,x1,y1,x2,y2,x3,y3,es,ey,wei,tou,kx,ky,ans:longint; R:Array[0..5000000] ofNode AA,A,FB,FB2:Array[0.. to,0.. to] ofLongint; FF,Z,Z2:Array[0.. to,0.. to,1..4] ofLongint; F:Array[0.. to,0.. to,1..4,1..4] ofLongint; procedure bfs(x,y:longint);  //Wide searchvarI,x1,y1:longint;beginwei:=1; tou:=0; R[wei].x:=x;//r.x is used to record the horizontal axis that can be movedR[wei].y:=y;//OrdinateR[wei].cs:=fb[x,y];//A few steps can be moved to   whileTou<wei Do   beginInc (TOU); fori:=1  to 4  Do //Can I move to another point     beginX1:=r[tou].x+u[i]; Y1:=r[tou].y+v[i];if(a[x1,y1]=1) and(r[tou].cs+1&LT;FB[X1,Y1]) Then //Can be moved to and less than the number of steps before        beginInc (Wei);         r[wei].x:=x1;         R[wei].y:=y1; r[wei].cs:=r[tou].cs+1; Fb[x1,y1]:=r[wei].cs;//record (the end of the process is moved to the F four-dimensional array)        End;End;End;End;beginREADLN (N,M,Q);//Read into Phalanx   fori:=1  toN Do    begin     forj:=1  toM Do      Read(A[i,j]); READLN;End; Aa:=a; fori:=1  toN Do //Initialize    forj:=1  toM Do     beginFb2[i,j]:=oo; forl:=1  to 4  Do        beginZ2[i,j,l]:=oo; forw:=1  to 4  DoF[i,j,l,w]:=oo;End;End; fori:=1  toN Do  //Pretreatment     forj:=1  toM Do     ifa[i,j]=1  Then  //If the piece can be moved        forl:=1  to 4  Do  //Four directions         beginX:=I+U[L]; Y:=J+V[L];if(x>0) and(x<n+1) and(y>0) and(y<m+1) and(a[x,y]=1) Then //Assuming a space of four weeks            begina[x,y]:=0;//has been moved to prevent repetitive movementFB:=FB2;//Resetfb[i,j]:=1;//spaces move to this positionBFS (I,J); forw:=1  to 4  Do               beginX1:=X+U[W]; Y1:=Y+V[W];if(x1>0) and(x1<n+1) and(y1>0) and(y1<m+1) and(Fb[x1,y1]<oo) Then                  beginF[I,J,L,W]:=FB[X1,Y1];//Record the number of steps required for the target piece to move 1 squares toward the h direction when the target piece is in position (x, y) and the space is adjacent to the square of the target piece's K direction                  End;End; a[x,y]:=1;//Backtracking            End;End; forii:=1  toQ Do    begin    Read(Kx,ky,x,y,es,ey);if(x=es) and(Y=ey) Then //If the initial position is the same as the target position      beginWriteln (0);Continue;End; FB:=FB2;//a[x,y]:=0;//spaces cannot be moved to this pointfb[kx,ky]:=0;//Space move 0 steps toBFS (Kx,ky);//Where the space can gowei:=0; Z:=Z2;//ResetFillchar (ff,sizeof (FF),0); forj:=1  to 4  Do //The target piece moves around      beginX1:=X+U[J]; Y1:=Y+V[J];if(x1>0) and(x1<n+1) and(y1>0) and(y1<m+1) and(Fb[x1,y1]<oo) Then //spaces can be moved to the grid where the target piece is located         begin  //RecordInc (Wei);          R[wei].x:=x;          R[wei].y:=y; R[wei].l:=j;//indicating directionZ[X,Y,J]:=FB[X1,Y1];//A position can move up to a few steps in a certain directionff[x,y,j]:=1;//A position can be moved in a certain direction         End;End; tou:=0; whileTou<wei Do //SPFA      beginInc (TOU); fori:=1  to 4  Do        beginx2:=r[tou].x;         Y2:=R[TOU].Y;         X1:=x2+u[r[tou].l]+u[i];         Y1:=y2+v[r[tou].l]+v[i];         X3:=X2+U[R[TOU].L]; Y3:=Y2+V[R[TOU].L];if(F[x2,y2,r[tou].l,i]<>oo) and(Z[x3,y3,i]>z[x2,y2,r[tou].l]+f[x2,y2,r[tou].l,i]) Then beginZ[x3,y3,i]:=z[x2,y2,r[tou].l]+f[x2,y2,r[tou].l,i];ifff[x3,y3,i]=0  Then //Record              beginff[x3,y3,i]:=1;              Inc (Wei);              r[wei].x:=x3;              R[wei].y:=y3; R[wei].l:=i;End;End;End; ff[r[tou].x,r[tou].y,r[tou].l]:=0;End; Ans:=oo; fori:=1  to 4  DoFindifZ[es,ey,i]<ans ThenAns:=z[es,ey,i];ifAns=oo ThenWriteln (-1)ElseWriteln (ANS); a[x,y]:=1;//Backtracking  End;End.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NOIP2013 Huarong Road--the strongest in history

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.