LeetCode每日一题(909. Snakes and Ladders)

给定一个表示棋盘的矩阵,玩家从1号格子开始,每次可以掷骰子移动1到6步。棋盘上存在蛇和梯子,玩家落在这些格子上会跳转到指定的新位置。任务是计算达到最后一格所需的最少步数。如果无法到达,返回-1。提供的解决方案使用哈希映射存储跳跃关系,并用队列进行广度优先搜索来找到最短路径。

You are given an n x n integer matrix board where the cells are labeled from 1 to n2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row.

You start on square 1 of the board. In each move, starting from square curr, do the following:

Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n2)].
This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the size of the board.
If next has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move to next.
The game ends when you reach the square n2.
A board square on row r and column c has a snake or ladder if board[r][c] != -1. The destination of that snake or ladder is board[r][c]. Squares 1 and n2 do not have a snake or ladder.

Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.

For example, suppose the board is [[-1,4],[-1,3]], and on the first move, your destination square is 2. You follow the ladder to square 3, but do not follow the subsequent ladder to 4.
Return the least number of moves required to reach the square n2. If it is not possible to reach the square, return -1.

Example 1:

Input: board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]
Output: 4

Explanation:
In the beginning, you start at square 1 (at row 5, column 0).
You decide to move to square 2 and must take the ladder to square 15.
You then decide to move to square 17 and must take the snake to square 13.
You then decide to move to square 14 and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
This is the lowest possible number of moves to reach the last square, so return 4.

Example 2:

Input: board = [[-1,-1],[-1,3]]
Output: 1

Constraints:

  • n == board.length == board[i].length
  • 2 <= n <= 20
  • grid[i][j] is either -1 or in the range [1, n2].
  • The squares labeled 1 and n2 do not have any ladders or snakes.

这题其实就是个计算最短路径的题目



use std::collections::HashMap;

impl Solution {
    pub fn snakes_and_ladders(board: Vec<Vec<i32>>) -> i32 {
        let n = board.len();
        let mut jumps = HashMap::new();
        for r in 0..n {
            for c in 0..n {
                let row = n - r;
                let label = if row % 2 == 1 { (row - 1) * n + c + 1 } else { row * n - c };
                if board[r][c] != -1 {
                    jumps.insert(label, board[r][c] as usize);
                }
            }
        }
        let mut steps = vec![i32::MAX; n.pow(2) + 1];
        steps[1] = 0;
        let mut queue = vec![1];
        while let Some(curr) = queue.pop() {
            let curr_steps = steps[curr as usize];
            for i in 1..7 {
                let next = curr + i;
                if next > n.pow(2) {
                    break;
                }
                if next == n.pow(2) {
                    steps[next] = steps[next].min(curr_steps + 1);
                    break;
                }
                if let Some(&jump) = jumps.get(&next) {
                    if curr_steps + 1 < steps[jump] {
                        steps[jump] = curr_steps + 1;
                        queue.push(jump);
                    }
                    continue;
                }
                if curr_steps + 1 < steps[next] {
                    steps[next] = curr_steps + 1;
                    queue.push(next);
                }
            }
        }
        if steps[n.pow(2)] == i32::MAX {
            return -1;
        }
        steps[n.pow(2)]
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值