14499번: 주사위 굴리기
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지
www.acmicpc.net
알고리즘 : 구현!
주사위를 구현하는 과정에서 생각하는 것이 어려웠던 것 같다.
너무 배열로만 풀려고 했던 것이 어렵게 문제를 풀게 된 이유인 것 같다.
- Javascript
function solution() {
let input = require("fs")
.readFileSync("input.txt") //"/dev/stdin"
.toString()
.split("\n")
.map((val) => val.trim());
let [n, m, x, y, k] = input
.shift()
.split(" ")
.map((v) => +v);
let moveArr = input
.pop()
.split(" ")
.map((v) => +v);
let graph = input.map((v) => v.split(" ").map((val) => +val));
const dice = {
top: 0,
bot: 0,
right: 0,
left: 0,
front: 0,
back: 0,
};
const move = (num) => {
const { top, right, left, bot, front, back } = dice;
let arr;
switch (num) {
case 0: {
arr = [left, top, right, bot, front, back];
break;
}
case 1: {
arr = [right, bot, left, top, front, back];
break;
}
case 2: {
arr = [front, right, back, left, bot, top];
break;
}
case 3: {
arr = [back, right, front, left, top, bot];
break;
}
}
dice.top = arr[0];
dice.right = arr[1];
dice.bot = arr[2];
dice.left = arr[3];
dice.front = arr[4];
dice.back = arr[5];
};
moveArr = moveArr.map((v) => v - 1);
const dx = [0, 0, -1, 1];
const dy = [1, -1, 0, 0];
let cnt = 0;
while (1) {
if (cnt === k) break;
let nx = x + dx[moveArr[cnt]];
let ny = y + dy[moveArr[cnt]];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
cnt++;
continue;
}
move(moveArr[cnt]);
if (graph[nx][ny] === 0) {
graph[nx][ny] = dice.bot;
} else {
dice.bot = graph[nx][ny];
graph[nx][ny] = 0;
}
console.log(dice.top);
x = nx;
y = ny;
cnt++;
}
}
solution();
- Python
import sys
sys.stdin = open("baekjoon/14499/input.txt","r")
input = sys.stdin.readline
n,m,x,y,k = map(int,input().split())
graph = []
for i in range(n):
graph.append(list(map(int,input().split())))
moveArr = list(map(int,input().split()))
moveArr = list(map(lambda v: v - 1, moveArr))
dice = {
"top": 0,
"bot": 0,
"right": 0,
"left": 0,
"front": 0,
"back": 0,
}
def move(num):
global dice
top, bot, right, left, front, back = dice.values()
if num == 0:
arr = [left, top, right, bot, front, back]
elif num == 1:
arr = [right, bot, left, top, front, back]
elif num == 2:
arr = [front, right, back, left, bot, top]
elif num == 3:
arr = [back, right, front, left, top, bot]
dice["top"], dice["right"], dice["bot"], dice["left"], dice["front"], dice["back"] = arr
cnt=0
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
while(1):
if cnt==k:
break
nx=x+dx[moveArr[cnt]]
ny=y+dy[moveArr[cnt]]
if(0<=nx<n and 0<=ny<m):
move(moveArr[cnt])
if graph[nx][ny]==0:
graph[nx][ny]=dice["bot"]
else:
dice["bot"]=graph[nx][ny]
graph[nx][ny]=0
print(dice["top"])
x=nx
y=ny
cnt+=1
'코딩테스트(알고리즘) > baekjoon' 카테고리의 다른 글
[baekjoon] 20055 컨테이어 벨트 위의 로봇 (Javascript, Python) (0) | 2023.03.25 |
---|---|
[baekjoon] 3190 뱀 (Javascript,Python) (0) | 2023.03.15 |
[baekjoon] 14888 연산자 끼워넣기 (Javascript,Python) (0) | 2023.03.10 |
[baekjoon] 1520 내리막 길 (Javascript,Python) (0) | 2023.03.03 |
[baekjoon] 1987 알파벳 (Javascript,Python) (0) | 2023.03.02 |