https://www.acmicpc.net/problem/14499
ํ์ด ๊ณผ์
๊ฐ์ฅ ์ค์ํ ํฌ์ธํธ๋ ์ฃผ์ฌ์ 6๋ฉด์ ์ ๋ณด ์ ์ฅ๋ฐฉ๋ฒ์ด๋ค.
๊ตฌ์กฐ์ฒด๋ ํด๋์ค๋ฅผ ์๊ฐํ์ง๋ง ๊ทธ๋ฅ ๋ฆฌ์คํธ๋ก ํด๋ ๋ ๊ฑฐ๊ฐ์์ 6ํฌ๊ธฐ์ ๋ฆฌ์คํธ ์ ์ธํ์ฌ ํด๊ฒฐํจ.
์ด๋์ ๊ฐ๋จํ ํจ์ 4๊ฐ๋ฅผ ๋ง๋ค์ด์ ์ฃผ์ฌ์ ๋ฆฌ์คํธ์ ๊ฐ๋ค์ ๊ตํ ์์ผฐ๋ค.
temp ์์ ๋ณ์๋ฅผ ํ๋ ์ ์ธํด์ ์ฝ๊ฒ ๊ตํ ๊ฐ๋ฅ.
import sys
input = sys.stdin.readline
N, M, X, Y, K = map(int,input().rstrip().split())
dx = [0,0,-1,1]
dy = [1,-1,0,0] # 1 ๋, 2 ์, 3 ๋ถ, 4 ๋จ
dice = [0,0,0,0,0,0] # down left front back right up
road = []
for i in range(N):
road.append([int(x) for x in input().rstrip().split()])
command = [int(x) for x in input().rstrip().split()]
์ํ์ข์ฐ ์ ๋ ฅํ๊ธฐ ํธํ๊ฒ dx, dy๋ฅผ ์ ์ธํ๊ณ ์ฃผ์ฌ์์ 6๋ฉด์ ๊ฐ์ ์ ์ฅํ๊ธฐ ์ํด dice๋ฆฌ์คํธ๋ฅผ ์ ์ธํด์ค๋ค.
def move_right():
temp = dice[1]
dice[1] = dice[0]
dice[0] = dice[4]
dice[4] = dice[5]
dice[5] = temp
์ด๋์ ํจ์๋ฅผ ๋ง๋ค์ด์ ํํํ์๋ค. ์ค๋ฅธ์ชฝ์ผ๋ก ์ฃผ์ฌ์๊ฐ ์ด๋ํ๋ฉด ์์น๊ฐ ๋ฐ๋๊ธฐ ๋๋ฌธ์ (์: ์ -> ์ค๋ฅธ์ชฝ)
๊ฐ๋จํ๊ฒ ๊ฐ์ swapํด์ฃผ๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ฌ์ฉ. ๋ค๋ฅธ ๋ฐฉํฅ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑํ๋ฉด ๋๋ค.
x, y = X, Y
for i in command:
if 0 <= x + dx[i - 1] < N and 0 <= y + dy[i - 1] < M:
x, y = x + dx[i - 1], y + dy[i - 1]
if i == 1:
move_right()
elif i == 2:
move_left()
elif i == 3:
move_back()
elif i == 4:
move_front()
if road[x][y] == 0:
road[x][y] = dice[0]
else:
dice[0] = road[x][y]
road[x][y] = 0
print(dice[5])
์ฃผ์ด์ง ๋ช ๋ น์ ๋ฐ๋ผ ์ฃผ์ฌ์๋ฅผ ์ด๋์์ผ์ฃผ๋ฉด ๋๋ค. ๊ฐ์ฅ ๋จผ์ ์ฃผ์ฌ์๊ฐ ์ด๋ ๊ฐ๋ฅํ์ง ํ๋ณํ๊ณ ,
์ด๋ ๋ฐฉํฅ์ ๋ฐ๋ผ 4๊ฐ์ง ํจ์๋ฅผ ์คํ์์ผ์ค๋ค.
๋ง์ฝ ํด๋น ์์น์ ์ง๋๊ฐ์ด 0์ด๋ฉด ์ง๋์ ์ฃผ์ฌ์๋ฐ๋ฅ์ ๊ฐ์ ์ ๋ ฅํ๊ณ ์๋๋ฉด ์ฃผ์ฌ์์ ์ง๋๊ฐ์ ์ ๋ ฅํ๋ค.
๋ง์ง๋ง์ผ๋ก ์ฃผ์ฌ์ ์๋ฉด์ ๊ฐ์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
์๋๋ ์ ์ฒด์ฝ๋
import sys
input = sys.stdin.readline
N, M, X, Y, K = map(int,input().rstrip().split())
dx = [0,0,-1,1]
dy = [1,-1,0,0] # 1 ๋, 2 ์, 3 ๋ถ, 4 ๋จ
dice = [0,0,0,0,0,0] # down left front back right up
road = []
for i in range(N):
road.append([int(x) for x in input().rstrip().split()])
command = [int(x) for x in input().rstrip().split()]
#print(road)
def move_right():
temp = dice[1]
dice[1] = dice[0]
dice[0] = dice[4]
dice[4] = dice[5]
dice[5] = temp
def move_left():
temp = dice[4]
dice[4] = dice[0]
dice[0] = dice[1]
dice[1] = dice[5]
dice[5] = temp
def move_front():
temp = dice[3]
dice[3] = dice[0]
dice[0] = dice[2]
dice[2] = dice[5]
dice[5] = temp
def move_back():
temp = dice[5]
dice[5] = dice[2]
dice[2] = dice[0]
dice[0] = dice[3]
dice[3] = temp
x, y = X, Y
for i in command:
if 0 <= x + dx[i - 1] < N and 0 <= y + dy[i - 1] < M:
x, y = x + dx[i - 1], y + dy[i - 1]
if i == 1:
move_right()
elif i == 2:
move_left()
elif i == 3:
move_back()
elif i == 4:
move_front()
if road[x][y] == 0:
road[x][y] = dice[0]
else:
dice[0] = road[x][y]
road[x][y] = 0
print(dice[5])
'๐งฉ Problem Solving > [๋ฐฑ์ค]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 11718 ๊ทธ๋๋ก ์ถ๋ ฅํ๊ธฐ (python ํ์ด์ฌ) (0) | 2022.06.10 |
---|---|
[๋ฐฑ์ค] 1005 ACMCraft (python ํ์ด์ฌ) (0) | 2022.06.10 |
[๋ฐฑ์ค] 7576 ํ ๋งํ (python ํ์ด์ฌ) (0) | 2022.06.08 |
[๋ฐฑ์ค] 14503 ๋ก๋ด ์ฒญ์๊ธฐ (python ํ์ด์ฌ) (0) | 2022.05.23 |
[๋ฐฑ์ค] 14502 _์ฐ๊ตฌ์ (python ํ์ด์ฌ) (0) | 2022.05.13 |