当前位置: 首页> 财经> 金融 > 商业网络平台_有实力高端网站设计地址_怎么做公司网站推广_国产免费crm系统有哪些

商业网络平台_有实力高端网站设计地址_怎么做公司网站推广_国产免费crm系统有哪些

时间:2025/7/11 7:45:02来源:https://blog.csdn.net/Pisasama/article/details/145844253 浏览次数:0次
商业网络平台_有实力高端网站设计地址_怎么做公司网站推广_国产免费crm系统有哪些

力扣1210. 穿过迷宫的最少移动次数

题目

在这里插入图片描述

题目解析及思路

题目要求找到最短的贪吃蛇到出口的路径,并且必须横着出去

就是比一般bfs多了一维,贪吃蛇当前水平还是竖直

代码

class Solution {static constexpr int dir[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
public:int minimumMoves(vector<vector<int>>& grid) {int n = grid.size();queue<tuple<int,int,int>> q;q.emplace(0,0,0);vector<vector<vector<int>>> dis(n, vector<vector<int>>(n, vector<int>(2, INT_MAX)));dis[0][0][0] = 0;auto bfs = [&]()->void{while(!q.empty()){auto [x,y,s] = q.front();q.pop();for(int i=0;i<3;i++){int sx = x + dir[i][0];int sy = y + dir[i][1];//更新状态int ss = s ^ dir[i][2];//求头的坐标int hx = sx + ss;int hy = sy + (ss ^ 1);//头的位置是1、尾的位置是1、尾右下的位置是1,都不行if(hx >= n || hy >= n || sx >= n || sy >= n || ss >= 2 || grid[hx][hy] == 1 || grid[sx][sy] == 1 || (i == 2 && grid[x+1][y+1] == 1)){continue;}if(dis[sx][sy][ss] > dis[x][y][s] + 1){dis[sx][sy][ss] = dis[x][y][s] + 1;q.emplace(sx,sy,ss);}}}};bfs();return dis[n-1][n-2][0] >= INT_MAX/2 ? -1 : dis[n-1][n-2][0];}
};
关键字:商业网络平台_有实力高端网站设计地址_怎么做公司网站推广_国产免费crm系统有哪些

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: