您的位置:首页 > 其它

hdu 1180诡异的楼梯

2016-10-19 16:58 309 查看
重写了一下以前没过的题,读了下以前wrong answer的代码,发现好像题意理解有误啊,我一直认为这样的情况只需要一秒钟,其实这样是无论如何上不去的,无法到达终点…….bfs完全是可行的,没必要加上优先级队列,bfs本身的性质决定时间一定是最短的

*T*
.|*


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

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 30;
char G[maxn][maxn];
int n,m,sx,sy;
bool vis[maxn][maxn];
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};

struct Pair
{
int x,y,step;
Pair(int xx,int yy,int sstep):x(xx),y(yy),step(sstep) {}
};
bool check(int x,int y)
{
if(x < 0 || x >= n || y < 0 || y >= m) return false;
if(vis[x][y] || G[x][y] == '*') return false;
return true;
}

int bfs()
{
queue<Pair> q;
memset(vis,0,sizeof(vis));
q.push(Pair(sx,sy,0));
vis[sx][sy] = true;
while(!q.empty())
{
Pair now = q.front(); q.pop();
int time = now.step;
if(G[now.x][now.y] == 'T')
{
return now.step;
}
for(int k = 0; k < 4; k++)
{
int x = now.x + dir[k][0];
int y = now.y + dir[k][1];
if(!check(x,y)) continue;
if(G[x][y] == '.' || G[x][y] == 'T')
{
q.push(Pair(x,y,now.step + 1)); vis[x][y] = true; continue;
}
if(G[x][y] == '|' || G[x][y] == '-')
{
if(time % 2 == 0) //状态不变
{
if(G[x][y] == '|' && (k == 0 || k == 1))
{
int xx = x + dir[k][0];
int yy = y + dir[k][1];
if(check(xx,yy))
{
q.push(Pair(xx,yy,now.step+1)); vis[xx][yy] = true;
}
}
else if(G[x][y] == '-' && (k == 2 || k == 3))
{
int xx = x + dir[k][0];
int yy = y + dir[k][1];
if(check(xx,yy))
{
q.push(Pair(xx,yy,now.step+1)); vis[xx][yy] = true;
}
}
else
{
q.push(Pair(now.x,now.y,now.step + 1)); //not move
}
}
else  //状态改变
{
if(G[x][y] == '|' && (k == 2 || k == 3))
{
int xx = x + dir[k][0];
int yy = y + dir[k][1];
if(check(xx,yy))
{
q.push(Pair(xx,yy,now.step+1)); vis[xx][yy] = true;
}
}
else if(G[x][y] == '-' && (k == 0 || k == 1))
{
int xx = x + dir[k][0];
int yy = y + dir[k][1];
if(check(xx,yy))
{
q.push(Pair(xx,yy,now.step+1)); vis[xx][yy] = true;
}
}
else
{
q.push(Pair(now.x,now.y,now.step + 1)); //not move
}
}
}
}
}
return -1;
}
int main()
{
#ifdef LOCAL_DEBUG
freopen("input.txt","r",stdin);
#endif // LOCAL_DEBUG
while(scanf("%d%d" ,&n,&m) != EOF)
{
memset(G,0,sizeof(G));
for(int i = 0; i < n; i++) scanf("%s" ,G[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(G[i][j] == 'S')
{
sx = i; sy = j;
}
}
}
int ans = bfs();
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: