๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 8972๋ฒ
ํ์ด ๊ณผ์
ํ๋ ์ด์ด์ ๋ฏธ์น ์๋์ด๋
ธ์ ๋์์ ์๋ฎฌ๋ ์ด์
ํ๋ ๊ตฌํ
๋ฌธ์ ์
๋๋ค.
์กฐ๊ฑด์ด ๋ง์ ๊ตฌํ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ ๋ชจ๋ํ์ ์ ๊ฒฝ์ ๋ง์ด ์จ์ ํด๊ฒฐํ์ต๋๋ค.
ํ๋ ์ด์ด์ ๋ก๋ด์ ์์น๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด๋ฅผ ๊ฒ์ํ๊ณผ ๋ณ๋๋ก ์ ์ธํ๊ณ ๊ฒ์ํ ์ด๋, ๊ฒ์ ์ข
๋ฅ ์ ๋ฌด๋ฅผ ํ๋จํด์คฌ์ต๋๋ค.
๋ฌธ์ ๊ตฌํํ๋ฉด์ python
์์ ์ ๊ณตํ๋ any
ํจ์๋ฅผ ์๊ฒ๋์์ต๋๋ค.Javascript
์ some
๊ณ ์ฐจํจ์์ ์ ์ฌํ๋ฐ ์ฝ๋ฐฑ์ ์ธ์๋ก ๋ฐ์ง ์์ ์ฌ์ฉ๋ฐฉ๋ฒ์ด ์ข ๋ค๋ฅด๋ค๋ ์ฐจ์ด์ ์ด ์์ต๋๋ค.
์ด์ธ์๋ ํ๋ ์ด์ด์ ๋ก๋ด์ด ์ด๋ํ ๋ ๊ฐ ์์น์์ ์ฌ๋ฌ๊ฐ์ ๋ก๋ด์ด๋ ํ๋ ์ด์ด๊ฐ
ํ๊ดด๋๊ธฐ ์ ๋์์ ์์นํ ์ ์๊ธฐ ๋๋ฌธ์ deque
๋ฅผ ์ด์ฉํด์ ๊ด๋ฆฌํ์ต๋๋ค.
์ฝ๋
import sys
from collections import deque
R, C = list(map(int, sys.stdin.readline().split()))
board = [list(sys.stdin.readline().strip()) for _ in range(R)]
commands = list(map(int, sys.stdin.readline().strip()))
dx = [0, 1, 1, 1, 0, 0, 0, -1, -1, -1]
dy = [0, -1, 0, 1, -1, 0, 1, -1, 0, 1]
def pre_processing():
game_map = [[deque() for _ in range(C)] for _ in range(R)]
meta = {
'I': [],
'R': []
}
for x in range(R):
for y in range(C):
if not board[x][y] == '.':
game_map[x][y].append(board[x][y])
meta[board[x][y]].append([x, y])
return meta, game_map
def move_jongsu(command, meta, game_map):
x, y, = meta['I'][0]
nx, ny = x + dx[command], y + dy[command]
game_map[x][y].popleft()
game_map[nx][ny].append('I')
meta['I'] = [[nx, ny]]
def next_position(robot, jongsu):
min_dist, position = sys.maxsize, None
for i in range(1, 10):
if i == 5:
continue
nx, ny = robot[0] + dx[i], robot[1] + dy[i]
if 0 <= nx < R and 0 <= ny < C:
dist = abs(jongsu[0] - nx) + abs(jongsu[1] - ny)
if min_dist > dist:
min_dist = dist
position = [nx, ny]
return position
def move_robots(meta, game_map):
jongsu = meta['I'][0]
robots = meta['R']
moved_robots = []
for robot in robots:
next_xy = next_position(robot, jongsu)
game_map[robot[0]][robot[1]].popleft()
game_map[next_xy[0]][next_xy[1]].append('R')
moved_robots.append(next_xy)
meta['R'] = moved_robots
def allowed_to_continue(jongsu, game_map):
x, y = jongsu
if any(map(lambda e: e == 'R', game_map[x][y])):
return False
return True
def explode_robots(game_map, meta):
survived_robots = []
for x in range(R):
for y in range(C):
if len(game_map[x][y]) > 0:
if game_map[x][y][0] == 'R':
if len(game_map[x][y]) == 1:
survived_robots.append([x, y])
else:
game_map[x][y].clear()
meta['R'] = survived_robots
def run(command, meta, game_map):
move_jongsu(command, meta, game_map)
if not allowed_to_continue(meta['I'][0], game_map):
return False
move_robots(meta, game_map)
if not allowed_to_continue(meta['I'][0], game_map):
return False
explode_robots(game_map, meta)
return True
def solution():
meta, game_map = pre_processing()
for turn, command in enumerate(commands):
survive = run(command, meta, game_map)
if not survive:
print(f"kraj {turn + 1}")
return
for x in range(R):
for y in range(C):
token = '.' if not game_map[x][y] else game_map[x][y][0]
sys.stdout.write(f"{token}")
sys.stdout.write("\n")
solution()
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 1806 - ๋ถ๋ถํฉ (0) | 2021.03.08 |
---|---|
BOJ 6186 - Best Grass (0) | 2021.03.08 |
BOJ 19941 - ํ๋ฒ๊ฑฐ ๋ถ๋ฐฐ (0) | 2021.03.08 |
BOJ 16469 - ์๋ ์ ํ (0) | 2021.03.05 |
BOJ 13565 - ์นจํฌ (0) | 2021.03.05 |
๐ฌ ๋๊ธ