HDU1180.诡异的楼梯.(bfs)

诡异的楼梯

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 20528 Accepted Submission(s): 5338

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

在这里插入图片描述

题解:这题注意可以停留在梯子前面等待梯子旋转,所以此时时间加一,每次过梯子的时候要注意check梯子后面的一个点,另外每次过梯子时,方向肯定与来时的方向相同,所以直接加上dir[i][x],另外过梯子的时候如果有不符合的情况直接continue

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
int n, m;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };//下,上,右,左;
char map[25][25];
int vis[25][25];
int bx, by, ex, ey;
bool checkedge(int x, int y){
	if (x<1 || x>m || y<1 || y>n || map[x][y] == '*' || vis[x][y])
		return 1;
	return 0;
}
struct node{
	int x, y;
	int step;
};
int bfs(int bx, int by,int ex,int ey){
	node now;
	queue<node>q;
	now.x = bx, now.y = by;
	now.step = 0;
	vis[bx][by] = 1;
	q.push(now);
	while (!q.empty()){
		now = q.front();
		q.pop();
		if (now.x == ex&&now.y==ey) return now.step;
		int nx, ny;
		for (int i = 0; i < 4; i++){
			nx = now.x + dir[i][0];
			ny = now.y + dir[i][1];
			if (checkedge(nx, ny)) continue;
			if (map[nx][ny] == '|'){
				if (!checkedge(nx + dir[i][0], ny + dir[i][1])){
					nx += dir[i][0];
					ny += dir[i][1];
					if ((now.step % 2 == 0 && (i == 2 || i == 3)) || (now.step % 2 != 0 && (i == 0 || i == 1)))
						now.step += 1;
				}
				else continue;
			}
			if (map[nx][ny] == '-'){
				if (!checkedge(nx + dir[i][0], ny + dir[i][1])){
					nx += dir[i][0];
					ny += dir[i][1];
					if ((now.step % 2 != 0 && (i == 2 || i == 3)) || (now.step % 2 == 0 && (i == 0 || i == 1)))
						now.step += 1;
				}
				else continue;
			}
			q.push({ nx, ny, now.step + 1 });
			vis[nx][ny] = 1;
		}
	}
	return 0;
}
int main(){
	while (~scanf("%d%d", &m, &n)){
		memset(vis, 0, sizeof vis);
		for (int i = 1; i <= m; i++)
			scanf("%s", map[i] + 1);
		for (int i = 1; i <= m; i++)
			for (int j = 1; j <= n; j++){
				if (map[i][j] == 'S')
					bx = i, by = j;
				else if (map[i][j] == 'T')
					ex = i, ey = j;
			}
		cout << bfs(bx, by, ex, ey) << endl;
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值