이번 주 알고리즘 문제는
[463. Island Perimeter]
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]]
Output: 4
Example 3:
Input: grid = [[1,0]]
Output: 4
Constraints:
- row == grid.length
- col == grid[i].length
- 1 <= row, col <= 100
- grid[i][j] is 0 or 1.
- There is exactly one island in grid.
[풀이 계획]
dfs를 이용해서 문제를 풀 예정입니다.
키 포인트는 그래프에서 한점을 골라 4가지의 방향을 탐색하며 둘레를 구하는데,
이때 둘레는 범위 밖에 있거나, 땅이 아니면 둘레를 1을 추가하는 방식으로 전개할 예정입니다.
'Algorithm' 카테고리의 다른 글
[알고리즘 스터디] 10월 4주차 문제 0커플, 폴더폰자판 (1) | 2022.10.23 |
---|---|
[알고리즘 스터디] 9월 1주차 문제 4Sum, 유기농 배추 (0) | 2022.08.31 |
[알고리즘 스터디] 8월 4주차 문제 Add Two Numbers, Maximum Number of Groups Entering a Competition (0) | 2022.08.24 |
[백준] 4375번 1 파이썬 초간단 문제 풀이 (0) | 2022.03.17 |