NOIP2013 Improvement Group Day2jzoj 3537 Huarong Road

Source: Internet
Author: User
Tags printf first row
Description

Little B has recently been fascinated by the huarong, but he always takes a long time to finish it. So, he thought of programming to complete the Huarong road: Given a situation, whether the Huarong road is not complete, if it can be completed, the minimum amount of time required.
Little B Play the Huarong Road and the classic Huarong road game slightly different, the game rule is this:
On a n*m chessboard there is a n*m lattice, of which there is only one lattice is blank, the rest of the n*m-1 lattice on each lattice there is a chess piece, the size of each piece is 1*1;
Some pieces are fixed, some pieces can be moved;
Any piece on a grid adjacent to a blank lattice (with a public edge) can be moved to a blank lattice. The purpose of the game is to move a piece that can be active at a specified position to the target location.
Given a chessboard, the game can play Q times, of course, each time the fixed grid on the board will not change, but the original position of the blank lattice on the board, the specified movable pieces of the initial position and the target position may be different. When I play, the blank grid is in column Ex_i ey_i, the initial position of the specified movable pawn is the first sx_i row sy_i column, the destination is the tx_i column of the first row.
Let's say that little B can move a pawn one time per second, while the rest of the operation is negligible. Please tell little B the minimum time required for each game, or tell him that it is impossible to complete the game.
Analysis

This is the noip of the problem.
in order to facilitate the description of the text, the white space is specified, the original position of the pieces called Special blocks (the remaining pieces are the same).
First, there is a clear conclusion: moving a piece is equivalent to moving the white block. The
also because the rest of the pieces are equivalent, so record the movement of white and special block position can determine a situation.
In this way, a wide search can pass 70% of data points (if you have an RP good can be over 80%).
But for 100% of the data such a method is not expected to optimize, it seems to find another path.
In fact, the above state can also be optimized: Notice that the position of the special block only changes when it is exchanged with the white block, so we only use record F[X][Y][FX] f[x][y][fx], which represents the coordinates of the special block and a bearing (up and down), which represents the position of the white block in the special block.
Thus, in order to simulate a special block move, we also preprocess a value: f[x][y][fx]−>f[x][y][fx′] f[x][y][fx]->f[x][y][fx '], which is equivalent to changing the direction of motion. In addition, by exchanging white blocks and special blocks, it is equivalent to taking one step.
A Bigzao question: that for the special block and the white block is not adjacent to the movement, no implementation ah.
Note: Changing direction actually results in non-adjacent movement, so the implementation is in preprocessing.
In this way, the block is abstracted to a point, and the shortest distance can be achieved by short circuit.
So for each inquiry, a violent start moves the white block to the four azimuth of the special block, and then follows the above connected edge. Running the shortest way again will solve the problem. O (n2∗m2∗16+q∗n∗m) O (n^2*m^2*16+q*n*m) Code

#include <cstdio> #include <queue> #include <cstring> #include <algorithm> #define FO (i,a,b) for
(int i=a;i<=b;i++) #define NUM (i,j) ((i-1) *m+ (j)) #define F (I,j,k) (k+4* (Num (i,j)-1)) using namespace Std;
const int n=35,m=905,v=m*4,e=v*5,inf=2139062143, fx[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int n,m,s,q1[m*m][2],q2[m*m][2],data[m*m],c[m][4][4];
int TOT,TO[E],NEXT[E],WEI[E],LAST[V],DIS[V];
BOOL Map[n][n],bz[n][n],bz1[v],p[v];
Queue<int> Q;
    int BFS (int xs,int ys,int xt,int yt,int x0,int y0) {if (xs==xt && Ys==yt) return 0;
    Q1[1][0]=xs,q1[1][1]=ys;
    data[1]=0;
    memset (bz,0,sizeof (BZ));
    Bz[xs][ys]=1;
    int l=0,r=1;
        while (l<r) {int x=q1[++l][0],y=q1[l][1];
            Fo (i,0,3) {int xx=x+fx[i][0],yy=y+fx[i][1];
            if (!map[xx][yy] | | bz[xx][yy]) continue;
            if (xx==x0 && yy==y0) continue;
            data[++r]=data[l]+1; if (xx==xt && yy==yt) return data[r];
            Q1[r][0]=xx,q1[r][1]=yy;
        Bz[xx][yy]=1;
}} return-1;
    } void link (int u,int v,int W) {to[++tot]=v;
    Wei[tot]=w;
    Next[tot]=last[u];
    Last[u]=tot;
if (tot&1) link (v,u,w);
    } void Spfa () {memset (dis,127,sizeof (dis));
    memset (bz1,0,sizeof (BZ1));
    dis[s]=0;
    Bz1[s]=1;
    Q.push (S);
        while (!q.empty ()) {int U=q.front (); Q.pop ();
            for (int i=last[u];i;i=next[i]) {int v=to[i];
                if (Dis[u]+wei[i]<dis[v]) {dis[v]=dis[u]+wei[i];
                    if (!bz1[v]) {Q.push (v);
                Bz1[v]=1;
    }}} bz1[u]=0;
    }} int main () {int _,x0,y0,xs,ys,xt,yt;
    scanf ("%d%d%d", &n,&m,&_);
    Fo (i,1,n) fo (j,1,m) scanf ("%d", &map[i][j]); Fo (i,1,n) fo (j,1,m) fo (k,0,3) {int i1=i+fx[k][0],J1=J+FX[K][1];
                if (!map[i1][j1]) continue;
                        FO (l,0,3) if (k!=l) {int i2=i+fx[l][0],j2=j+fx[l][1];
                        if (!map[i2][j2]) continue;
                    C[num (i,j)][k][l]=bfs (I1,J1,I2,J2,I,J);
    }} s=0;
        while (_--) {scanf ("%d%d%d%d%d%d", &x0,&y0,&xs,&ys,&xt,&yt);
            if (xs==xt && ys==yt) {printf ("0\n");
        Continue
        } tot=0;
        Memset (Last,0,sizeof (last));
        memset (P,0,sizeof (p));
            Fo (i,0,3) {int x=xs+fx[i][0],y=ys+fx[i][1];
            if (!map[x][y]) continue;
            int W=bfs (X0,y0,x,y,xs,ys);
            if (w!=-1) Link (s,f (xs,ys,i), W);
        P[f (xs,ys,i)]=1; } fo (i,1,n) fo (j,1,m) fo (k,0,3) {int i1=i+fx[K][0],J1=J+FX[K][1];
                    if (!map[i1][j1]) continue;
                    Link (f (i,j,k), F (i1,j1,k^1), 1);
                    P[f (i,j,k)]=p[f (i1,j1,k^1)]=1;
                        FO (l,0,3) if (k!=l) {int i2=i+fx[l][0],j2=j+fx[l][1];
                        if (!map[i2][j2]) continue;

                        if (C[num (i,j)][k][l]!=-1) Link (f (i,j,k), F (i,j,l), C[num (I,J)][k][l]);

                    P[f (i,j,k)]=p[f (i,j,l)]=1;
        }} SPFA ();
        int ans=inf;
        FO (i,0,3) if (P[f (xt,yt,i)) Ans=min (Ans,dis[f (xt,yt,i)]);
    printf ("%d\n", Ans==inf?-1:ans);
} return 0; }

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.