1 parent bff2a75 commit 942b27bCopy full SHA for 942b27b
1 file changed
leetcode3/최원준/463. Island Perimeter.py
@@ -0,0 +1,40 @@
1
+class Solution:
2
+ def islandPerimeter(self, grid: list[list[int]]) -> int:
3
+ n = len(grid)
4
+ m = len(grid[0])
5
+ dx = [0,0,1,-1]
6
+ dy = [1,-1,0,0]
7
+
8
+ def bfs(row, col):
9
+ ans = 0
10
+ grid[row][col] = 2
11
+ queue = Deque()
12
+ queue.append([row,col])
13
14
+ while queue:
15
+ x, y = queue.popleft()
16
+ perimeter = 4
17
+ for dirs in range(4):
18
+ nx = x + dx[dirs]
19
+ ny = y + dy[dirs]
20
21
+ if nx < 0 or n <= nx or ny < 0 or m <= ny:
22
+ continue
23
+ elif 0<=nx<n and 0<=ny<m:
24
+ if grid[nx][ny] == 1:
25
+ queue.append([nx,ny])
26
+ grid[nx][ny] = 2
27
+ perimeter-=1
28
+ elif grid[nx][ny] == 2:
29
30
+ ans += perimeter
31
+ return ans
32
33
34
35
+ for i in range(n):
36
+ for j in range(m):
37
+ if grid[i][j] == 1:
38
+ return bfs(i, j)
39
40
+ return 0
0 commit comments