Skip to content

Commit ac7d3bc

Browse files
committed
[BOJ] #2468. 안전 영역 / 실버1 / 50분 / 힌트, 성공
1 parent 387b4b8 commit ac7d3bc

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
sys.setrecursionlimit(100000)
3+
4+
input = sys.stdin.readline
5+
6+
# 방향벡터
7+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
8+
9+
def dfs(x, y):
10+
# 깊이 탐색을 하며 안전 영역 탐색
11+
visited[x][y] = True
12+
for dx, dy in directions:
13+
nx, ny = x + dx, y + dy
14+
if 0 <= nx < N and 0 <= ny < N and \
15+
not visited[nx][ny] and map[nx][ny] > h:
16+
dfs(nx, ny)
17+
18+
19+
N = int(input())
20+
map = [list(map(int, input().split())) for _ in range(N)]
21+
max_height = 0
22+
23+
# 맵 내의 최대값 구하기
24+
for m in map:
25+
max_height = max(max_height, max(m))
26+
27+
answer = 0
28+
29+
for h in range(0, max_height + 1): # 물이 잠기지 않는 상황을 고려하여 0부터 시작
30+
visited = [[False for _ in range(N)] for _ in range(N)]
31+
count = 0
32+
33+
for i in range(N):
34+
for j in range(N):
35+
# 아직 방문하지 않았고 h(높이) 이상인 지점이라면 DFS 탐색을 한다.
36+
if not visited[i][j] and map[i][j] > h:
37+
dfs(i, j)
38+
count += 1 # DFS 탐색을 마치면 하나의 안전 영역이므로 count
39+
40+
answer = max(answer, count) # 안전영역 최대 개수 계산
41+
42+
print(answer)

0 commit comments

Comments
 (0)