标签:
5 5 **..T **.*. ..|.. .*.*. S....
7
#include <iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; int dr[4][2]={{1,0},{-1,0},{0,1},{0,-1} };// 楼梯方向设置对后面判断有影响 struct node { int x,y,ti; }; char mp[25][25]; int ti[25][25]; int n,m,i,j,sx,sy,tx,ty; int bfs() { node p; queue<node> s; p.x=sx; p.y=sy; p.ti=0; ti[sx][sy]=0; s.push(p); while(!s.empty()) { node q=s.front(); s.pop(); for(int i=0;i<4;i++) { int xx=q.x+dr[i][0]; int yy=q.y+dr[i][1]; if (xx>=0 && xx<n && yy>=0 && yy<m && mp[xx][yy]!=‘*‘) { p.ti=q.ti+1; if(mp[xx][yy]==‘|‘ || mp[xx][yy]==‘-‘) //关键 { int xxx=xx+dr[i][0]; int yyy=yy+dr[i][1]; if(mp[xxx][yyy]==‘*‘ || xxx<0 ||xxx>=n || yyy<0 || yyy>=m ) continue; if(mp[xx][yy]==‘|‘) { if(q.ti%2==1 && i<=1 || q.ti%2==0 && i>1) p.ti++; } else if (mp[xx][yy]==‘-‘) if(q.ti%2==1 && i>1 || q.ti%2==0 && i<=1) p.ti++; xx=xxx; yy=yyy; } if(p.ti<ti[xx][yy]) { p.x=xx; p.y=yy; ti[xx][yy]=p.ti; s.push(p); } if(xx==tx && yy==ty) return ti[xx][yy]; } } } } int main() { while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++) { scanf("%s",mp[i]); for(j=0;j<m;j++) { if (mp[i][j]==‘S‘) {sx=i,sy=j;mp[i][j]=‘.‘;} if (mp[i][j]==‘T‘) {tx=i,ty=j;mp[i][j]=‘.‘;} } } memset(ti,10000,sizeof(ti)); printf("%d\n",bfs()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5667467.html