leetcode链接:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/
题目
解答
深度遍历搜索:向下和向右搜索
class Solution {
public int movingCount(int m, int n, int k) {
boolean[][] board = new boolean[m][n];
return dfs(board, 0, 0, k);
}
private int dfs(boolean[][] board, int x, int y, int k) {
// 终止条件:1.到达不可到的点 2.或者边界
if (get(x) + get(y) > k || x >= board.length || y >= board[0].length || board[x][y]) {
return 0;
}
board[x][y] = true;
return dfs(board, x + 1, y, k) + dfs(board, x, y + 1, k) + 1;
}
private int get(int num) {
int res = 0;
while (num > 0) {
res += num % 10;
num /= 10;
}
return res;
}
}