"BFS" The Devil's City _struct

Source: Internet
Author: User
Problem description
in a rectangular demon city divided into N*m square rooms, an explorer must follow the following rules in order to jump into action. He must enter from (1, 1), from (N, M); a magical number is written on the walls of each room, the natural number within the 1~13; the Explorer can imagine any one of the 8 directions (horizontal or vertical or diagonal), Then he can make a space jump through the continuous x room of the side up, where x is the magic number of his original room. But if the number of rooms on this side is less than x, then he does not make any jumps, but must imagine another direction. At the same time, explorers are not allowed to jump in the same direction twice in a row.
	1	2	3	4	5
1	3	3 6 7 One 2 3 2 1-1 3 3 3 2 2 1 1 4 2 1 2 2-	1
For example in the 5*4 of the upper figure of the Devil's Town, If the Explorer is now in the position of (3, 3), then by jumping in space he can reach one of the following rooms: (1, 1), (3, 1), (1, 3), (5, 1), or (5, 3). In addition, if he is going to jump from (5, 4) to (3, 2) with two jumps, he cannot jump first (4, 3) (because it will be the same direction as the first time, which is not allowed). So he had to jump first (2, 1).
please write a program for a given map to figure out how many steps the explorers need to jump to get out of the Devil's city.
the "input"
	line gives the n,m (none more than);
	down there are M rows, N natural numbers per action, representing the magic numbers in the room.
"Output" the
	minimum number of steps, if the explorers can not leave the city of Devil, please output "NEVER".
"Sample"
	pils.in						pils.out
	5 4							4
	3 3
	6 7 One 3 2 1 1 3 3 2 2 1 1 2 1 2 2-1

50 points at the time of the exam.

And then he did it reluctantly, only 90 points.

It seems to be the only way to create your own mystery.


#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include < cstring> #include <cmath> #include <ctime> #define LOC #define MXN 100+10 #define MXQ 200000 using NAMESP


Ace STD;
const int MX[10]={0,1,0,-1,0,1,1,-1,-1};

const int MY[10]={0,0,-1,0,1,1,-1,1,-1};
    struct Nd {int x,y;   
int stp,lst;
}Q[MXQ];

int l,r;
int n,m;
int MP[MXN][MXN];


BOOL V[mxn][mxn][9];
    inline void BFs () {int y,x;  
    L=r=1;
    q[r].x=q[r].y=1,q[r].stp=0,q[r].lst=0;
    
    for (int i=1;i<=8;++i) v[1][1][i]=true; while (L<=r) {for (int i=1;i<=8;++i) if (i!=q[l].lst) {x=q[l].x+mx[i]*mp[q
            [L].X][Q[L].Y];
            
            Y=Q[L].Y+MY[I]*MP[Q[L].X][Q[L].Y];
                if (x==n&&y==m) {printf ("%d", Q[R].STP);
            Exit (0); } if (!v[x][y][i]) {V[X][Y][I]=TRUe
               R= (r+1)%mxq;
               q[r].stp=q[l].stp+1;
               q[r].lst=i;
            Q[r].x=x,q[r].y=y;   
    } l= (l+1)%mxq;
    int main () {#ifdef loc freopen ("pils.in", "R", stdin);
    Freopen ("Pils.out", "w", stdout);
    #endif scanf ("%d%d", &m,&n);
    
    for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) scanf ("%d", &mp[i][j]);
    
    BFS ();
    printf ("NEVER");
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.