返回介绍

solution / 0500-0599 / 0576.Out of Boundary Paths / README_EN

发布于 2024-06-17 01:03:59 字数 6058 浏览 0 评论 0 收藏 0

576. Out of Boundary Paths

中文文档

Description

There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.

Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

 

Example 1:

Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6

Example 2:

Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12

 

Constraints:

  • 1 <= m, n <= 50
  • 0 <= maxMove <= 50
  • 0 <= startRow < m
  • 0 <= startColumn < n

Solutions

Solution 1

class Solution:
  def findPaths(
    self, m: int, n: int, maxMove: int, startRow: int, startColumn: int
  ) -> int:
    @cache
    def dfs(i, j, k):
      if i < 0 or j < 0 or i >= m or j >= n:
        return 1
      if k <= 0:
        return 0
      res = 0
      for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
        x, y = i + a, j + b
        res += dfs(x, y, k - 1)
        res %= mod
      return res

    mod = 10**9 + 7
    return dfs(startRow, startColumn, maxMove)
class Solution {
  private int m;
  private int n;
  private int[][][] f;
  private static final int[] DIRS = {-1, 0, 1, 0, -1};
  private static final int MOD = (int) 1e9 + 7;

  public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
    this.m = m;
    this.n = n;
    f = new int[m + 1][n + 1][maxMove + 1];
    for (var a : f) {
      for (var b : a) {
        Arrays.fill(b, -1);
      }
    }
    return dfs(startRow, startColumn, maxMove);
  }

  private int dfs(int i, int j, int k) {
    if (i < 0 || i >= m || j < 0 || j >= n) {
      return 1;
    }
    if (f[i][j][k] != -1) {
      return f[i][j][k];
    }
    if (k == 0) {
      return 0;
    }
    int res = 0;
    for (int t = 0; t < 4; ++t) {
      int x = i + DIRS[t];
      int y = j + DIRS[t + 1];
      res += dfs(x, y, k - 1);
      res %= MOD;
    }
    f[i][j][k] = res;
    return res;
  }
}
class Solution {
public:
  int m;
  int n;
  const int mod = 1e9 + 7;
  int f[51][51][51];
  int dirs[5] = {-1, 0, 1, 0, -1};

  int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
    memset(f, 0xff, sizeof(f));
    this->m = m;
    this->n = n;
    return dfs(startRow, startColumn, maxMove);
  }

  int dfs(int i, int j, int k) {
    if (i < 0 || i >= m || j < 0 || j >= n) return 1;
    if (f[i][j][k] != -1) return f[i][j][k];
    if (k == 0) return 0;
    int res = 0;
    for (int t = 0; t < 4; ++t) {
      int x = i + dirs[t], y = j + dirs[t + 1];
      res += dfs(x, y, k - 1);
      res %= mod;
    }
    f[i][j][k] = res;
    return res;
  }
};
func findPaths(m int, n int, maxMove int, startRow int, startColumn int) int {
  f := make([][][]int, m+1)
  for i := range f {
    f[i] = make([][]int, n+1)
    for j := range f[i] {
      f[i][j] = make([]int, maxMove+1)
      for k := range f[i][j] {
        f[i][j][k] = -1
      }
    }
  }
  var mod int = 1e9 + 7
  dirs := []int{-1, 0, 1, 0, -1}
  var dfs func(i, j, k int) int
  dfs = func(i, j, k int) int {
    if i < 0 || i >= m || j < 0 || j >= n {
      return 1
    }
    if f[i][j][k] != -1 {
      return f[i][j][k]
    }
    if k == 0 {
      return 0
    }
    res := 0
    for t := 0; t < 4; t++ {
      x, y := i+dirs[t], j+dirs[t+1]
      res += dfs(x, y, k-1)
      res %= mod
    }
    f[i][j][k] = res
    return res
  }
  return dfs(startRow, startColumn, maxMove)
}

Solution 2

class Solution {
  public int findPaths(int m, int n, int N, int i, int j) {
    final int MOD = (int) (1e9 + 7);
    final int[] dirs = new int[] {-1, 0, 1, 0, -1};
    int[][] f = new int[m][n];
    f[i][j] = 1;
    int res = 0;
    for (int step = 0; step < N; ++step) {
      int[][] temp = new int[m][n];
      for (int x = 0; x < m; ++x) {
        for (int y = 0; y < n; ++y) {
          for (int k = 0; k < 4; ++k) {
            int tx = x + dirs[k], ty = y + dirs[k + 1];
            if (tx >= 0 && tx < m && ty >= 0 && ty < n) {
              temp[tx][ty] += f[x][y];
              temp[tx][ty] %= MOD;
            } else {
              res += f[x][y];
              res %= MOD;
            }
          }
        }
      }
      f = temp;
    }
    return res;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文