๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/19237
์ฝ๋ ์ ์ถ ๊ธฐ๋ก (๋ฉ๋ชจ๋ฆฌ ๋ฐ ์๊ฐ)
์ ์ถ ๋ ์ง
@4/20/2021 โ 4/22/2021
๋ฉ๋ชจ๋ฆฌ
KB
์๊ฐ
ms
๋ฉ๋ชจ
โข
๋ฌธ์ ์ดํด๋ถํฐ.. ์ค๋๊ฑธ๋ฆผ..
โข
์ผ์์ใ
ใ
์ใ
์์์ใ
์์์์์์ใ
ใ
์์์์์
๋ฌธ์ ํ์ด
์ ๋ ฅ๊ฐ ์ ๋ฆฌ
1.
shark_info = [x์ขํ, y์ขํ, ๋ฐฉํฅ, ์์ฌ์ฌ๋ถ] ๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด
2.
shark_smell = [์์ด์ ๋ฒํธ, ์ ์ง์๊ฐ] ์ ์ ์ฅํ๋ ๋ฐฐ์ด
3.
shark_prio = ๊ฐ ๋ฐฉํฅ๋ง๋ค ์ฐ์ ์์๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด
์์)
shark_prio[0] = [[2, 3, 1, 4], [4, 1, 2, 3],[3, 4, 2, 1], [4, 3, 1, 2]]
shark_prio[1] = [[x, x, x, x], [x, x, x, x],[x, x, x, x], [x, x, x, x]]
shark_prio[2] = [[x, x, x, x], [x, x, x, x],[x, x, x, x], [x, x, x, x]]
shark_prio[3] = [[x, x, x, x], [x, x, x, x],[x, x, x, x], [x, x, x, x]]
DIRECTION = 2
LIVE = 1
DEAD = 0
N, M, K = map(int, input().split())
shark_info = [[] for _ in range(M)]
shark_smell = [[[0, 0] for _ in range(N)] for _ in range(N)]
shark_prio = [[] for _ in range(M)]
# ์์ด์ [x์ขํ, y์ขํ, ๋ฐฉํฅ, ์์ฌ์ฌ๋ถ]๋ฅผ ์ ์ฅํ๊ณ ์๋ ๋ฐฐ์ด ์
๋ ฅ๋ฐ๊ธฐ
def get_shark_info():
global N, M, shark_info, LIVE
for i in range(N):
_map = list(map(int, input().split()))
for j in range(N):
if _map[j] != 0:
shark_info[_map[j]-1].append(i)
shark_info[_map[j]-1].append(j)
_dir = list(map(int, input().split()))
for i in range(M):
shark_info[i].append(_dir[i]-1)
shark_info[i].append(LIVE)
# ์์ด์ ์ฐ์ ์์๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด ์
๋ ฅ๋ฐ๊ธฐ
def get_shark_prio():
global N, M, shark_prio
for i in range(M):
for j in range(4):
_dir = list(map(int, input().split()))
shark_prio[i].append(_dir)
get_shark_info()
get_shark_prio()
Python
๋ณต์ฌ
ํ์ด ์์
Code
์์ ๋ ๋ค ๋ง์๋๋ฐ.. ํ๋ ธ์ต๋๋ค...ใ
ใ
1%...์๊ฐ์ด๊ณผ ใ
ใ
ใ
์์ด์ ์ด๋์ด ๋์์! ์ผ์ด๋๋ค๋ ๊ฒ์ ๊ณ ๋ คํ๋ฉด 25%๊น์ง ๋ง์ ใ
ใ
ใ
ใ
ใ
ใ
import copy
import sys
input = sys.stdin.readline
X = 0
Y = 1
DIRECTION = 2
LIVE = 3
DEAD = 0
total_time = 0
N, M, K = map(int, input().split())
count_shark = M
shark_info = [[-1, -1, -1, 1] for _ in range(M)]
shark_smell = [[[0, 0] for _ in range(N)] for _ in range(N)]
shark_prio = [[None for _ in range(4)] for _ in range(M)]
next_shark_info = []
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# ์์ด์ [x์ขํ, y์ขํ, ๋ฐฉํฅ, ์์ฌ์ฌ๋ถ]๋ฅผ ์ ์ฅํ๊ณ ์๋ ๋ฐฐ์ด ์
๋ ฅ๋ฐ๊ธฐ
def get_shark_info():
global N, M, shark_info, LIVE
for i in range(N):
_map = list(map(int, input().split()))
for j in range(N):
if _map[j] != 0:
shark_info[_map[j]-1][X] = i
shark_info[_map[j]-1][Y] = j
_dir = list(map(int, input().split()))
for i in range(M):
shark_info[i][DIRECTION] = _dir[i]-1
# ์์ด์ ์ฐ์ ์์๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด ์
๋ ฅ๋ฐ๊ธฐ
def get_shark_prio():
global N, M, shark_prio
for i in range(M):
for j in range(4):
_dir = list(map(int, input().split()))
shark_prio[i][j] = [_dir[0]-1, _dir[1]-1, _dir[2]-1, _dir[3]-1]
# ํ์ฌ์์น์ ์์ด์ ๋์ ๋ฟ๋ฆฌ๊ธฐ
def put_smell():
global shark_info, shark_smell, K
for i in range(M):
_x = shark_info[i][X]
_y = shark_info[i][Y]
if shark_info[i][LIVE] != DEAD:
shark_smell[_x][_y][0] = i
shark_smell[_x][_y][1] = K
# ํด๋น ๋ฒํธ์ ์์ด๊ฐ ์ด์์์ผ๋ฉด ์์ด๋ฒํธ ๋ฐํ
def if_exist(shark_info, x, y):
global M
for i in range(M):
if (shark_info[i][0] == x) and (shark_info[i][1] == y) and (shark_info[i][LIVE] != DEAD):
return i
return -1
# ๋ชจ๋ ์์ด ์์ง์ด๊ธฐ
def move_shark(shark_num, shark_x, shark_y, shark_dir): # ์์ง์ผ ์์ด์ ํ์ฌ์์น
global N, M, shark_info, shark_smell, shark_prio, dx, dy, LIVE, count_shark, next_shark_info
flag1 = 0
flag2 = 0
# ๋น ์นธ์ด ์์ ๋
for i in range(4):
_dir = shark_prio[shark_num][shark_dir][i]
nx = shark_x + dx[_dir]
ny = shark_y + dy[_dir]
# ๊ฒฝ๊ณ๋ด์ ์๊ณ ๋์๊ฐ ์์ผ๋ฉด
if (0 <= nx < N) and (0 <= ny < N) and (shark_smell[nx][ny][1] == 0):
flag1 = 1
exist = if_exist(next_shark_info, nx, ny)
# ์์ด๊ฐ ์์ผ๋ฉด ๊ทธ๋ฅ ๋ฃ์ด์ฃผ๊ธฐ
if exist == -1:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 1
break
# ์์ด๊ฐ ์์ผ๋ฉด ์์๊ฑฐ ๋ฃ์ด์ฃผ๊ธฐ
else:
count_shark -= 1
if exist > shark_num:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 1
next_shark_info[exist][LIVE] = DEAD
break
else:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 0
break
# ๋ชจ๋ ๋์๋ก ๋งํ์ ๋ ์์ ์ ๋์๋ก ์ด๋
if flag1 == 0:
for i in range(4):
_dir = shark_prio[shark_num][shark_dir][i]
nx = shark_x + dx[_dir]
ny = shark_y + dy[_dir]
if (0 <= nx < N) and (0 <= ny < N) and (shark_smell[nx][ny][0] == shark_num) and (shark_smell[nx][ny][1] > 0):
flag2 = 1
exist = if_exist(next_shark_info, nx, ny)
# ์์ด๊ฐ ์์ผ๋ฉด ๊ทธ๋ฅ ๋ฃ์ด์ฃผ๊ธฐ
if exist == -1:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 1
break
# ์์ด๊ฐ ์์ผ๋ฉด ์์๊ฑฐ ๋ฃ์ด์ฃผ๊ธฐ
else:
count_shark -= 1
if exist > shark_num:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 1
next_shark_info[exist][LIVE] = DEAD
break
else:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 0
break
# ์ฐ์ ์์ ๋ฐ๋ผ ์ด๋
if flag2 == 0 and flag1 == 0:
for i in range(4):
_dir = shark_prio[shark_num][shark_dir][0]
nx = shark_x + dx[_dir]
ny = shark_y + dy[_dir]
if (0 <= nx < N) and (0 <= ny < N):
# ์์ด๊ฐ ์์ผ๋ฉด ๊ทธ๋ฅ ๋ฃ์ด์ฃผ๊ธฐ
exist = if_exist(next_shark_info, nx, ny)
if exist == -1:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 1
break
# ์์ด๊ฐ ์์ผ๋ฉด ์์๊ฑฐ ๋ฃ์ด์ฃผ๊ธฐ
else:
count_shark -= 1
if exist > shark_num:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 1
next_shark_info[exist][LIVE] = DEAD
break
else:
next_shark_info[shark_num][X] = nx
next_shark_info[shark_num][Y] = ny
next_shark_info[shark_num][DIRECTION] = _dir
next_shark_info[shark_num][LIVE] = 0
break
# ๋ชจ๋ ๋์์ ์ ์ง ์๊ฐ 1 ์ค์ด๊ธฐ
def decrease_smell():
global N, shark_smell
for i in range(N):
for j in range(N):
if shark_smell[i][j][1] == 1:
shark_smell[i][j][1] = 0
shark_smell[i][j][0] = 0
elif shark_smell[i][j][1] > 1:
shark_smell[i][j][1] = shark_smell[i][j][1] - 1
get_shark_info()
get_shark_prio()
while count_shark > 1: # 1๋ฒ ์์ด๋ง ๋จ์ ๋๊น์ง
total_time += 1
if total_time == 1000:
total_time = -1
break
decrease_smell()
put_smell()
next_shark_info.clear()
next_shark_info = copy.deepcopy(shark_info)
for i in range(M):
next_shark_info[i][0] = -1
next_shark_info[i][1] = -1
for i in range(M):
if shark_info[i][LIVE] != DEAD:
move_shark(i, shark_info[i][X], shark_info[i][Y], shark_info[i][DIRECTION])
shark_info.clear()
shark_info = copy.deepcopy(next_shark_info)
print(total_time)
Python
๋ณต์ฌ