5 5 **..T **.*. ..|.. .*.*. S....
7地图如下:HintHint这道深搜题,让我费了一上午时间,题半个小时就解出来了,就是WA, 看别人的报告,大体应该没错,可是就是过不了啊, 最后我在源代码里加了一句话,就AC了,内牛满面ING 我出错的地方就是在判断楼梯状态,来决定是否需要等待一分钟时,楼梯状态的步数少了1, 楼梯转换时间应该是人踩在楼梯上时的时间,而不是下一步将碰到楼梯时的时间。(好绕啊,不知道懂了没。。。)
#include <iostream> #include <queue> using namespace std; struct Coordinate { int x,y,tm; }; char map[25][25]; int visit[25][25]; int dis[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int m,n,sx,sy,fx,fy,tme; // 判断是否越界 bool isout(int x,int y) { if(x<0 || x>=m || y<0 || y>=n) return 1; if(visit[x][y]==1) return 1; if(map[x][y]==‘*‘) return 1; return 0; } // 判断是否需要等待楼梯变向 bool isgo(Coordinate t,char x,int n) { t.tm+=1; // 就是这句话= 。= if(x==‘|‘) { if(n%2==0 && t.tm%2==0) return 1; if(n%2==1 && t.tm%2==1) return 1; return 0; } else if(x==‘-‘) { if(n%2==1 && t.tm%2==0) return 1; if(n%2==0 && t.tm%2==1) return 1; return 0; } } void bfs() { memset(visit,0,sizeof(visit)); queue <Coordinate> cd; Coordinate p,next; int i; p.x=sx;p.y=sy;p.tm=0; visit[p.x][p.y]=1; cd.push(p); while(!cd.empty()) { p=cd.front(); cd.pop(); // 判断是否到了终点 if(p.x==fx && p.y==fy) {tme=p.tm;return;} // 四个方向遍历 for(i=0;i<4;++i) { next.x=p.x+dis[i][0]; next.y=p.y+dis[i][1]; //是否越界 if(isout(next.x,next.y)) continue; else if(map[next.x][next.y]==‘.‘) { next.tm=p.tm+1; visit[next.x][next.y]=1; cd.push(next); } else // 如果是楼梯 { // 楼梯对面是否访问过 if(isout(next.x+dis[i][0],next.y+dis[i][1])) continue; if(isgo(p,map[next.x][next.y],i)) { next.x+=dis[i][0]; next.y+=dis[i][1]; next.tm=p.tm+1; visit[next.x][next.y]=1; cd.push(next); } else { next.x=p.x; next.y=p.y; next.tm=p.tm+1; cd.push(next); } } } } } int main() { int i,j; while(cin>>m>>n) { for(i=0;i<m;++i) for(j=0;j<n;++j) { cin>>map[i][j]; if(map[i][j]==‘S‘) {map[i][j]=‘.‘;sx=i;sy=j;} if(map[i][j]==‘T‘) {map[i][j]=‘.‘;fx=i;fy=j;} } bfs(); cout<<tme<<endl; } return 0; }
原文:http://blog.csdn.net/lttree/article/details/19909653