C - 诡异的楼梯
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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地图如下:![]()
分析:这道题需要注意的是楼梯的变化,碰到楼梯时要用当前所用时间来判断楼梯状态,经过偶数秒,楼梯无变化,奇数秒,有变化;同时需要判断到楼梯的方向与楼梯方向是否相同,若相同,直接一秒通过楼梯,若不同,停下一秒等待楼梯改变状态。剩下的就是 优先队列 + 广搜了
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char map[30][30];//地图
int vis[30][30];//判断是否访问过,若访问过,标记为1,否则为0
int n,m;
int sx,sy;//起点坐标
int gx,gy;//终点坐标
struct pp
{
int x,y,t;
//重载"<"操作符,指定优先规则
bool operator < (const pp & w) const
{
return w.t < t;
}
};
//搜索方向
int go[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int bfs()
{
priority_queue<pp>que;
pp cur,next;
cur.x = sx;
cur.y = sy;
cur.t = 0;
//将起点加入队列,并将其标记为1
que.push(cur);
vis[sx][sy] = 1;
//不断循环直到队列为空
while(que.size())
{
//取出队首元素,同时队首元素出对
cur = que.top();
que.pop();
//如果取出的状态已经是终点,则返回最小步数
if(cur.x == gx && cur.y == gy)
return cur.t;
//对四个方向进行搜索
for(int i = 0;i < 4;i++)
{
int nx = cur.x + go[i][0];
int ny = cur.y + go[i][1];
//如果可以走且没遇到楼梯,将其入队,并且将到该位置的距离确定为到cur的距离+1
if(nx >= 0 && nx < n && ny >= 0 && ny < m && (map[nx][ny] == '.' || map[nx][ny] == 'T')&& !vis[nx][ny])
{
next.x = nx;
next.y = ny;
next.t = cur.t + 1;
que.push(next);
vis[nx][ny] = 1;
}
//如果遇到"|"的楼梯,讨论Harry的位置及确定楼梯此时的状态,以此确定是否需要等待
if(nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] == '|')
{
if(cur.y == ny && cur.x < nx && (map[nx + 1][ny] == '.' || map[nx + 1][ny] == 'T') && !vis[nx + 1][ny])
{
next.x = nx + 1;
next.y = ny;
if(cur.t % 2 == 0)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx + 1][ny] = 1;
}
if(cur.y == ny && cur.x > nx && (map[nx - 1][ny] == '.' || map[nx - 1][ny] == 'T') && !vis[nx -1][ny])
{
next.x = nx - 1;
next.y = ny;
if(cur.t % 2 == 0)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx - 1][ny] = 1;
}
if(cur.y > ny && cur.x == nx && (map[nx][ny - 1] == '.' || map[nx][ny - 1] == 'T') && !vis[nx][ny - 1])
{
next.x = nx;
next.y = ny - 1;
if(cur.t % 2 == 1)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx][ny - 1] = 1;
}
if(cur.x == nx && cur.y < ny &&(map[nx][ny + 1] == '.' || map[nx][ny + 1] == 'T') && !vis[nx][ny + 1])
{
next.x = nx;
next.y = ny + 1;
if(cur.t % 2 == 1)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx][ny + 1] = 1;
}
}
//如果遇到"-"的楼梯,讨论Harry的位置及确定楼梯此时的状态,以此确定是否需要等待
if(nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] == '-')
{
if(cur.y == ny && cur.x < nx && (map[nx + 1][ny] == '.' || map[nx + 1][ny] == 'T') && !vis[nx + 1][ny])
{
next.x = nx + 1;
next.y = ny;
if(cur.t % 2 == 1)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx + 1][ny] = 1;
}
if(cur.y == ny && cur.x > nx && (map[nx - 1][ny] == '.' || map[nx - 1][ny] == 'T') && !vis[nx -1][ny])
{
next.x = nx - 1;
next.y = ny;
if(cur.t % 2 == 1)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx - 1][ny] = 1;
}
if(cur.x == nx && cur.y < ny &&(map[nx][ny + 1] == '.' || map[nx][ny + 1] == 'T') && !vis[nx][ny + 1])
{
next.x = nx;
next.y = ny + 1;
if(cur.t % 2 == 0)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx][ny + 1] = 1;
}
if(cur.x == nx && cur.y > ny &&(map[nx][ny - 1] == '.' || map[nx][ny - 1] == 'T') && !vis[nx][ny - 1])
{
next.x = nx;
next.y = ny - 1;
if(cur.t % 2 == 0)
next.t = cur.t + 1;
else
next.t = cur.t + 2;
que.push(next);
vis[nx][ny - 1] = 1;
}
}
}
}
return -1;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
getchar();
for(int i = 0;i <n;i++)
{
gets(map[i]);
for(int j = 0;j < m;j++)
{
if(map[i][j] == 'S') //记录起点
{
sx = i;sy = j;
}
if(map[i][j] == 'T') //记录终点
{
gx = i;gy = j;
}
}
}
memset(vis,0,sizeof(vis));//初始化为0,都未被访问过
int res = bfs();
printf("%d\n",res);
}
return 0;
}