[RT] [NOIP2013] Huarong Road

Source: Internet
Author: User

1, the problem surface

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

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 ex_i, ey_i, Sx_i, Sy_i, Tx_i, ty_i, 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.

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.

2. Summary

Temporarily not write AC Ah, after a few years again use this problem to try the next Noip in the BFS, a little bit not smooth, directly with four-dimensional marker array to record the state, but will TLE20 points, so only 80 points. Ask how it is AC.

3, code (80 points)

#include <cstdio>#include<cstring>#defineMAXN 35Const intvx[4] = {0,0,1, -1}, vy[4] = {1, -1,0,0};intN, M, T, A[MAXN][MAXN], VIS[MAXN][MAXN][MAXN][MAXN];intex, EY, SX, Sy, TX, Ty;structQueue {intx, y, Ox, Oy, D;} Q[MAXN* MAXN * MAXN *MAXN];intBFS () {inth =1, t =2; q[1] = (Queue) {Ex, EY, SX, SY,0};  while(H! =t) { for(inti =0; I <=3; i++) {            intNX = q[h].x + vx[i], NY = q[h].y +Vy[i]; if(NX = = Q[h].ox && NY = =Q[h].oy) {Q[t].ox= q[h].x, Q[t].oy =q[h].y; if(Q[t].ox = = TX && Q[t].oy = = ty)returnQ[H].D +1; }            ElseQ[t].ox = Q[h].ox, Q[t].oy =Q[h].oy; if(!a[nx][ny] | | vis[nx][ny][q[t].ox][q[t].oy])Continue; Vis[nx][ny][q[t].ox][q[t].oy]=1; q[t].x= NX, Q[t].y = ny, Q[T].D = Q[H].D +1; T++; } h++; }    return-1;}intMain () {Freopen ("1.in","R", stdin); Freopen ("1.out","W", stdout); scanf (" %d%d%d", &n, &m, &t);  for(inti =1; I <= N; i++)         for(intj =1; J <= M; J + +) scanf ("%d", &A[i][j]);  for(inti =1; I <= t; i++) {memset (Vis,0,sizeof(VIS)); scanf ("%d %d%d%d%d%d", &ex, &ey, &sx, &sy, &AMP;TX, &ty); Vis[ex][ey][sx][sy]=1; printf ("%d\n", SX = = TX && Sy = = ty?0: BFS ()); }    return 0;}

[RT] [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.