Search
Duplicate
๐Ÿฅ‡

์–ด๋ฅธ ์ƒ์–ด

์ฃผ์ฐจ
๋ฌธ์ œ๋ฒˆํ˜ธ
19237
์–ธ์–ด
Python
ํ‹ฐ์–ด
๊ณจ๋“œ
์œ ํ˜•
์‹œ๋ฎฌ๋ ˆ์ด์…˜
๊ตฌํ˜„
nj_Blog
X
nj_์ƒํƒœ
๋‹ค์‹œ ํ’€๊ธฐ
์ดํ•ด๋„
ํ’€์ด
์‚ฌ๋žŒ
์ดํ•ด๋„ 2
13 more properties

๋ฌธ์ œ๋งํฌ

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
๋ณต์‚ฌ

์ฐธ๊ณ