JZ66-机器人的运动范围
机器人的运动范围
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1&tab=answerKey
class Solution { public int movingCount(int threshold, int rows, int cols) { if (rows <= 0 || cols <= 0 || threshold < 0) { return 0; } boolean[][] isVisited = new boolean[rows][cols]; int count = movingCountCore(threshold, rows, cols, 0, 0, isVisited); return count; } private int movingCountCore(int threshold, int rows, int cols, int row, int col, boolean[][] isVisited) { if (row < 0 || col < 0 || row >= rows || col >= cols || isVisited[row][col] || sum(row) + sum(col) > threshold) { return 0; } isVisited[row][col] = true; return 1 + movingCountCore(threshold, rows, cols, row + 1, col, isVisited) + movingCountCore(threshold, rows, cols, row - 1, col, isVisited) + movingCountCore(threshold, rows, cols, row, col + 1, isVisited) + movingCountCore(threshold, rows, cols, row, col - 1, isVisited); } private int sum(int num) { int temp = 0; while (num > 0) { temp += num % 10; num /= 10; } return temp; } }