HDU - 1175 bfs

本文介绍了一种使用单调队列优化的迷宫寻路算法,该算法能够有效地找到从起点到终点的路径,并且考虑了转弯次数的限制。通过避免使用优先队列减少内存消耗,确保算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

            思路:d[x][y][z]表示以z方向走到(x, y)的转弯次数。

         如果用优先队列会超时,因为加入队列的节点太多,无用的节点不能及时出队,会造成MLE,用单调队列即可。

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int> 
typedef long long LL;
const int maxn = 1000 + 5;
map<PI, int>dir;
const int dx[] = {0,0,-1,1};
const int dy[] = {1,-1,0,0};
int n, m, d[maxn][maxn][4], G[maxn][maxn];
struct node{
	int x, y, tc, dir;
	node() {}
	node(int x, int y, int tc, int dir):x(x), y(y), tc(tc), dir(dir) {
	}
};
bool bfs(int x1, int y1, int x2, int y2) {
	memset(d, inf, sizeof(d));
	queue<node>q;
	for(int i = 0; i < 4; ++i) {
		d[x1][y1][i] = 0;
		q.push(node(x1, y1, 0, i));
	}
	while(!q.empty()) {
		node p = q.front(); q.pop();
		int x = p.x, y = p.y, tc = p.tc, dir = p.dir;
		if(tc > d[x][y][dir]) continue;
		for(int i = 0; i < 4; ++i) {
			int px = x + dx[i], py = y + dy[i];
			if(px < 0 || py < 0 || px >= n || py >= m) continue;
			int pt = tc;
			if(i != dir) ++pt; //发生偏转 
			if(pt > 2) continue;
			if(px == x2 && py == y2) return true;
			if(G[px][py]) continue;
			if(pt < d[px][py][i]) {
				d[px][py][i] = pt;
				q.push(node(px, py, pt, i));
			}
		}
	}
	return false;
}
int main() {
	int q;
	while(scanf("%d%d", &n, &m) == 2 && n && m) {
		for(int i = 0; i < n; ++i) 
			for(int j = 0; j < m; ++j) 
				scanf("%d", &G[i][j]);
		scanf("%d", &q);
		int x1, y1, x2, y2;
		while(q--) {
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			--x1, --y1, --x2, --y2;
			if( G[x1][y1] && G[x2][y2] && G[x1][y1] == G[x2][y2] && bfs(x1, y1, x2, y2)) printf("YES\n");
			else printf("NO\n");
		}
	}
	return 0;
}


如有不当之处欢迎指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值