#include
#include
#include
using namespace std; int row,line,xx[4]={-1,1,0,0},yy[4]={0,0,-1,1}; char map[100][100]; bool vis[100][100]; struct node { int x,y,step; }st; void init() { int i,j; bool flag=1; for(i=0;i
=row||y>=line||vis[x][y]||map[x][y]=='*') return 0; return 1; } void bfs() { int i; queue
qq; node t1,t2; qq.push(st); while(qq.size()) { t1=qq.front(); qq.pop(); if(map[t1.x][t1.y]=='T') { printf("%d\n",t1.step); return; } for(i=0;i<4;i++) { t2=t1; t2.x+=xx[i]; t2.y+=yy[i]; t2.step++; if(!iscan(t2.x,t2.y)) continue; if(map[t2.x][t2.y]=='.'||map[t2.x][t2.y]=='T') { vis[t2.x][t2.y]=1; qq.push(t2); } else if((map[t2.x][t2.y]=='|'&&!(t1.step&1))||(map[t2.x][t2.y]=='-'&&(t1.step&1))) { if(i>1) { t2=t1; t2.step++; qq.push(t2); continue; } t2.x+=xx[i]; if(!iscan(t2.x,t2.y)) continue; vis[t2.x][t2.y]=1; qq.push(t2); } else { if(i<2) { t2=t1; t2.step++; qq.push(t2); continue; } t2.y+=yy[i]; if(!iscan(t2.x,t2.y)) continue; vis[t2.x][t2.y]=1; qq.push(t2); } } } } int main() { while(scanf("%d%d",&row,&line)!=EOF) { init(); bfs(); } }