当前位置: 首页 > news >正文

Leetcode 921 Shortest Path in Binary Matrix

题意:求二维矩阵中往8个方向移动的话,从左上方到右下方移动的最短路径
https://leetcode.com/problems/shortest-path-in-binary-matrix/description/

解答:bfs易得

class Solution {
public:int shortestPathBinaryMatrix(vector<vector<int>>& grid) {int m = grid.size();int n = grid[0].size();int ret = 0;vector<vector<int>> vis(m, vector<int>(n, 0));if(grid[0][0] == 1 || grid[m-1][n-1] == 1) {return -1;}int x[] = {1, 1, 0, -1, -1, -1, 0, 1};int y[] = {0, 1, 1, 1,   0,-1, -1 , -1};queue<pair<int, int>> q;q.push({0,0});vis[0][0] = 1;while(q.size()) {int qS = q.size();for(int i = 0; i < qS; i++) {auto node = q.front();q.pop();if (node.first == m-1 && node.second == n-1) return ret+1;for (int k = 0 ; k < 8; k++) {int dx = node.first + x[k];int dy = node.second + y[k];if(dx >= 0 && dx < m && dy >= 0 && dy < n && grid[dx][dy] == 0 && vis[dx][dy] == 0) {q.push({dx,dy});vis[dx][dy] = 1;}}}ret += 1;}return -1;}
};
http://www.lryc.cn/news/463334.html

相关文章:

  • 第二十二篇——菲欧几何:相对论的数学基础是什么?
  • 【AI整合包及教程】EchoMimic:开创数字人新时代,让静态图像“活”起来!
  • ArcGIS 最新底图服务地址
  • 【服务器部署】Docker部署小程序
  • 三菱FX PLC设计一个电子钟程序实例
  • 妇女、商业与法律(WBL)(1971-2023年)
  • python 卸载、安装、virtualenv
  • ubuntu24.0离线安装Ollama和纯cpu版本以及对接Spring AI
  • 机器学习核心:监督学习与无监督学习
  • 服务器托管的优缺点有哪些?
  • RestClient查询文档排序、分页和高亮
  • API项目5:申请签名 在线调用接口
  • Google FabricDiffusion:开启3D虚拟试穿新篇章
  • 【开发语言】c++的发展前景
  • 【机器学习】图像识别——计算机视觉在工业自动化中的应用
  • lstm基础知识
  • Linux :at crontab简述
  • Python,Swift,Haskell三种语言在使用正则表达式上的方法对比
  • leetcode力扣刷题系列——【三角形的最大高度】
  • 工业相机解决方案
  • 设计一个高效的日志分析系统:自动检测错误日志的实用指南
  • 英语学习--如果你的父母不听你的话
  • LeetCode:3258.统计满足k约束的子串数量 I(滑动窗口 Java)
  • 如果用Java设计MySQL中表级锁、行级锁和间歇锁会是怎么的?
  • GIT batch的支持中文的方法和系统建议
  • 骑砍霸主MOD天芒传奇Ⅱ·前传-序章
  • 神经网络量化基础
  • 飞机大战告尾
  • 支持向量机SVM原理详解
  • 使用JMeter进行Spring Boot接口的压力测试