public class Solution {
    // 仅需new一个访问数组
    public int movingCount(int threshold, int rows, int cols) {
        boolean[][] visited = new boolean[rows][cols];
        return dfs(threshold, 0, 0, visited);
    }

    // 深度优先搜索
    private static int dfs(int threshold, int x, int y, boolean[][] visited) {
        int m = visited.length, n = visited[0].length;
        // 如果越界、访问过、大于阈值,直接返回0个。
        if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y] || sum(x, y) > threshold) {
            return 0;
        }
        visited[x][y] = true;
        // 四个方向,加上本身1个
        return 1 + dfs(threshold, x + 1, y, visited)+ dfs(threshold, x - 1, y, visited) 
            + dfs(threshold, x, y + 1, visited)+ dfs(threshold, x, y - 1, visited);
    }

    // 计算两数位和
    private static int sum(int a, int b) {
        int ret = 0;
        while (a != 0) {
            ret += a % 10;
            a /= 10;
        }
        while (b != 0) {
            ret += b % 10;
            b /= 10;
        }
        return ret;
    }
}