新闻详情

新闻详情

首页 / 资讯中心 / 详情

788 · 迷宫II(bfs)

发布时间:2026/9/1 13:17:36来源:尧图网络
788 · 迷宫II(bfs)
终点检查移到出队时确保返回的是最短路径LintCode 炼码 - 更高效的学习体验class Solution { public: /** * param maze: the maze * param start: the start * param destination: the destination * return: the shortest distance for the ball to stop at the destination */ int shortestDistance(vectorvectorint maze, vectorint start, vectorint destination) { // write your code here int m maze.size(); if (m 0) { return 0; } int n maze[0].size(); if (n 0) { return 0; } // 越大的越小 priority_queuepairint, int, vectorpairint, int, greaterpairint, int que; unordered_mapint, int dist; int begin start[0] * n start[1]; que.push({0, begin}); dist[begin] 0; while (!que.empty()) { auto f que.top(); que.pop(); int x f.second / n; int y f.second % n; if (x destination[0] y destination[1]) { return f.first; } vectorpairint, int nodes get_next_nodes(maze, dist, x, y, m, n); for (auto node : nodes) { if (node.first/n destination[0] node.first%n destination[1]) { //return dist[f.second] node.second; } if (dist.find(node.first) dist.end() || dist[f.second] node.second dist[node.first]) { dist[node.first] dist[f.second] node.second; que.push({dist[node.first], node.first}); } } } return -1; } vectorvectorint direc{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; vectorpairint, int get_next_nodes(vectorvectorint maze, unordered_mapint, int dist, int x, int y, int m, int n) { int QIANG 1; vectorpairint, int result; for (auto d : direc) { int next_x x; int next_y y; bool next false; int count 0; while (next_x d[0] 0 next_y d[1] 0 next_x d[0] m next_y d[1] n maze[next_xd[0]][next_yd[1]] ! QIANG) { count; next_x next_x d[0]; next_y next_y d[1]; next true; } if (next) { int next_node next_x * n next_y; result.push_back(pairint, int(next_node, count)); } } return result; } };class Solution { public: int shortestDistance(vectorvectorint maze, vectorint start, vectorint destination) { vectorvectorint distance(maze.size(), vectorint(maze[0].size(), INT_MAX)); vectorvectorbool visited(maze.size(), vectorbool(maze[0].size())); distance[start[0]][start[1]] 0; dijkstra(maze, start, distance); return distance[destination[0]][destination[1]] INT_MAX ? -1 : distance[destination[0]][destination[1]]; } vectorint minDistance(vectorvectorint distance, vectorvectorbool visited) { vectorint min{-1,-1}; int min_val INT_MAX; for (int i 0; i distance.size(); i) { for (int j 0; j distance[0].size(); j) { if (!visited[i][j] distance[i][j] min_val) { min {i, j}; min_val distance[i][j]; } } } return min; } void dijkstra(vectorvectorint maze, vectorint start, vectorvectorint distance) { vectorvectorint dirs{{0,1},{0,-1},{-1,0},{1,0}}; auto cmp [](vectorint a, vectorint b){return a[2] b[2];}; priority_queuevectorint, vectorvectorint, decltype(cmp) que(cmp); que.push({start[0],start[1],0}); while (!que.empty()) { vectorint s que.top(); que.pop(); if(distance[s[0]][s[1]] s[2]) continue; for (vectorint dir: dirs) { int x s[0] dir[0]; int y s[1] dir[1]; int count 0; while (x 0 y 0 x maze.size() y maze[0].size() maze[x][y] 0) { x dir[0]; y dir[1]; count; } if (distance[s[0]][s[1]] count distance[x - dir[0]][y - dir[1]]) { distance[x - dir[0]][y - dir[1]] distance[s[0]][s[1]] count; que.push({x - dir[0], y - dir[1], distance[x - dir[0]][y - dir[1]]}); } } } } };链接LintCode 炼码class Solution { public: int shortestDistance(vectorvectorint maze, vectorint start, vectorint destination) { vectorvectorint distance(maze.size(), vectorint(maze[0].size(), INT_MAX)); distance[start[0]][start[1]] 0; vectorvectorint dirs{{0, 1} ,{0, -1}, {-1, 0}, {1, 0}}; queuevectorint que; que.push(start); while (!que.empty()) { vectorint s que.front(); que.pop(); for (vectorint dir: dirs) { int x s[0] dir[0]; int y s[1] dir[1]; int count 0; while (x 0 y 0 x maze.size() y maze[0].size() maze[x][y] 0) { x dir[0]; y dir[1]; count; } if (distance[s[0]][s[1]] count distance[x - dir[0]][y - dir[1]]) { distance[x - dir[0]][y - dir[1]] distance[s[0]][s[1]] count; que.push({x - dir[0], y - dir[1]}); } } } return distance[destination[0]][destination[1]] INT_MAX ? -1 : distance[destination[0]][destination[1]]; } };
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

自身免疫与感染免疫:GMCSF/IFNg/IFNα/IL12/IL17/IL23/IL6七因子Luminex检测方案正式落地 2026/9/1 15:00:12

自身免疫与感染免疫:GMCSF/IFNg/IFNα/IL12/IL17/IL23/IL6七因子Luminex检测方案正式落地

自身免疫病和慢性感染的诊断与治疗监测,长期面临一个核心难题:如何精准区分不同的免疫活化模式?Th1(IFNg/IL12)、Th17(IL17/IL23)、I型干扰素(IFNα)和粒细胞-巨噬细胞集…

阅读更多 →
斗鱼直播 8.6 实战:从安装配置到画质弹幕设置与常见问题排查 2026/9/1 15:00:12

斗鱼直播 8.6 实战:从安装配置到画质弹幕设置与常见问题排查

本文以 Windows 平台斗鱼直播客户端(版本 8.6.3)为例,完整记录从安装、画质与弹幕配置到常见问题排查的实操过程。内容面向需要稳定观看游戏赛事与主播直播的玩家和内容从业者,相关步骤均已在本地验证,可作为日常运维与…

阅读更多 →
kkce.com:为什么网站测速要验TLS握手而非只看HTTPS通?-快快测 2026/9/1 15:00:12

kkce.com:为什么网站测速要验TLS握手而非只看HTTPS通?-快快测

把 网站测速​ 收敛成“HTTPS 能打开、地址栏挂锁、TTFB 350ms 就算安全又健康”,是混淆了“传输可达”与“握手成本”的典型降维。HTTPS 通不等于 TLS 握手快——一次 TLS1.2 完整握手要 2-RTT 非对称加密(ServerKeyExchange ClientKeyExchange&#x…

阅读更多 →
基于微信小程序的剧本杀预约系统(源代码+文档+PPT+调试+讲解) 2026/9/1 15:00:12

基于微信小程序的剧本杀预约系统(源代码+文档+PPT+调试+讲解)

温馨提示:本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:本人主页置顶文章(点我)开头有 CSDN 平台…

阅读更多 →
飞书多维表格自动化实战:从数据联动到跨系统集成 2026/9/1 15:00:12

飞书多维表格自动化实战:从数据联动到跨系统集成

多维表格在团队协作、项目管理、数据收集等场景中已经非常常见,但很多使用者仍然停留在手动录入、复制粘贴、人工通知的初级阶段,大量重复性工作消耗了宝贵的时间。实际上,多维表格的自动化能力被严重低估了。通过合理配置,它可以…

阅读更多 →
蔚来算法岗笔试复盘:从KMP到卡尔曼滤波的秋招通关指南 2026/9/1 14:57:11

蔚来算法岗笔试复盘:从KMP到卡尔曼滤波的秋招通关指南

1. 笔试整体画像与题型结构各位准备秋招的学弟学妹们,蔚来算法岗的笔试我今年刚经历过一轮,趁着记忆还热乎,来写一篇完整复盘。先说结论:蔚来算法岗笔试的难度在整车厂里属于第一梯队,不亚于互联网大厂的核心算法岗。其…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞