您的位置:首页 > 其它

HDU1180 诡异楼梯

2016-07-30 09:52 369 查看
H - 诡异的楼梯
Time Limit:1000MS     Memory Limit:65536KB     64bit
IO Format:
%I64d & %I64u

Submit Status Practice HDU
1180

Description

正式开学以后, fish发现在BIT里, 某些楼梯并不是静止不动的,相反, 他们每隔一分钟就变动一次方向.
比如下面的例子里, 一开始楼梯在竖直方向, 一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.fish发现对他来说很难找到能使得他最快到达目的地的路线, 这时uu( fish最好的朋友)告诉fish正好有一个魔法道具可以帮助他寻找这样的路线, 而那个魔法道具上的咒语, 正是由你纂写的.

Input

测试数据有多组,每组的表述如下: 

第一行有两个数, M和N, 接下来是一个M行N列的地图,'*' 表示障碍物,'.'表示走廊,'|' 或者'-'表示一个楼梯, 并且标明了它在一开始时所处的位置:'|' 表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,' T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.fish每秒只能停留在'.'或'S'和'T'所标记的格子内.

Output

只有一行, 包含一个数T, 表示到达目标的最短时间. 

注意: fi
b763
sh只能每次走到相邻的格子而不能斜走, 每移动一次恰好为一分钟,并且fish登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,fish从来不在楼梯上停留.并且每次楼梯都恰好在fish移动完毕以后才改变方向.

Sample Input

5 5

**..T
**.*.
..|..
.*.*.
S....


Sample Output

7


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,a,b,p,q;
char str[30][30];
int book[30][30];
int nx[4][2] = {0,1,1,0,0,-1,-1,0};
struct note
{
int x,y,step;
friend bool operator < (note A,note B)
{
return A.step > B.step;
}
};
int bfs()
{
priority_queue<note> Q;
note n1,n2;
n1.x = a,n1.y = b,n1.step = 0;
Q.push(n1);
memset(book,0,sizeof(book));
book[a][b] = 1;
int i,j;
while(!Q.empty())
{
n1 = Q.top();
Q.pop();
if(n1.x == p && n1.y == q)
{
return n1.step;
}
for(i = 0; i<4; i++)
{
int tx = n1.x+nx[i][0];
int ty = n1.y+nx[i][1];

if(tx<0 || ty<0 || tx>=n || ty>=m  || book[tx][ty] || str[tx][ty] == '*' )
continue;
n2.x = tx;
n2.y = ty;
n2.step = n1.step+1;

if((str[tx][ty] == '|'&&(n1.step%2)) || (str[tx][ty] == '-'&&(n1.step%2 == 0))) // 楼梯的方向 ——
{
if(nx[i][0] == 0) //人走的方向 ——
{
if(nx[i][1] == 1) ty = n1.y+2;
else ty = n1.y-2;
if(ty>0 && ty<m && !book[tx][ty] )
{
n2.step = n1.step+1; //方向相同加1
n2.y = ty;
Q.push(n2);
book[n2.x][n2.y] = 1;
}
}
if(nx[i][1] == 0)//人走的方向 |
{
if(nx[i][0] == 1) tx = n1.x+2;
else tx = n1.x-2;
if(tx>0 && tx<n && !book[tx][ty] )
{
n2.step = n1.step+2; //方向相反加2
n2.x = tx;
Q.push(n2);
book[n2.x][n2.y] = 1;
}
}
}
else if((str[tx][ty] == '|'&&(n1.step%2) == 0) || (str[tx][ty] == '-'&&(n1.step%2)))  //楼梯 |
{
if(nx[i][0] == 0) //人的方向 ——
{
if(nx[i][1] == 1) ty = n1.y+2;
else ty = n1.y-2;
if(ty>0 && ty<m && !book[tx][ty] )
{
n2.step = n1.step+2;  //加2
n2.y = ty;
Q.push(n2);
book[n2.x][n2.y] = 1;
}
}
if(nx[i][1] == 0) // |
{
if(nx[i][0] == 1) tx = n1.x+2;
else tx = n1.x-2;
if(tx>0 && tx<n && !book[tx][ty] )
{
n2.step = n1.step+1; //加1
n2.x = tx;
Q.push(n2);
book[n2.x][n2.y] = 1;
}
}
}
else  if(str[n2.x][n2.y]!='.' || str[n2.x][n2.y]!='T')
{
Q.push(n2);              //及时入队,不能放在下面入队,因为可能是楼梯但不符合条件的
book[n2.x][n2.y] = 1;
}
}
}
return -1;
}
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out2.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
int i,j;
for(i = 0; i<n; i++)
{
scanf("%s",str[i]);
for(j = 0; str[i][j]!='\0'; j++)
{
if(str[i][j] == 'S')
{
a = i,b = j;
}
if(str[i][j] == 'T')
{
p = i,q = j;
}
}
}
int ans = bfs();
printf("%d\n",ans);
}
return 0;
}


附上一组测试数据:

S | . -
- T - .
. |  .  .

7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: