Hdoj/hdu 1180 quirky staircase (classic bfs-)

Source: Internet
Author: User

Problem Description
After Hogwarts officially opened, Harry found that in Hogwarts, some staircases were not stationary, but instead they changed direction every minute.
In the example below, the staircase begins in a vertical direction, and a minute later it moves horizontally, and in a minute it returns to the vertical direction. Harry found that it was hard for him to find a route that would make him the quickest destination, when Ron (Harry's best friend) told Harry that there was a magic prop that would help him find the route, and that the spell on the Magic prop was written by you.

Input
There are several sets of test data, each of which is stated as follows:
The first line has two numbers, M and N, followed by a M-row N-column map, ' * ' denotes an obstacle, '. ' Show Corridor, ' | ' Or '-' represents a staircase and indicates where it was in the first place: ' | ' The staircase represents the vertical direction at the very beginning, and '-' indicates that the staircase is in the horizontal direction at the beginning. There is also an ' S ' in the map that is the starting point, ' T ' is the target, 0<=m,n<=20, and there are no two connected ladders in the map. Harry can only stay in '. ' Every second. or ' S ' and ' T ' in the squares labeled.

Output
A single row containing a number T, indicating the shortest time to reach the target.
Note: Harry can only walk to a neighboring lattice and not take a diagonal walk, each move happens to be one minute, and Harry takes a minute to get to the stairs and across the stairs, and Harry never stops on the stairs. And every time the stairs happened, he just changed direction after Harry moved. .

Sample Input

5 5**..T**.*...|...*.*.S....

Sample Output

7

Hint
Hint

The map is as follows:


Very classic a wide-search topic!
Test instructions is very well understood. When doing a clear idea, pay attention to the direction of the staircase, the direction of the staircase can be reached by the time of the odd couple to judge. Because the stairs only have 2 directions! Up and down, or around.
Move to the back of the staircase also to determine whether the cross-border, the staircase is not the wall, is not already traversed, these 3 conditions, to meet the past.
If the staircase to the direction of the past and the direction of different, you can only wait a minute, at this time, the position is not moving, time plus 1! into the queue again. This order is important!
You cannot go past, then time adds 2, then into the queue. This will be hyper-memory, which is why many people use priority queues!
(Because of this, you will find that the pop-up of the node-type variable t will appear in the short-t-time will pop up in the back, so that increased memory use!!!) )

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace STD;structnode{intX,y,t;};intN,m;intmx[]={0,1,0,-1};intmy[]={1,0,-1,0};//Right, bottom, left, topChar Map[ -][ -];intd[ -][ -];intTx,ty,sx,sy;node ft;intJudgeintXintY) {if(x<0|| x>=n| | y<0|| Y&GT;=M) {return 0; }if(D[x][y]) {return 0; }if(Map[x] [y]==' * '){return 0; }return 1;}voidBFS () { queue<node>Q    FT.X=SX;    Ft.y=sy; ft.t=0;    Q.push (FT); d[sx][sy]=1; while(!q.empty ())        {node Ff=q.front (); Q.pop ();intx=ff.x;intY=FF.Y;if(Map[x] [y]==' T '){printf("%d\n", ff.t);return; } for(intI=0;i<4; i++) {x=ff.x+mx[i]; Y=ff.y+my[i];if(!judge (x, y)) {Continue; } node NT;if(Map[x] [y]=='. '||Map[x] [y]==' T '){//Normal walkingNt.x=x;                Nt.y=y; nt.t=ff.t+1; d[nt.x][nt.y]=1; Q.push (NT);Continue; }Else if(i==0|| i==2){//Right and left                if(Map[x] [y]=='-'&&ff.t%2==0){//Can passNt.x=x+mx[i]; Nt.y=y+my[i];if(Judge (NT.X,NT.Y)) {nt.t=ff.t+1; d[nt.x][nt.y]=1;                    Q.push (NT); }                }Else if(Map[x] [y]==' | '&&ff.t%2!=0) {Nt.x=x+mx[i]; Nt.y=y+my[i];if(Judge (NT.X,NT.Y)) {nt.t=ff.t+1; d[nt.x][nt.y]=1;                    Q.push (NT); }                }Else{//Can't go over stairs, wait a minutent.x=ff.x;                    NT.Y=FF.Y; nt.t=ff.t+1; d[nt.x][nt.y]=1;                Q.push (NT); }            }Else{//Up and down                if(Map[x] [y]==' | '&&ff.t%2==0){//Can passNt.x=x+mx[i]; Nt.y=y+my[i];if(Judge (NT.X,NT.Y)) {nt.t=ff.t+1; d[nt.x][nt.y]=1;                    Q.push (NT); }                }Else if(Map[x] [y]=='-'&&ff.t%2!=0) {Nt.x=x+mx[i]; Nt.y=y+my[i];if(Judge (NT.X,NT.Y)) {nt.t=ff.t+1; d[nt.x][nt.y]=1;                    Q.push (NT); }                }Else{//Can't go over stairs, wait a minutent.x=ff.x;                    NT.Y=FF.Y; nt.t=ff.t+1; d[nt.x][nt.y]=1;                Q.push (NT); }            }        }    }}intMain () { while(~scanf("%d%d", &n,&m)) {memset(d,0,sizeof(d)); for(intI=0; i<n;i++) {scanf('%s ',Map[i]); for(intj=0; j<m;j++) {if(MapI [j]==' S ') {sx=i,sy=j;    }}} BFS (); }return 0;} Test data:/*3 3s|. -.. T.. 5 5**. t**.*...| ...*.*. S .... 3 4s|.| -T-.. |. 20s.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.| .|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|. |.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|.||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.| .||.|.|.|.|.|.|.|.|.|..|.|.|.|.|.|.|.|.|. T2 7 7 20*/

Hdoj/hdu 1180 Spooky staircase (classic bfs-)

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.