传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1180
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
Hint
Hint
地图如下:
这道题还是挺值得写的
里面有很多坑 一次wa 一次T
最大的亮点在于这个梯子会转,
**注意注意 :
wa就在这坑了, 梯子会随时间的推移进行旋转 我们想一下 是不是 走奇数次那么梯子的方向就会发生变化 ,所以要前面是梯子时进行特判**
思路:
首先我们先用结构体记录开始的位置加入优先队列,毕竟找最少时间当然用优先队列
进行正常的搜索 对下一步符合要求的点(不超出边界,不是*)我们可以分3类操作
1. 如果是梯子(即为-或|), 先判断现在梯子的方位
如果可以直接通过,直接通过。否则将步数加1 即等待了1秒
处理完成后再判断通过梯子后的位置是否满足情况,满足情况,标记, 步数+1, 加入队列
2. 如果是终点,加入队列,等待下次读取时结束
3. 满足正常走的条件 (即为. 且没有走过) ,标记, 步数+1, 加入队列
一定要标记 可能会T 走过的不能再走
其实详尽解释都在代码
#include <iostream>
#include <string.h>
#include <algorithm>
//#include <bits/stdc++.h>
#include <queue>
#include <cstdio>
using namespace std;
int m, n;
char Map[1003][1003]; //存储位置
int cx[4][2] = {0,1,0,-1,1,0,-1,0}; //四个方向
int vist[1003][1003]; //标记数组
struct node
{
int x,y;
int step;
friend bool operator<(node a,node b) //对步数设置优先级
{
return a.step>b.step;
}
}now;
void bfs()
{
node next;
priority_queue<node>q; //优先队列
q.push(now);
while (!q.empty())
{
now = q.top();
//cout<<now.x<<" "<<now.y<<endl;
q.pop();
if (Map[now.x][now.y]=='T') //当到达终点时,输出,返回
{
printf ("%d\n",now.step);
return;
}
for (int i=0; i<4; i++) //走四个方向
{
next.x = now.x+cx[i][0];
next.y = now.y+cx[i][1];
//如果不出边界并且不为*
if (next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&Map[next.x][next.y]!='*')
{
//cout<<"!"<<endl;
if (Map[next.x][next.y]=='-'||Map[next.x][next.y]=='|') //当下一个为梯子时
{
//cout<<next.x<<" @@@@ "<<next.y<<endl;
char floor;
if (now.step%2) //由步数计算当前梯子的位置,奇数->改变, 偶数->不变
{
if (Map[next.x][next.y]=='-')
floor = '|';
else
floor = '-';
}
else
floor = Map[next.x][next.y];
next.x = next.x+cx[i][0]; //既然梯子时直通的所以多走一步
next.y = next.y+cx[i][1];
//当梯子方向与路线相反时,等待1秒便可以通过
if (floor=='-'&&(cx[i][0]==1||cx[i][0]==-1))
next.step = now.step+1;
else if (floor=='|'&&(cx[i][1]==1||cx[i][01]==-1))
next.step = now.step+1;
else
next.step = now.step;
//判断通过梯子后的位置是否满足边界 并否和要求
if (next.x>=0&&next.x<m&&next.y>=0&&next.y<n)
{
if (Map[next.x][next.y]=='T')
{
vist[next.x][next.y] = 1;
next.step++;
q.push(next);
}
else if (Map[next.x][next.y]=='.'&&!vist[next.x][next.y])
{
vist[next.x][next.y] = 1;
next.step++;
q.push(next);
}
}
}
else if (Map[next.x][next.y]=='T') //当下一个终点时进队,标记,步数+1
{
vist[next.x][next.y] = 1;
next.step = now.step+1;
q.push(next);
}
else if (Map[next.x][next.y]=='.'&&!vist[next.x][next.y])//当下一个可以走时进队,标记,步数+1
{
//cout<<"!!!!"<<endl;
vist[next.x][next.y] = 1;
next.step = now.step+1;
q.push(next);
}
}
}
}
}
int main()
{
while (~scanf("%d%d",&m,&n))
{
memset(vist, 0, sizeof(vist));
for (int i=0; i<m; i++)
{
getchar();
for (int j=0; j<n; j++)
{
scanf("%c",&Map[i][j]);
if (Map[i][j]=='S')
{
now.x = i;
now.y = j;
now.step = 0;
vist[i][j] = 1;
}
}
}
bfs();
}
return 0;
}