诡异的楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2021 Accepted Submission(s): 484
Problem Description
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
Input
测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
Output
只有一行,包含一个数T,表示到达目标的最短时间.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
Sample Input
5 5 **..T **.*. ..|.. .*.*. S....
Sample Output
7
地图如下:

算法:DFS+优先队列
代码如下:
#include<cstdio>
#include<queue>
using namespace std;
int n,m,map[25][25],k,sa,sb;
int d[4][2]={-1,0,1,0,0,-1,0,1};
char str[25][25];
typedef struct Lnode{
int x,y,step;
friend operator<(Lnode a,Lnode b)
{
return a.step>b.step;
}
}node;
int bfs(node start)
{
priority_queue<node> Q;
node now,next;
now=start;
Q.push(now);
char c;
int i,time=0;
while(!Q.empty())
{
now=Q.top();
Q.pop();
for(i=0;i<=4;i++)
{
next.x=now.x+d[i][0];
next.y=now.y+d[i][1];
next.step=now.step+1;
if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&(str[next.x][next.y]=='-'||str[next.x][next.y]=='|'))
{
if(!(now.step%2))
c=str[next.x][next.y];
else if(now.step%2)
{
if(str[next.x][next.y]=='|')
c='-';
else
c='|';
}
if(((d[i][0]==-1&&d[i][1]==0)||(d[i][0]==1&&d[i][1]==0))&&c=='|')
{
next.x+=d[i][0];
next.y+=d[i][1];
}
else if(((d[i][0]==0&&d[i][1]==-1)||(d[i][0]==0&&d[i][1]==1))&&c=='-')
{
next.x+=d[i][0];
next.y+=d[i][1];
}
else if(((d[i][0]==-1&&d[i][1]==0)||(d[i][0]==1&&d[i][1]==0))&&c=='-')
{
next.x+=d[i][0];
next.y+=d[i][1];
next.step++;
}
else if(((d[i][0]==0&&d[i][1]==-1)||(d[i][0]==0&&d[i][1]==1))&&c=='|')
{
next.x+=d[i][0];
next.y+=d[i][1];
next.step++;
}
}
if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&map[next.x][next.y]==0)
{
if(str[next.x][next.y]=='T')
return next.step;
else if(str[next.x][next.y]=='.')
{
map[next.x][next.y]=1;
Q.push(next);
}
}
}
}
return -1;
}
int main()
{
int i,j,time;
while(scanf("%d%d",&n,&m)!=-1)
{
k=0;
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
for(j=0;j<m;j++)
{
if(str[i][j]=='S')
{
sa=i;sb=j;
}
}
}
memset(map,0,sizeof(map));
node start;
start.step=0;
start.x=sa;
start.y=sb;
map[sa][sb]=1;
time=bfs(start);
printf("%d\n",time);
}
return 0;
}
整理后的代码:
#include<cstdio>
#include <cstring>
#include <iostream>
#include<queue>
using namespace std;
char map[21][21];
int to[4][2]={0,1,0,-1,1,0,-1,0};
int sx,sy,ans,m,n;
struct node
{
int x,y,step;
bool operator <(const node t)const
{
return step > t.step;
}
};
bool check(int x,int y)
{
if(x<0||y<0||x>=n||y>=m)
return 1;
if(map[x][y]=='*')
return 1;
return 0;
}
void BFS()
{
int i;
node a,next;
priority_queue<node>Q;
a.x=sx;
a.y=sy;
a.step=0;
Q.push(a);
while(!Q.empty())
{
a=Q.top();
Q.pop();
for(i=0;i<4;i++)
{
next.x=a.x+to[i][0];
next.y=a.y+to[i][1];
next.step=a.step+1;
if(map[next.x][next.y]=='|'&&a.step%2==0||map[next.x][next.y]=='-'&&a.step%2==1)
{
if(i==2||i==3)
{
next.x+=to[i][0];
next.y+=to[i][1];
//next.step+=1;
}
else if(i==0||i==1)
{
next.x+=to[i][0];
next.y+=to[i][1];
next.step+=1;
}
}
if(map[next.x][next.y]=='-'&&a.step%2==0||map[next.x][next.y]=='|'&&a.step%2==1)
{
if(i==0||i==1)
{
next.x+=to[i][0];
next.y+=to[i][1];
//next.step+=1;
}
else if(i==2||i==3)
{
next.x+=to[i][0];
next.y+=to[i][1];
next.step+=1;
}
}
if(check(next.x,next.y))
continue;
if(map[next.x][next.y]=='T')
{
ans=next.step;
return;
}
map[next.x][next.y]='*';
Q.push(next);
}
}
ans=-1;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='S')
{
sx=i;
sy=j;
break;
}
}
BFS();
printf("%d\n",ans);
}
return 0;
}