诡异的楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 11140 Accepted Submission(s): 2781
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
Hint
Hint
地图如下:
//刚开始又理解错了题意,真特么佩服自己。。。老是傻逼。。。
//优先队列加bfs,用的时间为奇数时,翻转楼梯方向,楼梯方向与前进方向不一致时,等待一分钟,所以加一。另外楼梯不要标记,可重复经过
//总结:首先要理解题意!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <set>
#include <queue>
#include <vector>
using namespace std;
char s[30][30];
int m, n, k;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
bool use[30][30];
struct node
{
int x, y, cnt;
bool operator< (const node a) const
{
return cnt > a.cnt;
}
} e, p;
struct mm
{
int x, y;
} tmp[500];
void change(int f) //奇数时改变楼梯方向
{
if(f & 1)
{
for(int i = 0; i < k; i++)
if(s[tmp[i].x][tmp[i].y] == '|') s[tmp[i].x][tmp[i].y] = '-';
else s[tmp[i].x][tmp[i].y] = '|';
}
}
void bfs(int x, int y)
{
priority_queue <node> que;
e.x = x, e.y = y, e.cnt = 0;
que.push(e);
use[x][y] = true;
while(! que.empty())
{
p = que.top(); que.pop();
if(s[p.x][p.y] == 'T')
{
printf("%d\n", p.cnt);
break;
}
change(p.cnt);
for(int i = 0; i < 4; i++)
{
e.x = p.x + dx[i], e.y = p.y + dy[i];
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
{
e.cnt = p.cnt + 1;
if(s[e.x][e.y] == '|')
{
if(i == 0)
{
e.x = e.x - 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, que.push(e);
}
else if(i == 1)
{
e.y = e.y + 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e);
}
else if(i == 2)
{
e.x = e.x + 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, que.push(e);
}
else
{
e.y = e.y - 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e);
}
}
else if(s[e.x][e.y] == '-')
{
if(i == 0)
{
e.x = e.x - 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e);
}
else if(i == 1)
{
e.y = e.y + 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, que.push(e);
}
else if(i == 2)
{
e.x = e.x + 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, e.cnt = e.cnt + 1, que.push(e);
}
else
{
e.y = e.y - 1;
if(e.x >= 0 && e.x < m && e.y >= 0 && e.y < n && s[e.x][e.y] != '*' && use[e.x][e.y] == false)
use[e.x][e.y] = true, que.push(e);
}
}
else
{
use[e.x][e.y] = true;
que.push(e);
}
}
}
change(p.cnt);
}
}
int main()
{
while(~ scanf("%d%d", &m, &n))
{
int x, y;
memset(use, false, sizeof use);
k = 0;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
{
scanf(" %c", &s[i][j]);
if(s[i][j] == 'S') x = i, y = j;
if(s[i][j] == '-' || s[i][j] == '|') tmp[k].x = i, tmp[k].y = j, k++;
}
bfs(x, y);
}
return 0;
}