It is important to note that although stairs are sometimes not possible, we can wait in front of the stairs and wait until the staircase reaches the state we need, but it is also possible without a priority queue. Just think about it for a long time.
#include <iostream> #include <queue> #include <cstring>int m,n;int Tx,ty,sx,sy;char mapp[21][21];int Visit[21][21];int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; struct stu{int x,y,t;}; Using namespace Std;void BFs () {Queue<stu>root;stu x,y;x.x=sx;x.y=sy;x.t=0;root.push (x); visit[x.x][x.y]=1; while (Root.size ()) {X=root.front ();//cout<<x.x<< ' <<x.y<<endl;root.pop (); if (mapp[x.x][ x.y]== ' T ') {Cout<<x.t<<endl;return;} if (x.x+1>=0&&x.x+1<m&&visit[x.x+1][x.y]==0) {y.x=x.x+1;y.y=x.y;y.t=x.t+1;if (mapp[y.x][y.y ]=='.'|| mapp[y.x][y.y]== ' T ') Root.push (y), visit[y.x][y.y]=1;else if (mapp[y.x][y.y]== ' | ' && (y.t-1)%2==0) | | (mapp[y.x][y.y]== '-') && (y.t-1)%2==1) {y.x++;if (y.x+1>=0&&y.x<m&&visit[y.x][y.y]== 0) Root.push (y), visit[y.x][y.y]=1,y.x--;} else if (mapp[y.x][y.y]!= ' * ') {Y.x=x.x;y.y=x.y;y.t=x.t+1;root.push (y);}} if (x.x-1>=0&&x.x-1<m&&visit[x.x-1][x.y]==0) {y.x=x.x-1;y.y=x.y;y.t=x.t+1;if (mapp[y.x][y.y]== '. ' | | mapp[y.x][y.y]== ' T ') Root.push (y), visit[y.x][y.y]=1;else if (mapp[y.x][y.y]== ' | ' && (y.t-1)%2==0) | | (mapp[y.x][y.y]== '-' && (y.t-1)%2==1)) {y.x--;if (y.x>=0&&y.x<m&&visit[y.x][y.y]==0) Root.push (y), visit[y.x][y.y]=1,y.x++;} else if (mapp[y.x][y.y]!= ' * ') {Y.x=x.x;y.y=x.y;y.t=x.t+1;root.push (y);}} if (x.y-1>=0&&x.y-1<n&&visit[x.x][x.y-1]==0) {y.x=x.x;y.y=x.y-1;y.t=x.t+1;if (mapp[y.x][y.y ]=='.'|| mapp[y.x][y.y]== ' T ') Root.push (y), visit[y.x][y.y]=1;else if (mapp[y.x][y.y]== '-' && (y.t-1)%2==0) | | (mapp[y.x][y.y]== ' | ') && (y.t-1)%2==1) {y.y--;if (y.y>=0&&y.y<n&&visit[y.x][y.y]==0) Root.push (y), visit[ y.x][y.y]=1,y.y++;} else if (mapp[y.x][y.y]!= ' * ') {Y.x=x.x;y.y=x.y;y.t=x.t+1;root.push (y);}} if (x.y+1>=0&&x.y+1<n&&visit[x.x][x.y+1]==0) {y.x=x.x;y.y=x.y+1;y.t=x.t+1;if (mapp[y.x][y.y ]=='.'|| mapp[y.x][y.y]== ' T ') Root.push (y), visit[y.x][y.y]=1;else if (mapp[y.x][y.y]== '-' && (y.t-1)%2==0) | | (mapp[y.x][y.y]== ' | ') && (y.t-1)%2==1) {y.y++;if (y.y>=0&&y.y<n&&visit[y.x][y.y]==0) Root.push (y), visit[ y.x][y.y]=1,y.y--;} else if (mapp[y.x][y.y]!= ' * ') {Y.x=x.x;y.y=x.y;y.t=x.t+1;root.push (y);}}} }int Main () {while (cin>>m>>n) {if (m==0&&n==0) {Cout<<0<<endl;} for (int i=0;i<m;i+ +) {for (int j=0;j<n;j++) {cin>>mapp[i][j];if (mapp[i][j]== ' T ') tx=i,ty=j;if (mapp[i][j]== ' S ') Sx=i,sy=j;}} memset (visit,0,sizeof (visit)); BFs ();} return 0;}
HDU 1180 Weird Stairs.