https://www.acmicpc.net/problem/14499
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
문제
크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 이 지도의 위에 주사위가 하나 놓여져 있으며, 주사위의 전개도는 아래와 같다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다.
2
4 1 3
5
6
주사위는 지도 위에 윗 면이 1이고, 동쪽을 바라보는 방향이 3인 상태로 놓여져 있으며, 놓여져 있는 곳의 좌표는 (x, y) 이다. 가장 처음에 주사위에는 모든 면에 0이 적혀져 있다.
지도의 각 칸에는 정수가 하나씩 쓰여져 있다. 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다. 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.
주사위를 놓은 곳의 좌표와 이동시키는 명령이 주어졌을 때, 주사위가 이동했을 때 마다 상단에 쓰여 있는 값을 구하는 프로그램을 작성하시오.
주사위는 지도의 바깥으로 이동시킬 수 없다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.
입력
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다.
둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 주사위를 놓은 칸에 쓰여 있는 수는 항상 0이다. 지도의 각 칸에 쓰여 있는 수는 10 미만의 자연수 또는 0이다.
마지막 줄에는 이동하는 명령이 순서대로 주어진다. 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4로 주어진다.
출력
이동할 때마다 주사위의 윗 면에 쓰여 있는 수를 출력한다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.
난이도 및 분류
solved.ac 티어 기준 gold 4 난이도
분류 : 구현, 시뮬레이션
접근 방법 및 생각
문제를 보면 일견 복잡해 보이지만 두 가지 과정으로 나누어서 풀어보면 어떨까 하는 생각을 했다.
- 주사위 객체를 따로 만들고 방향에 따라 주사위의 상,하,좌,우,위,아래 값들을 변경하는 코드 구현
- 명령에 따라 주사위를 이동시키고 상단에 있는 값을 출력하는 코드 구현
1번 과정
우선, 주사위 객체를 만들고 각 면에 대한 필드를 생성했다.
초기 주사위의 값은 0이라고 했으므로 6개면 모드 0으로 초기화
class Dice {
int top;
int bottom;
int left;
int right;
int front;
int back;
public Dice() {
this.top = 0;
this.bottom = 0;
this.left = 0;
this.right = 0;
this.front = 0;
this.back = 0;
}
}
그 다음 주사위를 굴렸을 때 각 면이 어떤 면으로 이동할지를 생각하고 그에 맞는 메서드를 구현했다.
만약, 주사위를 동쪽으로 굴렸을 때 다음과 같이 각 면이 변경될 것이다.
- top -> right
- right -> bottom
- bottom -> left
- left -> top
이를 동, 서, 남, 북에 대한 변경되는 값들을 메서드를 추가한다.
class Dice {
int top;
int bottom;
int left;
int right;
int front;
int back;
public Dice() {
this.top = 0;
this.bottom = 0;
this.left = 0;
this.right = 0;
this.front = 0;
this.back = 0;
}
public void moveNorth(){
int temp = this.top;
this.top = this.front;
this.front = this.bottom;
this.bottom = this.back;
this.back = temp;
}
public void moveEast(){
int temp = this.top;
this.top = this.left;
this.left = this.bottom;
this.bottom = this.right;
this.right = temp;
}
public void moveWest(){
int temp = this.top;
this.top = this.right;
this.right = this.bottom;
this.bottom = this.left;
this.left = temp;
}
public void moveSouth(){
int temp = this.top;
this.top = this.back;
this.back = this.bottom;
this.bottom = this.front;
this.front = temp;
}
}
여기에 각 이동명령은 동쪽은1, 서쪽은2, 북쪽은3, 남쪽은4로 되어 있기 때문에 명령을 매개변수로 받는 메서드를 하나 추가하면 주사위 객체를 완성할 수 있다.
class Dice {
int top;
int bottom;
int left;
int right;
int front;
int back;
public Dice() {
this.top = 0;
this.bottom = 0;
this.left = 0;
this.right = 0;
this.front = 0;
this.back = 0;
}
public void operation(int op){
switch(op){
case 1:
moveEast();
break;
case 2:
moveWest();
break;
case 3:
moveNorth();
break;
case 4:
moveSouth();
break;
}
}
...
}
2번 과정
이 부분은 문제에 나와 있는 그대로 로직을 짜 주면 된다.
각 방향에 따라 이동하는 좌표를 계산하고
이동한 지도에 0이 쓰여 있다면 주사위 바닥면을 복사,
0이 아니라면 주사위 바닥에 지도 숫자를 이동시키면 된다.
구현 코드
/*
Boj 14499. 주사위 굴리기
level. gold 4
solved by 송찬환
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Boj_14499_주사위굴리기 {
static int N, M;
static int x, y;
static int K;
static int[][] map;
static Queue<Integer> operation;
static int[] dx = {0, 0, 0, -1, 1};
static int[] dy = {0, 1, -1, 0, 0};
static Dice dice;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new int[N][M];
operation = new LinkedList<>();
dice = new Dice();
// map 구성
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
// 이동명령 구성
st = new StringTokenizer(br.readLine());
for (int i = 0; i < K; i++) {
operation.offer(Integer.parseInt(st.nextToken()));
}
moveDice();
}
static void moveDice() {
while(!operation.isEmpty()){
int cur = operation.poll();
int nx = x + dx[cur];
int ny = y + dy[cur];
if(isInRange(nx, ny)){
dice.operation(cur);
if(map[nx][ny] == 0){
map[nx][ny] = dice.bottom;
}else{
dice.bottom = map[nx][ny];
map[nx][ny] = 0;
}
x = nx;
y = ny;
System.out.println(dice.top);
}
}
}
static boolean isInRange(int x, int y){
return (0 <= x && x < N) && (0 <= y && y < M);
}
}
class Dice {
int top;
int bottom;
int left;
int right;
int front;
int back;
public Dice() {
this.top = 0;
this.bottom = 0;
this.left = 0;
this.right = 0;
this.front = 0;
this.back = 0;
}
public void operation(int op){
switch(op){
case 1:
moveEast();
break;
case 2:
moveWest();
break;
case 3:
moveNorth();
break;
case 4:
moveSouth();
break;
}
}
public void moveNorth(){
int temp = this.top;
this.top = this.front;
this.front = this.bottom;
this.bottom = this.back;
this.back = temp;
}
public void moveEast(){
int temp = this.top;
this.top = this.left;
this.left = this.bottom;
this.bottom = this.right;
this.right = temp;
}
public void moveWest(){
int temp = this.top;
this.top = this.right;
this.right = this.bottom;
this.bottom = this.left;
this.left = temp;
}
public void moveSouth(){
int temp = this.top;
this.top = this.back;
this.back = this.bottom;
this.bottom = this.front;
this.front = temp;
}
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 2580. 스도쿠 - JAVA (0) | 2022.10.19 |
---|