-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
- 예전 풀이보다 메모리는 많이 쓰고 시간은 줄었다 (?)
# 물고기 M 마리 + 아기상어 1 마리
# 아기상어의 크기의 초기값 = 2
# <규칙>
# 아기상어는 자신의 크기보다 큰 물고기가 있는 칸은 지나갈 수 없다. !!
# 아기상어는 자신의 크기보다 작은 물고기만 먹을 수 있다.
# (크기가 같은 물고기는 먹을 수 없고, 지나갈 수만 있다 !!)
from collections import deque
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
## <아기 상어의 크기>
# 아기 상어는 자신의 크기와 같은 수의 물고기를 먹을 때 마다 크기가 1 증가한다.
# 예를 들어, 크기가 2인 아기 상어는 물고기를 2마리 먹으면 크기가 3이 된다.
size = 2
cnt_score = 0
# 게임 수행
cnt = 0
while True:
fish_lt = []
check = [[-1]* n for _ in range(n)]
for i in range(n):
for j in range(n):
if a[i][j] == 9: # 아기상어 위치에서 스타트
check[i][j] = 0
q = deque([(i, j)])
shark_x, shark_y = i, j
while q:
###print(q)
x, y = q.popleft()
for k in range(4):
nx, ny = x+dx[k], y+dy[k]
if 0<=nx<n and 0<=ny<n and check[nx][ny] == -1:
if a[nx][ny] > size:
continue
check[nx][ny] = (check[x][y] + 1)
q.append((nx, ny))
if a[nx][ny] != 0 and a[nx][ny] < size: fish_lt.append([check[nx][ny], nx, ny])
###and (check[nx][nx] == -1 or check[nx][ny] > (check[x][y] + 1)):
# if a[nx][ny] <= size: ## 빈칸이거나 물고기가 있더라도 크기가 작거나 같은 물고기 (a[nx][ny] == 0 or a[nx][ny] <= size)
# ### 1) 먹을 수 있음
# if a[nx][ny] != 0 and a[nx][ny] < size: ### (빈칸 아니고 물고기인데) + 아기상어보다 크기가 작은 물고기
# fish_lt.append([check[nx][ny], nx, ny, a[nx][ny]])
# check[nx][ny] = (check[x][y] + 1)
# q.append((nx, ny))
# print(q)
# ### 2) 지나가는 것만 가능
# else:
# check[nx][ny] = (check[x][y] + 1)
# q.append((nx, ny))
# print(q)
break
'''
if cnt < 5:
print('### : 초' + str(cnt))
print(size, fish_lt)
for line in check:
print(line)
print()
for line in a:
print(line)
'''
###
# 1) 더이상 먹을 수 있는 물고기가 없을 때
if len(fish_lt) == 0: ### ok == False:
break
###
# 2) 먹을 수 있는 물고기가 1마리 일 때
elif len(fish_lt) == 1:
dist_cnt, eat_x, eat_y = fish_lt[0]
#eat_size = a[nx][ny]
# 3) 2마리 이상일 때
# (dist[i][j] 거리 down / 행 down / 열 down)
else:
fish_lt.sort()
dist_cnt, eat_x, eat_y = fish_lt[0]
#eat_size = a[nx][ny]
# ( 즉, 아기 상어가 먹을 수 있는 물고기가 있는 칸으로 이동했다면, 이동과 동시에 물고기를 먹는다.
# 물고기를 먹으면, 그 칸은 빈 칸이 된다. )
cnt_score += 1
if cnt_score == size:
size += 1
cnt_score = 0 ### (초기화 리셋)
a[shark_x][shark_y] = 0
a[eat_x][eat_y] = 9
cnt += dist_cnt
print(cnt)
Metadata
Metadata
Assignees
Labels
No labels
