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

909. 蛇梯棋

https://leetcode.cn/problems/snakes-and-ladders/description/?envType=study-plan-v2&envId=top-interview-150

思路:题目要求我们使用最小的步数走到终点(注意不能走回头路,传送不算),那我们的想法就很明确了,我们遍历所有可以走到终点的方法然后找出最小的那个就行,怎么找到所有的方法呢,bfs就很适合我们,每一层就代表我们这一步的所有落点情况。


我们可以先分析一下这个矩阵的规律,

如果nn&1==0

        并且 x&1 == 1(奇数层),board[x][y] = (nn-x-1)* nn + (y + 1)
              *x&1 == 0(偶数层), board[x][y] = (nn-x-1)* nn + (nn - y)
如果nn&1==1上述情况就反一下。


所以我们知道当前的下标就可以推出在board中的二维下标
nn&1 == 0:
如果((index-1)/nn  + 1) & 1 = 0(奇数层),x = nn - ((index-1)/nn  + 1) , y = (index - 1)% nn
如果((index-1)/nn  + 1) & 1 = 1(偶数层),x = nn - ((index-1)/nn  + 1) , y = nn - ((index - 1) % nn) - 1
nn&1 == 1就把奇偶的规律反一下

class Solution {public int snakesAndLadders(int[][] board) {int n = board.length * board[0].length; // 终点int currIndex = 1; // 当前下标int currStepNum = 0; // 当前步数Queue<Integer> currStep = new LinkedList<>(); // 当前位置currStep.offer(currIndex);Queue<Integer> nextStep = new LinkedList<>(); // 下一步数所有可落点下标boolean[] signed = new boolean[n + 1]; // 标记点有没有走过signed[1] = true;int ans = Integer.MAX_VALUE;while (!currStep.isEmpty()) {currStepNum++;if(currStepNum > ans) break;while (!currStep.isEmpty()) {int curr = currStep.poll();for (int i = 1; i <= 6; i++) {int nextIndex = getNextIndex(board, curr, i);if(nextIndex == n) {// 走到终点了ans = Math.min(ans, currStepNum);continue;}if(nextIndex == -1 || signed[nextIndex]) {// 超出边界了或走过了continue;}nextStep.add(nextIndex); // 加入下一步signed[nextIndex] = true;}}// 交换currStep = nextStep;nextStep = new LinkedList<>();}return ans == Integer.MAX_VALUE ? -1 : ans;}public int getNextIndex(int[][] board, int currIndex, int stepLength) {int nn = board.length;int nextIndex = currIndex + stepLength;if(nextIndex > nn * nn) {return -1;}int x = nn - ((nextIndex - 1) / nn + 1);int y = 0;if((nn & 1) == 0) {if((x & 1) == 1) {y = (nextIndex - 1) % nn;} else {y = nn - ((nextIndex - 1) % nn) - 1;}} else {if ((x & 1) == 1) {y = nn - ((nextIndex - 1) % nn) - 1;} else {y = (nextIndex - 1) % nn;}}// 如果有梯子或蛇,则跳到指定位置return board[x][y] == -1 || nextIndex == nn*nn ? nextIndex : board[x][y];}public static void main(String[] args) {Solution solution = new Solution();System.out.println(solution.snakesAndLadders(new int[][]{{1, 1, -1}, {1, 1, 1}, {-1, 1, 1}}));}
}

相关文章:

  • NODEEDITOR
  • idea 安装飞算-javaAI 插件使用
  • AG-UI 协议是什么?MCP、A2A 后,AI 领域又新增 AG-UI 协议
  • 初识 java
  • Kubernetes in action-配置和应用升级
  • 关于 Web 漏洞原理与利用:2. XSS(跨站脚本攻击)
  • 斜齿轮直列齿轮箱市场分析报告:驱动因素、挑战及前景预测
  • 初学c语言16(内存函数)
  • 【Fine-Tuning】大模型微调高阶技术点概要
  • 【Unity】Unity中将字典序列化
  • YOLO模型predict(预测/推理)的参数设置
  • Java集合框架解析:从基础到底层源码
  • 题目练习之综合运用
  • 【PhysUnits】4.4 零类型(Z0)及其算术运算(zero.rs)
  • 《解锁具身智能社交密码:文化适配算法探秘》
  • 小目标检测层优化+多模态数据增强——YOLOv5在油气管道环焊缝缺陷识别的创新应用
  • SymPy | 隐函数导数求解:从基础到高阶的完整指南
  • 卓力达手撕垫片:精密制造的创新解决方案与多领域应用
  • docker运行Redis
  • 基于DeepSeek的智能客服系统实践与创新
  • 国家发改委谈整治“内卷式”竞争:加力破除地方保护和市场分割,遏制落后产能无序扩张
  • 视频丨习近平在河南洛阳市考察调研
  • 学人、学术、学科、学脉:新时代沾溉下的中国西方史学史
  • 聚焦智能浪潮下的创业突围,“青年草坪创新创业湃对”走进北杨人工智能小镇
  • 浙江推动人工智能终端消费:家居机器人纳入以旧换新补贴范围
  • 西域都护府博物馆今日在新疆轮台县开馆