Problem Description
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
Input
测试数据有多组,每组的表述如下: 第一行有两个数,M和N,接下来是一个M行N列的地图,’*‘表示障碍物,’.‘表示走廊,’|‘或者’-‘表示一个楼梯,并且标明了它在一开始时所处的位置:’|‘表示的楼梯在最开始是竖直方向,’-'表示的楼梯在一开始是水平方向.地图中还有一个’S’是起点,‘T’是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在’.'或’S’和’T’所标记的格子内.
Output
只有一行,包含一个数T,表示到达目标的最短时间. 注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
Sample Input
5 5
**…T
**..
…|…
..*.
S…
Sample Output
7
[hint]Hint[/hint]
地图如下:
[img]/data/images/C6-1003.gif[/img]
Source
Gardon-DYGG Contest 1
``一开始以为只有单个楼梯wa了两次,后来改回来的时候,当楼梯不能走的时候应该压入还没走之前的位置。我压了走之后的位置又错了两发…
#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy,ex,ey;
char mp[25][25];
int vis[25][25];
int dir[4][2]={-1,0,0,-1,1,0,0,1};//上左下右
struct s{
int x,y,pre,step;
};
bool check(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0)
{
return true;
}
return false;
}
void bfs()
{
queue<s>q;
s cur,next;
cur.pre=-1;cur.x=sx;cur.y=sy;cur.step=0;
vis[cur.x][cur.y]=1;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(mp[cur.x][cur.y]=='T')
{
printf("%d\n",cur.step);
return ;
}
for(int i=0;i<4;i++)
{
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];
next.pre=i;
if(check(next.x,next.y))
{
if(mp[next.x][next.y]=='*')
continue;
if(mp[next.x][next.y]=='.'||mp[next.x][next.y]=='T')
{
vis[next.x][next.y]=1;
next.step=cur.step+1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
else if(next.pre==1||next.pre==3)//左和右
{
if(mp[next.x][next.y]=='-'&&cur.step%2==0)
{
next.x+=dir[i][0];
next.y+=dir[i][1];
if(check(next.x,next.y)&&mp[next.x][next.y]!='*')
{
next.step=cur.step+1;
vis[next.x][next.y]=1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
}
else if(mp[next.x][next.y]=='|'&&cur.step%2!=0)
{
next.x+=dir[i][0];
next.y+=dir[i][1];
if(check(next.x,next.y)&&mp[next.x][next.y]!='*')
{
next.step=cur.step+1;
vis[next.x][next.y]=1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
}
else
{
next.x=cur.x;
next.y=cur.y;
next.step=cur.step+1;
vis[next.x][next.y]=1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
}
else if(next.pre==0||next.pre==2)
{
if(mp[next.x][next.y]=='|'&&cur.step%2==0)
{
next.x+=dir[i][0];
next.y+=dir[i][1];
if(check(next.x,next.y)&&mp[next.x][next.y]!='*')
{
next.step=cur.step+1;
vis[next.x][next.y]=1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
}
else if(mp[next.x][next.y]=='-'&&cur.step%2!=0)
{
next.x+=dir[i][0];
next.y+=dir[i][1];
if(check(next.x,next.y)&&mp[next.x][next.y]!='*')
{
next.step=cur.step+1;
vis[next.x][next.y]=1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
}
else
{
next.x=cur.x;
next.y=cur.y;
next.step=cur.step+1;
vis[next.x][next.y]=1;
// printf("%d %d\n",next.x,next.y);
q.push(next);
}
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='S')
{
sx=i;
sy=j;
}
}
}
bfs();
}
return 0;
}
//上是0 左是1 下是2 右是3