# HDU1180 诡异的楼梯[bfs]

E - 诡异的楼梯
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的，相反,他们每隔一分钟就变动一次方向.

Input

Output

Sample Input

5 5 **..T **.*. ..|.. .*.*. S....

Sample Output

7

Hint

Hint



#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=30;
int m,n,sx,sy,tx,ty;
char Map[N][N];
int mark[N][N],dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct node
{
int x,y,step;
friend bool operator <(const node &a,const node &b)//重载小于号 小于号相反是升序
{
return a.step>b.step;
}
};
//s(步数) 如果是奇数的话  会让 | - 反过来   设|是1  -是0
bool check(char pic,int dis,int s)
{
s=s%2;
if (pic=='|')
s=(s+1)%2;
else
s=(s+0)%2;
if ((s && (dis==0 || dis==2)) || (!s && (dis==1 || dis==3)))//如果是|就是上下能过 如果是-就是左右能过
return 1;
return 0;
}
priority_queue<node>q;
int bfs()
{
while (!q.empty())
q.pop();
node start,later;
start.x=sx;
start.y=sy;
start.step=0;
mark[sx][sy]=1;
q.push(start);
while(!q.empty())
{
start=q.top();
q.pop();
if (Map[start.x][start.y]=='T')
return start.step;
for (int i=0 ; i<4; i++)
{
int nx=start.x+dir[i][0];
int ny=start.y+dir[i][1];
if (nx<0 || nx>=n || ny<0 || ny>=m || mark[nx][ny] || Map[nx][ny]=='*')
continue;
if (Map[nx][ny]!='|' && Map[nx][ny]!='-')
{
mark[nx][ny]=1;
later.x=nx;
later.y=ny;
later.step=start.step+1;
q.push(later);
}
else if (check(Map[nx][ny],i,start.step))
{
nx+=dir[i][0];
ny+=dir[i][1];
if (nx>=0 && nx<n && ny>=0 && ny<m && !mark[nx][ny] && Map[nx][ny]!='*')
{
mark[nx][ny]=1;
later.x=nx;
later.y=ny;
later.step=start.step+1;
q.push(later);
}
}
else
{
later.x=start.x;
later.y=start.y;
later.step=start.step+1;
q.push(later);
}
}
}
return 0;
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
memset(mark,0,sizeof(mark));
bool fs=0;
sx=sy=0;
for (int i=0 ; i<n ; i++)
{
scanf("%s",Map[i]);
if (!fs)
{
for (int j=0 ; j<m ; j++)
{
if (Map[i][j]=='S')
{
sx=i;
sy=j;
fs=1;
}
}
}
}
printf("%d\n",bfs());
}
return 0;
}
/*
5 5
**...
**.*.
..-.*
-*.*.
ST...

5 5
**...
**.*.
..-.*
-*.*.
S|...

5 5
**...
**.*.
..-..
.*.*.
S....
*/