-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2061.java
More file actions
executable file
·48 lines (45 loc) · 1.49 KB
/
Copy pathSolution2061.java
File metadata and controls
executable file
·48 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution2061 {
private int[] actions;
private int[][] directions;
private int res;
public int numberOfCleanRooms(int[][] room) {
res = 0;
actions = new int[] {0, 1, 2, 3};
directions = new int[][]{
new int[] {0, 1},
new int[] {1, 0},
new int[] {0, -1},
new int[] {-1, 0},
};
int[][] visited = new int[room.length][room[0].length];
dfs(room, visited, 0, 0, 0);
return res;
}
public void dfs(int[][] room, int[][] visited, int x, int y, int action) {
/**
* 右移,下移,左移,上移
* 0 1 2 3
*/
visited[x][y]++;
if (visited[x][y] == 1) res++;
for (int i = 0; i < 4; i++) {
int temp = (action + i) % 4;
if (go(room, x, y, temp)) {
dfs(room, visited, x + directions[temp][0], y + directions[temp][1], actions[temp]);
break;
}
else {
visited[x][y]++;
}
if (visited[x][y] >= 4) return;
}
}
public boolean go(int[][] room, int x, int y, int action) {
if (x + directions[action][0] >= 0 && x + directions[action][0] < room.length
&& y + directions[action][1] >= 0 && y + directions[action][1] < room[0].length
&& room[x + directions[action][0]][y + directions[action][1]] != 1
)
return true;
return false;
}
}