hdu 1180 诡异的楼梯

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1180

求起点到终点的最短时间,题目没说应该都是存在最短时间的。

和一般的广搜区别就是楼梯的判断,分四种情况讨论就可以了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int dir[][2] = {1,0,-1,0,0,1,0,-1};
int row, col, startx, starty, endx, endy;
char map[21][21];
bool vis[21][21];
struct Node
{
	int x, y, step;
	friend bool operator < (const Node &a, const Node &b)
	{
		return a.step > b.step;
	}
};
inline bool cango(int x, int y)
{
	if (x >= 0 && x < row && y >= 0 && y < col && !vis[x][y] && map[x][y] != '*')
		return true;
	return false;
}
int bfs()
{
	memset(vis, 0, sizeof(vis));
	Node now, next;
	now.x = startx, now.y = starty, now.step = 0;
	vis[now.x][now.y] = 1;
	priority_queue<Node> q;
	q.push(now);
	while (!q.empty())
	{
		now = q.top();
		q.pop();
		if (now.x == endx && now.y == endy)
			return now.step;
		for (int i = 0; i < 4; ++i)
		{
			next.x = now.x + dir[i][0], next.y = now.y + dir[i][1];
			bool isvertical = 0, iseven = 0;
			if (cango(next.x, next.y))
			{
				if (map[next.x][next.y] != '|' && map[next.x][next.y] != '-')
				{
					vis[next.x][next.y] = 1;
					next.step = now.step + 1;
					q.push(next);
					continue;
				}
				if (map[next.x][next.y] == '|')
					isvertical = 1;
				if (now.step % 2 == 0)
					iseven = 1;
				if (cango(next.x+dir[i][0], next.y+dir[i][1]))
				{
					Node temp;
					temp.x = next.x + dir[i][0], temp.y = next.y + dir[i][1];
					vis[temp.x][temp.y] = 1;
					if (i == 0 || i == 1)
					{
						if ((isvertical && iseven) || (!isvertical && !iseven))
							temp.step = now.step + 1;
						else
							temp.step = now.step + 2;
					}
					if (i == 2 || i == 3)
					{
						if ((isvertical && !iseven) || (!isvertical && iseven))
							temp.step = now.step + 1;
						else
							temp.step = now.step + 2;
					}
					q.push(temp);
				}
			}
		}
	}
}
int main()
{
	while (scanf("%d %d", &row, &col) != EOF)
	{
		for (int i = 0; i < row; ++i)
		{
			scanf("%s", map[i]);
			for (int j = 0; j < col; ++j)
			{
				if (map[i][j] == 'S')
					startx = i, starty = j;
				else if (map[i][j] == 'T')
					endx = i, endy = j;
			}
		}
		printf("%d\n", bfs());
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PegasusWang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值