诡异的楼梯
#include <iostream> #include <queue> #include <fstream> using namespace std; struct posiT { int x,y,n; }; int steps[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int main() { queue<posiT> que; char map[22][22],what; int m,n,min=999,newx,newy,add_step; cin>>m>>n; posiT start,end,newtemp,temp; for(int i=0;i<=m+1;i++) { for(int j=0;j<=n+1;j++) { if(i>=1&&i<=m&&j>=1&&j<=n) { cin>>map[i][j]; if(map[i][j]=='S') { start.x=i;start.y=j; } if(map[i][j]=='T'){end.x=i;end.y=j;} } else { map[i][j]='*'; } } } start.n=0; que.push(start); while(!que.empty()) { temp=que.front(); que.pop(); if(temp.x==end.x&&temp.y==end.y) { if(temp.n<min)min=temp.n; } else{ for(int i=0;i<4;i++) { newx=temp.x+steps[i][0];newy=temp.y+steps[i][1]; if(map[newx][newy]=='.'||map[newx][newy]=='T') { newtemp.x=newx; newtemp.y=newy; newtemp.n=temp.n+1; que.push(newtemp); map[temp.x][temp.y]='*'; } // steps[0] steps[1] 代表左右, steps[2] step[3]上下 else if(map[newx][newy]=='|'||map[newx][newy]=='-') { add_step=0; what=map[newx][newy]; if(temp.n%2==1) { if(what=='|')what='-'; else{what='|';} } if((i>1&&what=='|')||(i<=1&&what=='-'))add_step=1; newx+=steps[i][0];newy+=steps[i][1]; if(map[newx][newy]=='.'||map[newx][newy]=='T') { newtemp.x=newx; newtemp.y=newy; newtemp.n=temp.n+1+add_step; que.push(newtemp); map[temp.x][temp.y]='*'; } } } } } cout<<min; }