noip2013 Huarong Road

Source: Internet
Author: User

P1979 Huarong Road
      • 131 through
      • 471 Submit
    • Topic provider The user does not exist
    • Tag graph theory search/Enumerate Noip raise group
    • Difficulty Saving/noi-

Submit a discussion of the problem record

Recent discussions
    • Be aware that some data has a starting point and end ...
    • Ask for help help
    • Is there a problem with the title data?
Title Description

"Problem 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:

    1. 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;

    2. Some pieces are fixed, some pieces can be moved;

    3. 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. I-th

When playing, the blank grid in column EXi, eyi, the initial position of the specified movable pawn is the first SXi row syi column, the target location is TXi row, tyi column.

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.

Input output Format input format:

The input file is puzzle.in.

The first line has 3 integers, each two integers separated by a space, which in turn represents N, M, and Q;

The next n lines describe a n*m checkerboard with m integers per line, separated by a space between each of the two integers, each integer describing the state of a lattice on the board, 0 indicating that the piece on the grid is fixed, and 1 means that the piece on the grid can move or the grid is blank. The next Q line, each line containing 6 integers is EXi, eyi, SXi, SYi, TXi, tyi, each two integers separated by a space, indicating the position of each game blank lattice, specify the original position and target position of the pawn.

Output format:

The output file name is Puzzle.out.

The output has q lines, each line contains 1 integers, indicating the minimum time required for each game, and output −1 if a game fails to complete the target.

Input and Output Sample input example # #:
3 4 20 1 1 10 1 1 00 1 0 03 2 1 2 2 21 2 2 2 3 2
Sample # # of output:
2-1
Description

"Input and Output sample description"

The grid on the board is fixed, the red lattice is the target position, the circle represents the pawn, and the green circle represents the target piece.

    1. For the first time, the initial position of the blank lattice is (3, 2) (shown in blank), and the goal of the game is to move the original position (1, 2) to the target position (2, 2) (the red lattice in the picture).

The move process is as follows:

    1. The second game, the initial position of the blank lattice is (1, 2) (shown in the blanks), the goal of the game is to move the original position on (2, 2) on the piece (shown in the green circle) to the target position (3, 2).

To move the specified block into the target position, you must first move the blank block to the target position, and the blank block to move to the target position, which is bound to be swapped from position (2, 2) with the pawn at the target position in the current diagram, and then to swap the position with the blank block only at the target position in the current diagram. So the target piece can never go to its target position, the game has no

Method is completed.

"Data Range"

For 30% of data, 1≤n, m≤10,q = 1;

For 60% of data, 1≤n, m≤30,q≤10;

For 100% of data, 1≤n, m≤30,q≤500.

Analysis: You can find the size of the n,m for any data range is the same, Q of the range is constantly getting bigger, and see this problem will think of BFS, unfortunately only over 60% of the data, 60 points for me already a lot, if in the examination room I certainly write 60 points explode. BFS can be optimized into two-way BFS, but difficult to write, and seems to only increase by 10 points? The reason for tle is that it repeats too much data, and according to the principle of memory, we can record the calculated data, and for this problem, because the n,m will not change, we just need to record the public and useful data.

Target block If you want to move to the target location, it must be connected with the blank box, or the target block cannot be moved. Then we first move the blank block around the target block, each block if you want to move to the adjacent position, it must be related to the position of the blank square, set movee[i][j][k][h] as ( I,J) moves in the direction of H, the blank block moves in the direction of the K. can be seen as the distance of the movement of the blank Block +1 (because (i,j) to move), then you can use BFS to find out, the result of +1, but note that (I,J) can not be moved, so that the effective and common data. As you can imagine, each move of the blank block and the target block is together, and can be abstracted into a block that is bound together. So in order to find out the moving steps of this "block" to the target position, we can use the Movee array previously obtained to optimize the calculation. And the SPFA algorithm can be used. It is important to note that after moving the blank block is opposite to the position of the previous blank block relative to the target block.

#include <cstdio>#include<cstring>#include<queue>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN = *, INF =0x3f3f3f3f;intN, M, Q, MAP[MAXN][MAXN], ex, EY, TX, Ty, SX, sy,ans,movee[maxn][maxn][5][5],step[maxn][maxn],vis[maxn][maxn],step2[maxn][maxn][5],vis2[maxn][maxn][5];structnode{intx, Y, K;};intFanintk) {    if(k = =1)        return 2; if(k = =2)        return 1; if(k = =3)        return 4; if(k = =4)        return 3;} Node Zhuanyi (Node A,intb) {Node T=A; if(b = =1) T.x--; if(b = =2) T.x++; if(b = =3) T.y--; if(b = =4) T.y++; returnt;}intBFS (node s, node T) {if(!map[s.x][s.y] | |!Map[t.x][t.y])returninf; memset (step,0x3f,sizeof(step)); memset (Vis,0,sizeof(VIS)); Queue<node>Q;    Q.push (s); STEP[S.X][S.Y]=0; VIS[S.X][S.Y]=1;    Node u,v;  while(!Q.empty ()) {u=Q.front ();        Q.pop ();  for(intK =1; K <=4; k++) {v=Zhuanyi (U, k); if(!vis[v.x][v.y] &&Map[v.x][v.y]) {VIS[V.X][V.Y]=1;                Q.push (v); STEP[V.X][V.Y]= Step[u.x][u.y] +1; }        }    }    returnStep[t.x][t.y];}voidinit () {memset (Movee,0x3f,sizeof(Movee));  for(inti =1; I <= N; i++)         for(intj =1; J <= M; J + +)        {            if(!Map[i][j])Continue; MAP[I][J]=0;//Place move target block             for(intK =1; K <=4; k++)                 for(inth =1; H <=4; h++)                {                    if(H <k) {Movee[i][j][k][h]=Movee[i][j][h][k]; Continue; } node T1= Zhuanyi ((node) {i, J}, k), t2 =Zhuanyi (node) {i, J}, H); if(!map[t1.x][t1.y] | |!Map[t2.x][t2.y])Continue; MOVEE[I][J][K][H]= BFS (t1, T2) +1; } Map[i][j]=1; }}intSPFA (node s, node T) {if(s.x = = T.x && S.y = =t.y)return 0; memset (Step2,0x3f,sizeof(STEP2)); memset (Vis2,0,sizeof(VIS2)); if(!map[s.x][s.y] &&!Map[t.x][t.y])returninf; MAP[S.X][S.Y]=0; Queue<node>Q;  for(inti =1; I <=4; i++) {node T=(node) {s.x, s.y, i};        Q.push (t); Vis2[s.x][s.y][i]=1; Step2[s.x][s.y][i]=BFS (node) {Ex, EY}, Zhuanyi (S, i)); } Map[s.x][s.y]=1;  while(!Q.empty ()) {Node U=Q.front ();        Q.pop (); VIS2[U.X][U.Y][U.K]=0;  for(inti =1; I <=4; i++) {node v=Zhuanyi (U, i); V.K=fan (i); if(STEP2[U.X][U.Y][U.K] + Movee[u.x][u.y][u.k][i] <STEP2[V.X][V.Y][V.K]) {STEP2[V.X][V.Y][V.K]= STEP2[U.X][U.Y][U.K] +Movee[u.x][u.y][u.k][i]; if(!VIS2[V.X][V.Y][V.K])                    {Q.push (v); VIS2[V.X][V.Y][V.K]=1; } }}} ans=inf;  for(inti =1; I <=4; i++) ans=min (ans, step2[t.x][t.y][i]); returnans;}intMain () {scanf ("%d%d%d", &n, &m, &q);  for(inti =1; I <= N; i++)         for(intj =1; J <= M; J + +) scanf ("%d", &Map[i][j]);    Init ();  for(inti =1; I <= Q; i++) {scanf ("%d%d%d%d%d%d", &ex, &ey, &sx, &sy, &AMP;TX, &ty); Ans=SPFA (node) {SX, sy}, (node) {tx, ty}); if(Ans <inf) printf ("%d\n", ans); Elseprintf ("-1\n"); }    return 0;}

noip2013 Huarong Road

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.