-
Notifications
You must be signed in to change notification settings - Fork 0
/
IslandPerimeter.java
77 lines (59 loc) · 1.8 KB
/
IslandPerimeter.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Solution {
private int[] dy = {-1, 0, 1, 0};
private int[] dx = {0, 1, 0, -1};
public int islandPerimeter(int[][] grid) {
int r = 0, c = 0;
for(int i=0; i<grid.length; i++){
for(int j=0; j<grid[i].length; j++){
if(grid[i][j] == 1){
r = i; c = j;
break;
}
}
}
return bfs(r, c, grid);
}
class Pair{
int r, c;
Pair(int r, int c){
this.r = r;
this.c = c;
}
}
private int bfs(int r, int c, int[][] grid){
ArrayList<Pair> q = new ArrayList<>();
boolean[][] visited = initBool(grid.length, grid[grid.length - 1].length);
int ans = 0;
q.add(new Pair(r, c));
visited[r][c] = true;
while(!q.isEmpty()){
Pair pii = q.get(0);
int y = pii.r;
int x = pii.c;
q.remove(0);
int cnt = 0;
for(int i=0; i<4; i++){
int movey = dy[i] + y;
int movex = dx[i] + x;
if(movey < 0 || movey >= grid.length || movex < 0 || movex >= grid[grid.length-1].length) continue;
if(grid[movey][movex] == 1){
cnt++;
if(!visited[movey][movex]){
q.add(new Pair(movey, movex));
visited[movey][movex] = true;
}
}
}
ans += 4-cnt;
}
return ans;
}
boolean[][] initBool(int r, int c){
boolean[][] tmp = new boolean[r][c];
for(int i=0; i<r; i++){
for(int j=0; j<c; j++)
tmp[i][j] = false;
}
return tmp;
}
}