https://www.acmicpc.net/problem/2468
2468번: 안전 영역
재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는
www.acmicpc.net
'''
2468번 - 안전 영역 / silver 1 / 브루트포스, DFS
'''
import sys
import copy
sys.setrecursionlimit(10**8)
INF = int(1e9)
input = sys.stdin.readline
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
def cnt_dfs(cp, x, y):
cp[x][y] = INF
for i in range(4):
nx = dx[i] + x
ny = dy[i] + y
if 0 <= nx < n and 0 <= ny < n:
if cp[nx][ny] != INF:
cnt_dfs(cp, nx, ny)
# 1. 입력 받기
n = int(input())
graph = []
for _ in range(n):
graph.append(list(map(int, input().split())))
max_cnt = 1
height = 1
# 2. 브루트포스 / height 1~100까지 모든 경우 탐색
while height <= 100:
# 2-1. height 보다 같거나 낮은 영역 INF(물에 잠김)
for i in range(n):
for j in range(n):
if graph[i][j] <= height:
graph[i][j] = INF
# 2-2. graph를 복사 후, dfs 활용하여 cnt 구하기
cp_graph = copy.deepcopy(graph)
cnt = 0
for i in range(n):
for j in range(n):
if cp_graph[i][j] != INF:
cnt_dfs(cp_graph, i, j)
cnt += 1
# 2-3. 구한 cnt와 max_cnt 비교
if max_cnt < cnt:
max_cnt = cnt
height += 1
print(max_cnt)
알고리즘
브루트포스 (모든 경우의 수 알아보기)
DFS (Depth-First Search)
구현 방법 ( 코드 주석 )
# 1. 입력 받기
# 2. 브루트포스 / height 1~100까지 모든 경우 탐색
# 2-1. height 보다 같거나 낮은 영역 INF(물에 잠김)
# 2-2. graph를 복사 후, dfs 활용하여 cnt 구하기
# 2-3. 구한 cnt와 max_cnt 비교
'백준 문제풀이' 카테고리의 다른 글
1655번 - 가운데를 말해요 / gold 2 / 우선순위 큐 (0) | 2023.01.19 |
---|---|
1715번 - 카드 정렬하기 / gold 4 / 우선순위 큐 (0) | 2023.01.18 |
15649번 - N과 M (1) / silver 3 / itertools (0) | 2023.01.16 |
4963번 - 섬의 개수 / silver 2 / DFS (0) | 2023.01.15 |
1058번 - 친구 / silver 2 / 브루트포스 (1) | 2023.01.14 |