Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1180
Weird stairs.
Time limit:2000/1000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 10827 Accepted Submission (s): 2694
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:
Started WA a lot of times, the original is test instructions didn't understand, still think only example of ' | ' This kind of stair sign, original also '-'!
Key points: Construction of the priority queue, determination of condition extraction, special conditions (wait one second in advance)
Priority queue, which makes the current time a small first-out queue,
So the structure of the current coordinate needs to be written like this
struct Node{int x,y,step; Two ways to overload less than #/friend bool operator < (node A,node b) {return a.step>b.step;} */bool operator < (const node &a) CO Nst{return step>a.step;//Step number Small first out queue}}init;
Because the priority queue is used, it is no longer possible to use the traditional vis tag to walk through, for a position that has been traversed, if the current step is less than the previous step, you can repeat
So use statue[][] this array to store the current number of steps passing the position;
Judging a lot of conditions, we extract it to the function judge
BOOL Judge (node No) {//If the boundary is exceeded, or if the position is a wall, or if the current number of steps is greater than the previous step, False if (no.x<0| | no.x>=n| | no.y<0| | no.y>=m| | map[no.x][no.y]== ' * ' | | (Statue[no.x][no.y]&&no.step>=statue[no.x][no.y])) return false; return true;}
If the direction of the staircase is inconsistent with the forward direction, you can wait one second and then move forward.
Put this situation together with the same situation, only the number of steps is different
"Source Code"
#include <iostream> #include <queue> #include <cstring>using namespace std;int n,m;const int maxn = 21; int Stx,sty,gx,gy;char map[maxn][maxn];bool vis[maxn][maxn];int statue[maxn][maxn];int dx[4]={0,0,1,-1};int dy[4]={- 1,1,0,0};struct node{int x,y,step;/* friend bool operator < (node A,node b) {return a.step>b.step;} */bool operator &L t; (const node &a) Const{return step>a.step;//Step Small first-out queue}}init;bool judge (node No) {//If the boundary is out of bounds, or the location is a wall, or the current step is greater than the number of previous steps , Return to Falseif (no.x<0| | no.x>=n| | no.y<0| | no.y>=m| | map[no.x][no.y]== ' * ' | | (Statue[no.x][no.y]&&no.step>=statue[no.x][no.y])) return False;return true;} int BFs () {Init.x=stx;init.y=sty;init.step=0;statue[init.x][init.y]=1;priority_queue<node>pq;while (! Pq.empty ()) Pq.pop ();p Q.push (init), while (!pq.empty ()) {node past=pq.top (), node Now;pq.pop (); for (int i=0;i<4;i++) {now.x=past.x+dx[i];now.y=past.y+dy[i];now.step=past.step+1;if (!judge (now)) continue;if (map[now.x][now.y]== ' | ') {if (now.x==past.x&& (PASt.step & 1) ==0)//When walking sideways, and | Invariant now.step++;if (now.y==past.y&& (Past.step & 1) ==1)//when walking vertically and | changing {now.step++;} Now.x+=dx[i]; Now.y+=dy[i];} else if (map[now.x][now.y]== '-') {if (now.x==past.x&& (Past.step & 1) ==1)//When walking sideways, and-change now.step++;if (now.y== past.y&& (Past.step & 1) ==0)//When vertical walk and-unchanged now.step++;now.x+=dx[i]; Now.y+=dy[i];} if (!judge (now)) continue;if (map[now.x][now.y]== ' T ') return Now.step;statue[now.x][now.y]=now.step;pq.push (now);}} return 0;} int main () {while (cin>>n>>m) {for (Int. i=0;i<n;i++) for (int j=0;j<m;j++) {cin>>map[i][j];if ( map[i][j]== ' S ') stx=i,sty=j;if (map[i][j]== ' T ') gx=i,gy=j;} memset (statue,0,sizeof (statue)); int Ans=bfs (); Cout<<ans<<endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1180 Spooky Staircase (bfs+ priority queue)