【搜索之BFS + 优先队列】杭电 hdu 1180 诡异的楼梯

/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
    Copyright (c) 2012 panyanyany All rights reserved.

    URL   : http://acm.hdu.edu.cn/showproblem.php?pid=1180
    Name  : 1180 诡异的楼梯

    Date  : Tuesday, April 03, 2012
    Time Stage : 3 hours

    Result:
5696313	2012-04-03 14:44:43	Accepted	1180
0MS	280K	3502 B
C++	pyy


Test Data :

Review :
WA了无数次之后,看到有人说用优先队列……于是AC了,但不明白有人没用优先队列是怎么
AC的,求解答!
//----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std ;

#define MEM(a, v)        memset (a, v, sizeof (a))    // a for address, v for value
#define max(x, y)        ((x) > (y) ? (x) : (y))
#define min(x, y)        ((x) < (y) ? (x) : (y))

#define INF     (0x3f3f3f3f)
#define MAXN    (22)

#define DB    /##/
#define LL __int64

#define _IN_RANGE(a, s, t)	((s) <= (a) && (a) < (t))
#define _IN(x, y)	(_IN_RANGE(x, 0, m) && _IN_RANGE(y, 0, n))
#define _MAP(pt)	map[pt.x][pt.y]

struct POINT {
	POINT(int ax = -1, int ay = -1): x(ax), y(ay) {}
	bool operator== (const POINT &pt) const {
		return pt.x == x && pt.y == y;
	}
	bool operator< (const POINT &pt) const {	// 两个 const 的限定是必须的,否则会报错
		return mi > pt.mi;
	}
	int x, y;
	int mi;
} path[MAXN][MAXN];

const int dir[4][2] = {0,1, 0,-1, 1,0, -1,0};

int		m, n;

char	map[MAXN][MAXN];
//bool	vis[MAXN][MAXN];

POINT	S, T, np, tm;

int bfs()
{
	char c ;
	int i, j;
	priority_queue<POINT>	q; 	// 因为有楼梯的存在,所以每个格子的花费不是完全相同
								// 因此要用到优先队列

	// 要注意 m = n = 0 的情况
	S.mi = 0;
	T.mi = 0;
	path[T.x][T.y] = T;
	q.push(S);
	while (!q.empty())
	{
		tm = q.top();
		path[tm.x][tm.y] = tm;
		q.pop();

		if (tm == T)
			break;

DB      printf("\ntm:(%d,%d), map:%c, mi:%d\n", tm.x, tm.y, _MAP(tm), tm.mi);
DB      printf("---- for ----\n");

		for (i = 0; i < 4; ++i)
		{
			np = tm;
			np.x = np.x + dir[i][0];
			np.y = np.y + dir[i][1];
			
			if (!_IN(np.x, np.y) || '*' == _MAP(np))
				continue;

DB          printf("1. np: (%d,%d), map:%c, mi:%d\n", np.x, np.y, _MAP(np), np.mi);

			if ('.' == _MAP(np))
			{
				np.mi = tm.mi + 1;
				_MAP(np) = '*';
				q.push(np);
			}
			else
			{
				if (tm.mi & 1)	// 时间为奇数,则楼梯转向
				{
					if ('|' == _MAP(np))
						c = '-';
					else
						c = '|';
				}
				else
					c = _MAP(np);

				// 楼梯与自己的方向不同,则等一分钟,并使楼梯转向
				if ('|' == c && np.y != tm.y)
				{
					c = '-';
					np.mi = tm.mi + 1;
				}
				else if ('-' == c && np.x != tm.x)
				{
					c = '|';
					np.mi = tm.mi + 1;
				}

				// 把harry送到对面
				if ('|' == c)
				{
					if (np.x < tm.x)	// 向上送
						--np.x;
					else if (np.x > tm.x)	// 向下送
						++np.x;
				}
				else
				{
					if (np.y < tm.y)	// 向左边送
						--np.y;
					else if (np.y > tm.y)	// 向右边送
						++np.y;
				}

				// 送的过程要一分钟
				++np.mi;

				// 如果不判断楼梯背后的点是否被访问过,则会 MLE
				if (_IN(np.x, np.y) && '*' != _MAP(np))
					q.push(np);
			}
			
DB          printf("2. np: (%d,%d), map:%c, mi:%d\n", np.x, np.y, _MAP(np), np.mi);
		}
	}
	return path[T.x][T.y].mi;
}

int main()
{
	int i, j;
	while (scanf("%d %d", &m, &n) != EOF)
	{
		for (i = 0; i < m; ++i)
		{
			getchar();
			for (j = 0; j < n; ++j)
			{
				scanf("%c", &map[i][j]);
				if (map[i][j] == 'S')
				{
					S = POINT(i, j);
					map[i][j] = '*';	// 起点没有用了,标记为墙
				}
				else if ('T' == map[i][j])
				{
					T = POINT(i, j);
					map[i][j] = '.';	// 终点标记为走廊,方便bfs统一处理
				}
			}
		}

		printf ("%d\n", bfs());
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

写代码的安徒生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值