LeetCode《程序员面试金典》面试题 08.02. 迷路的机器人

题目

在这里插入图片描述

解题

解题一:DFS + 剪枝

在这里插入图片描述

// javascript
var pathWithObstacles = function(obstacleGrid) {
    if (obstacleGrid.length === 0 || obstacleGrid[0].length === 0) return [];
    let path = [];
    if (getPath(obstacleGrid, path, obstacleGrid.length - 1, obstacleGrid[0].length - 1) === true) {
        return path;
    }
    return [];
};

var getPath = function(obstacleGrid, path, row, col) {
	// 如果越界或无效,则直接返回
    if (row < 0 || col < 0 || obstacleGrid[row][col] === 1) return false;
    let isAtOrigin = (row === 0) && (col === 0);
    if (isAtOrigin || getPath(obstacleGrid, path, row - 1, col) || getPath(obstacleGrid, path, row, col - 1)) {
    	// 如果有一条路径从起点通向这里,把它添加到我的位置
        path.push([row, col]);
        return true;
    }
    return false;
}

在这里插入图片描述

// javascript
var pathWithObstacles = function(obstacleGrid) {
    if (obstacleGrid.length === 0 || obstacleGrid[0].length === 0) return [];
    let path = [],
        rows = obstacleGrid.length - 1,
        cols = obstacleGrid[0].length - 1;
    if (getPath(obstacleGrid, path, rows, cols) === true) {
        return path;
    }
    return [];
};

var getPath = function(obstacleGrid, path, row, col) {
    if (row < 0 || col < 0 || obstacleGrid[row][col] === 1) return false;
    obstacleGrid[row][col] = 1; // 就加了这一句,表示已经 visited
    let isAtOrigin = (row === 0) && (col === 0);
    if (isAtOrigin || getPath(obstacleGrid, path, row - 1, col) || getPath(obstacleGrid, path, row, col - 1)) {
        path.push([row, col]);
        return true;
    }
    return false;
}

仅加一句:obstacleGrid[row][col] = 1;,上面直接在 obstacleGrid 上进行了修改,如果不想改变改变数组,可以额外创建一个 visited 数组 或者 Set() 来记录。注意:

let visited = new Set();
visited.add([0, 0]);
visited.has([0, 0]); // false
visited.add(0 + '-' + 0);
visited.has(0 + '-' + 0); // true

解题二:回溯法

// javascript
let rows, cols;
var pathWithObstacles = function(obstacleGrid) {
    if (obstacleGrid.length === 0 || obstacleGrid[0].length === 0) return []; // 空网格
    rows = obstacleGrid.length;
    cols = obstacleGrid[0].length;
    if (obstacleGrid[rows - 1][cols - 1] === 1) return []; // 终点有障碍
    let path = [], visited = new Array(rows).fill(false).map(arr => new Array(cols).fill(false));
    backTrack(obstacleGrid, 0, 0, path, visited);
    return path;
};

var backTrack = function(obstacleGrid, row, col, path, visited) {
    // 越界,有障碍,已访问
    if (row >= rows || col >= cols || obstacleGrid[row][col] === 1 || visited[row][col] === true)
        return false;
    visited[row][col] = true;
    path.push([row, col]);
    let isAtDes = (row === rows - 1) && (col === cols - 1); // 是否到达终点
    // 到达终点 或 向下能到达终点 或 向右能到达终点
    if (isAtDes === true || 
        backTrack(obstacleGrid, row + 1, col, path, visited) === true ||
        backTrack(obstacleGrid, row, col + 1, path, visited) === true)
        return true;
    // 既不能向下也不能向右,是个死胡同,撤销选择
    path.pop();
    return false;
}

比较 解题一和二,可以发现从终点往回寻找路径的代码会更加简单。

解题三:动态规划

在这里插入图片描述

// javascript
var pathWithObstacles = function(obstacleGrid) {
    if (obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return [];
    let path = [],
        rows = obstacleGrid.length,
        cols = obstacleGrid[0].length;
    // 如果是 1,有障碍物,有 0 条路能到 [0, 0]
    // 如果是 0,无障碍物,有 1 条路能到 [0, 0]
    obstacleGrid[0][0] ^= 1;
    for (let i = 1; i < cols; i++) {
    	// 如果是 1,有障碍物,有 0 条路能到 [0, i]
    	// 如果是 0,无障碍物,参照 [0, i-1] 的路径数,因为在第一行,仅能从左边到达
        obstacleGrid[0][i] = obstacleGrid[0][i] == 1 ? 0 : obstacleGrid[0][i - 1];
    }
    for (let i = 1; i < rows; i++) {
        obstacleGrid[i][0] = obstacleGrid[i][0] == 1 ? 0 : obstacleGrid[i - 1][0];
        for (let j = 1; j < cols; j++) {
            obstacleGrid[i][j] = obstacleGrid[i][j] == 1 ? 0 : obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
        }
    }
    // 如果 [rows - 1, cols - 1] 可到达,那么去寻找 path
    if (obstacleGrid[rows - 1][cols - 1] > 0) {
        getPath(obstacleGrid, path, rows - 1, cols - 1);
    }
    return path;
};

var getPath = function(obstacleGrid, path, row, col) {
    path.unshift([row, col]);
    if (row > 0 && obstacleGrid[row - 1][col] > 0)
        getPath(obstacleGrid, path, row - 1, col);
    else if (col > 0 && obstacleGrid[row][col - 1] > 0)
        getPath(obstacleGrid, path, row, col - 1);
}

时间复杂度: O ( r ∗ c ) O(r * c) O(rc),空间复杂度: O ( r + c ) O(r + c) O(r+c)

如果额外构建一个 dp 数组来存储,空间复杂度是 O ( r ∗ c ) O(r * c) O(rc)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值