public static int movingCount(int m, int n, int k) {
boolean[][] visited = new boolean[m][n];
return search(m, n, k, visited, 0, 0);
}
public static int search(int m, int n, int k, boolean[][] visited, int x, int y){
if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y] || compare(x, y, k)){
return 0;
}
visited[x][y] = true;
return 1 + search(m, n, k, visited, x + 1, y) + search(m, n, k, visited, x, y + 1);
}
public static boolean compare(int x, int y, int k){
int sumX = x / 100 + (x % 100) / 10 + x % 10;
int sumY = y / 100 + (y % 100) / 10 + y % 10;
if (sumX + sumY > k){
return true;
}
return false;
}