https://www.acmicpc.net/problem/2468
문제
- 비가 옴에 따라 물에 잠기는 높이가 0 부터 최고 높이까지 증가함.
- 잠기는 영역이 생김에 따라 '안전영역' 뭉치의 개수가 계속 바뀌게 됨.
- 안전영역 뭉치 개수의 최대값을 구하는 문제
용답노트
- for 문 안에 같은 변수가 들어갔음.
- (수정전) x,y = q.pop()
- (수정후) x0, y0 = q.pop()
- 코드를 수정하면서 q= collections.deque()의 적절한 위치가 변경되었는데 이것을 체크하지 못했음.
답안
import sys
import collections
n = int(sys.stdin.readline().strip())
graph =[]
max_height=0
for _ in range(n):
tmp = list(map(int,sys.stdin.readline().strip().split()))
max_height=max(max_height, max(tmp))
graph.append(tmp)
answer = 0
dx=[1,0,-1,0]
dy=[0,1,0,-1]
#비오는 양 rain을 올려가면서 answer 값을 저장. 최대 answer를 찾는다.
for rain in range(max_height):
visited=[[False] *n for _ in range(n)]
cnt =0
for x in range(n):
for y in range(n):
#조건 확인. 만족할 경우 ans +1 하고 주변 방문시키기
if visited[x][y] == 0 and graph[x][y] > rain:
visited[x][y]=1
cnt +=1
q= collections.deque()
q.append((x,y))
while q:
x0,y0 = q.pop()
for i in range(4):
nx = x0 + dx[i]
ny = y0 + dy[i]
if nx >= 0 and nx <n and ny >=0 and ny <n:
if visited[nx][ny] == 0 and graph[nx][ny] > rain:
visited[nx][ny]=1
q.append((nx,ny))
answer = max(cnt,answer)
print(answer)
'sw > 알고리즘' 카테고리의 다른 글
백준 1213 팰린드롬 만들기 (0) | 2022.12.07 |
---|---|
백준 2178 미로탐색 (0) | 2022.12.03 |
파이썬 자료형 별 시간복잡도(List, Dict,set) (0) | 2022.11.28 |
(작성중)백준 9996 한국이 그리울 땐 서버에 접속하지 (0) | 2022.11.26 |
백준 5613 계산기 프로그램 (0) | 2022.11.14 |