Weird stairs.
Time limit:2000/1000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 10678 Accepted Submission (s): 2662
After problem Descriptionhogwarts 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.
There are several sets of input test data, each of which is expressed 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 has only one row and contains 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
7HintHint map is as follows:
Sourcegardon-dygg Contest 1
/* A little different from the direct shortest path is that his ladder is not allowed to pass in the wrong direction. The change of State is the position of man. For ladders, there can be when you can pass, let a person through, that is, the ladder as a time-consuming, in the same direction to move another step. You can do it with SPFA. */#include <iostream> #include <stdio.h> #include <cstring> #include <algorithm> #include < Queue> #include <map>using namespace std;const int maxn = 25;int N, m;int SX, Sy, EX, Ey;int Fx[]={1,0,-1,0,1};cha R ditu[maxn][maxn];int pay[maxn][maxn];void Init () {for (Int. i=0; i<maxn; i++) {for (int j=0; j<maxn; j+ +) {ditu[i][j]= ' * '; }} memset (Pay,-1,sizeof pay);} struct node{int x, y; int pay;}; void BFs () {Node A, B; a.x = SX; A.Y = sy; A.pay = 0; PAY[A.X][A.Y] = A.pay; queue<node>q; Q.push (a); while (! Q.empty ()) {a = Q.front (); Q.pop (); for (int i=0; i<4; i++) {b.x = a.x + fx[i]; B.Y = A.y + fx[i+1]; B.pay = A.pay + 1; The wall cannot walk if (ditu[b.x][b.y]== ' * ') continue; if (ditu[b.x][b.y]=='.') {if (pay[b.x][b.y] = = 1 | | pay[b.x][b.y] > B.pay) {pay[b.x][b.y] = b . Pay; Q.push (b); }}else//if (ditu[b.x][b.y]== ' | ') {int flag = ditu[b.x][b.y]== ' | '? 0:1; Take one more step b.x + = Fx[i]; B.y + = fx[i+1]; Vertical direction if ((Flag+b.pay)%2==1) {//go from left to right for one second if (I==1 | | i== 3) {b.pay++; }}else{//Changed "-"//from up and down for one second if (i==0 | | i==2) { b.pay++; There are no two connected ladders in the map, just consider "." and "*", "." can go if (ditu[b.x][b.y]== '. ') {if (pay[b.x][b.y] = = 1 | | pay[b.x][b.y] > B.pay) { PAY[B.X][B.Y] = B.pay; Q.push (b); }}}}}}int main () {int i, J; while (cin>>n>>m) {init (); For (I=1, i<=n; i++) {for (j=1; j<=m; J + +) {cin>>ditu[i][j]; if (ditu[i][j]== ' S ') {sx = i; sy = j; DITU[I][J] = '. '; }else if (ditu[i][j]== ' T ') {ex = i; EY = j; DITU[I][J] = '. '; }}} BFS (); printf ("%d\n", Pay[ex][ey]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1180 weird Stairs.