1391. Check if There is a Valid Path in a Grid (M)

Check if There is a Valid Path in a Grid (M)

Given a m x n grid. Each cell of the grid represents a street. The street of grid[i][j] can be:

  • 1 which means a street connecting the left cell and the right cell.
  • 2 which means a street connecting the upper cell and the lower cell.
  • 3 which means a street connecting the left cell and the lower cell.
  • 4 which means a street connecting the right cell and the lower cell.
  • 5 which means a street connecting the left cell and the upper cell.
  • 6 which means a street connecting the right cell and the upper cell.

在这里插入图片描述

You will initially start at the street of the upper-left cell (0,0). A valid path in the grid is a path which starts from the upper left cell (0,0) and ends at the bottom-right cell (m - 1, n - 1). The path should only follow the streets.

Notice that you are not allowed to change any street.

Return true if there is a valid path in the grid or false otherwise.

Example 1:

在这里插入图片描述

Input: grid = [[2,4,3],[6,5,2]]
Output: true
Explanation: As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).

Example 2:

在这里插入图片描述

Input: grid = [[1,2,1],[1,2,1]]
Output: false
Explanation: As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)

Example 3:

Input: grid = [[1,1,2]]
Output: false
Explanation: You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).

Example 4:

Input: grid = [[1,1,1,1,1,1,3]]
Output: true

Example 5:

Input: grid = [[2],[2],[2],[2],[2],[2],[6]]
Output: true

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • 1 <= grid[i][j] <= 6

题意

判断沿着一个由路径块组合成的路径能否从左上角走到右下角。

思路

定义4个方向:0 - 左,1 - 上,2 - 右,3 - 下。

判断能否走到一个路径块[i, j]的几个判断依据:

  1. i、j坐标是否有效;
  2. 当前路径块与来时的上一个块是否相连。

除了从第左上角的块走到下一个块有两种可能,从其他块走到下一个块都只有一种情况(即只能沿着路走且不能回头)。

使用DFS处理。


代码实现

class Solution {
    int m, n;
    // 定义沿着左上右下四个方向走时坐标的变化
    int[] iPlus = {0, -1, 0, 1};
    int[] jPlus = {-1, 0, 1, 0};
    int[][] path = {{0, 2}, {1, 3}, {0, 3}, {2, 3}, {0, 1}, {1, 2}};

    public boolean hasValidPath(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        if (m == 1 && n == 1) {
            return true;
        }
        return judge(grid, -1, 0, 0);
    }

    // pre为从上一个路径块来时的方向
    private boolean judge(int[][] grid, int pre, int i, int j) {
        // 先判断i,j是否合法
        if (i < 0 || i >= m || j < 0 || j >= n) {
            return false;
        }
        // 当前路径块的类型及它连接的两个方向
        int type = grid[i][j] - 1;
        int directionA = path[type][0], directionB = path[type][1];
        // 如果当前块是起点,需要向两个方向dfs
        if (pre == -1) {
            return judge(grid, (directionA + 2) % 4, i + iPlus[directionA], j + jPlus[directionA])
                    || judge(grid, (directionB + 2) % 4, i + iPlus[directionB], j + jPlus[directionB]);
        }
        // 判断当前块与上一个路径块是否相连
        if (pre != directionA && pre != directionB) {
            return false;
        }
        // 已经走到右下角则直接返回
        if (i == m - 1 && j == n - 1) {
            return true;
        }
        // 沿着当前路径走到下一个路径块
        int nextDir = pre == directionA ? directionB : directionA;
        return judge(grid, (nextDir + 2) % 4, i + iPlus[nextDir], j + jPlus[nextDir]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值