백준 문제 풀이
백준 14499 파이썬 python : 주사위 굴리기 @@황소처럼 우직하게@@ 깔끔하게 풀기
hjp845
2020. 2. 26. 04:27
반응형
import sys
input = sys.stdin.readline
mat = []
n, m, y, x, k = map(int, input().split())
for i in range(n):
mat.append(list(map(int, input().split())))
orders = list(map(int, input().split()))
class Dice:
def __init__(self):
self.west = 0
self.north = 0
self.east = 0
self.south = 0
self.up = 0
self.down = 0
def go_south(self):
self.north, self.up, self.south, self.down = self.down, self.north, self.up, self.south
def go_north(self):
self.north, self.up, self.south, self.down = self.up, self.south, self.down, self.north
def go_west(self):
self.west, self.up, self.east, self.down = self.up, self.east, self.down, self.west
def go_east(self):
self.west, self.up, self.east, self.down = self.down, self.west, self.up, self.east
def set_down(self, down):
self.down = down
def get_down(self):
return self.down
def get_up(self):
return self.up
def moving(y, x):
if mat[y][x] == 0:
mat[y][x] = dice.get_down()
else:
dice.set_down(mat[y][x])
mat[y][x] = 0
print(dice.get_up())
dice = Dice()
for order in orders:
if order == 1: # east
if x < m - 1:
x = x + 1
dice.go_east()
moving(y, x)
elif order == 2: # west
if x > 0:
x = x - 1
dice.go_west()
moving(y, x)
elif order == 3: # NORTH
if y > 0:
y = y - 1
dice.go_north()
moving(y, x)
elif order == 4: # south
if y < n - 1:
y = y + 1
dice.go_south()
moving(y, x)
문제 딱 보면 막막한데, 객체화시켜서 풀겠다 생각하면 어렵지않습니다.
Dice 클래스에서 좌로 우로 굴리는거 다 고려해주고 구현해줍니다.
그리고 문제에서는 인풋값을 x, y 순서로 받으라고 했지만 y, x로 받았습니다.
반응형