bfs 非常xjb搞一下,便利到下一个点都要看它能否走,看他是不是梯子;
处理梯子转的方式是很骚的;你一看就懂了,全网最简单代码
诡异的楼梯
Time Limit: 1000msMemory Limit: 65536KB This problem will be judged on HDU. Original ID: 1180
64-bit integer IO format: %I64d Java class name: Main
Prev Submit Status Statistics Next
Type:
None
Tag it!
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
地图如下:
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int x, y, step;
};
int n, m;
char map[30][30];
int vist[30][30];
int xx, yy;
int dir[4][2] = { 0,1,0,-1,1,0,-1,0 };
int check(int x, int y)
{
if (x<1 || y<1 || x>n || y>m)
return 0;
if (map[x][y] == '*')
return 0;
if (vist[x][y])
return 0;
return 1;
}
int bfs(int x1, int y1)
{
queue<node> q;
node temp;
temp.x = x1;
temp.y = y1;
temp.step = 0;
memset(vist, 0, sizeof(vist));
vist[temp.x][temp.y] = 1;
q.push(temp);
node t, p;
while (!q.empty())
{
p = q.front();
q.pop();
if (map[p.x][p.y] == 'T')
return p.step;
for (int i = 0; i < 4; i++)
{
int nx = p.x + dir[i][0];
int ny = p.y + dir[i][1];
if (nx<1 || ny<1 || nx>n || ny>m) //边界
continue;
if (vist[nx][ny] || map[nx][ny] == '*')
continue;
else if (map[nx][ny] == '.' || map[nx][ny] == 'T')
{
t.x = nx;
t.y = ny;
t.step = p.step + 1;
q.push(t);
vist[t.x][t.y] = 1;
}
else
{
if (i == 0 || i == 1)
{
if (map[nx][ny] == '|'&&p.step % 2 == 1)
{
nx = nx + dir[i][0];
ny = ny + dir[i][1];
if (check(nx, ny))
{
t.x = nx;
t.y = ny;
t.step = p.step + 1;
q.push(t);
vist[t.x][t.y] = 1;
}
}
else if (map[nx][ny] == '-'&&p.step % 2 == 0)
{
nx = nx + dir[i][0];
ny = ny + dir[i][1];
if (check(nx, ny))
{
t.x = nx;
t.y = ny;
t.step = p.step + 1;
q.push(t);
vist[t.x][t.y] = 1;
}
}
else
{
t.x = p.x;
t.y = p.y;
t.step = p.step + 1;
vist[t.x][t.y] = 1;
q.push(t);
}
}
else //if (i == 2 || i == 3)
{
if (map[nx][ny] == '|'&&p.step % 2 == 0)
{
nx = nx + dir[i][0];
ny = ny + dir[i][1];
if (check(nx, ny))
{
t.x = nx;
t.y = ny;
t.step = p.step + 1;
q.push(t);
vist[t.x][t.y] = 1;
}
}
else if (map[nx][ny] == '-'&&p.step % 2 == 1)
{
nx = nx + dir[i][0];
ny = ny + dir[i][1];
if (check(nx, ny))
{
t.x = nx;
t.y = ny;
t.step = p.step + 1;
q.push(t);
vist[t.x][t.y] = 1;
}
}
else//等一分钟;
{
t.x = p.x;
t.y = p.y;
t.step = p.step + 1;
vist[t.x][t.y] = 1;
q.push(t);
}
}
}
}
}
return -1;
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
int i, j;
for (i = 1; i <= n; i++)
{
getchar();
for (j = 1; j <= m; j++)
{
scanf("%c", &map[i][j]);
if (map[i][j] == 'S')
{
xx = i;
yy = j;
}
}
}
//cout << xx << " " << yy << endl;
int ans = bfs(xx, yy);
cout << ans << endl;
}
return 0;
}