728x90
1. 링크
https://www.acmicpc.net/problem/17086
2. 난이도(solved.ac 참고)
실버2
3. 풀이1(메모리 초과)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
input = sys.stdin.readline | |
n, m = tuple(map(int, input().split())) | |
shark_list = [list(map(int, input().split())) for _ in range(n)] | |
ans = 0 | |
def bfs(x, y): | |
q = deque([(x, y, 0)]) | |
dx = [-1, -1, -1, 0, 1, 0, 1, 1] | |
dy = [-1, 0, 1, 1, 1, -1, 0, -1] | |
while q: | |
x, y, distance = q.popleft() | |
for i in range(8): | |
nx = x + dx[i] | |
ny = y + dy[i] | |
if 0 <= nx < n and 0 <= ny < m: | |
if shark_list[nx][ny] == 0: | |
q.append((nx, ny, distance+1)) | |
else: | |
return distance+1 | |
for i in range(n): | |
for j in range(m): | |
if shark_list[i][j] == 0: | |
ans = max(ans, bfs(i, j)) | |
print(ans) |
왜 메모리초과가 떴는지 이해가 안 간다.. 시간 초과도 아니고 메모리 초과라니. ans값을 심지어 배열로 두지도 않았고 상수로 두어 최댓값을 계속 업데이트하는 식으로 진행했는데? 메모리 초과가 뜬 원인을 아는 분이 있다면 댓글로 남겨주시면 좋겠다.
하.. 그래프탐색 문제를 오랜만에 푸니까 정신이 나갔나보다. visited 배열을 당연히 선언해줬어야 했는데 실수했다.
4. 풀이2(AC)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
input = sys.stdin.readline | |
n, m = tuple(map(int, input().split())) | |
shark_list = [list(map(int, input().split())) for _ in range(n)] | |
ans = 0 | |
def bfs(x, y): | |
visited = [[False for _ in range(m)] for _ in range(n)] | |
q = deque([(x, y, 0)]) | |
visited[x][y] = True | |
dx = [-1, -1, -1, 0, 1, 0, 1, 1] | |
dy = [-1, 0, 1, 1, 1, -1, 0, -1] | |
while q: | |
x, y, distance = q.popleft() | |
for i in range(8): | |
nx = x + dx[i] | |
ny = y + dy[i] | |
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]: | |
if shark_list[nx][ny] == 0: | |
q.append((nx, ny, distance+1)) | |
visited[nx][ny] = True | |
else: | |
return distance+1 | |
for i in range(n): | |
for j in range(m): | |
if shark_list[i][j] == 0: | |
ans = max(ans, bfs(i, j)) | |
print(ans) |
풀이1에서 visited를 추가했다.
5. 풀이3(AC)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
input = sys.stdin.readline | |
n, m = tuple(map(int, input().split())) | |
shark_list = [list(map(int, input().split())) for _ in range(n)] | |
q = deque() | |
for i in range(n): | |
for j in range(m): | |
if shark_list[i][j] == 1: | |
q.append([i, j]) | |
ans = 0 | |
dx = [-1, -1, -1, 0, 1, 0, 1, 1] | |
dy = [-1, 0, 1, 1, 1, -1, 0, -1] | |
while q: | |
x, y = q.popleft() | |
for i in range(8): | |
nx = x + dx[i] | |
ny = y + dy[i] | |
if 0 <= nx < n and 0 <= ny < m: | |
if shark_list[nx][ny] == 0: | |
q.append([nx, ny]) | |
shark_list[nx][ny] = shark_list[x][y] + 1 | |
ans = max(ans, shark_list[x][y] + 1) | |
print(ans-1) |
위 풀이와는 다르게 1일 때 큐에 넣고 BFS를 함수 형태로 빼지 않고 바로 시작했다.
뭐 결국 거리의 최댓값을 구하는 것이니 어렵지 않았다만 시간 초과도 아니고 메모리 초과가 뜬 건 이해가 잘 안 된다🤷♂️
'CS > 알고리즘' 카테고리의 다른 글
[Python] BOJ(백준) 2178번 - 미로 탐색 (0) | 2022.03.23 |
---|---|
[Python] BOJ(백준) 10942번 - 팰린드롬? (0) | 2022.03.06 |
[Python] BOJ(백준) 12852번 - 1로 만들기 2 (0) | 2022.03.05 |
[Python] BOJ(백준) 1294번 - 문자열 장식 (0) | 2022.02.21 |
[Python] BOJ(백준) 16496번 - 큰 수 만들기 (0) | 2022.02.20 |