Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 일급 컬렉션
- OAuth 2.0
- Google OAuth
- builder
- lombok
- java
- spring security
- Dependency Injection
- synchronized
- factory
- nestjs
- middleware
- 일급 객체
- Volatile
- Spring
Archives
- Today
- Total
HJW's IT Blog
[BOJ/JAVA] 주사위 굴리기 본문
문제 분석
이 문제는 크기가 N x M 인 지도 위에서 주사위를 굴리는 시뮬레이션 문제이다. 주사위 이동에 대한 명령이 주어지며, 이동한 칸의 값에 따라 주사위와 지도의 값을 교환한다.
문제 요구사항
- 주사위는 주어진 명령에 따라 이동한다
- 1 : 동
- 2 : 서
- 3 : 북
- 4 : 남
- 주사위가 지도 바깥으로 나가는 명령을 받는 경우 무시한다
- 주사위가 이동했을때, 지도의 값이 0 이라면, 주사위의 바닥면을 복사한다
- 주사위가 이동했을때, 지도의 값이 0 이 아니라면, 칸의 값을 주사위의 바닥에 복사하고, 칸은 0이 된다.
코드 설계
Dice 클래스
Dice 는 6가지 면의 상태를 기록해 두는 클래스로, second : 윗면, fourth : 아랫면 을 기준으로 first : 북쪽을 바라보는 면, third : 남쪽을 바라보는면, left , right 은 양쪽 면으로 정의 했다.
roll 메서드는 인자로 방향을 가르키는 dir 을 받게 되는데, 해당 방향에 따라 각 상태가 어떻게 변화할 지에 대한 결정을 내린다.
해당 설계에서 주의깊게 볼 점은, second 의 경우, 항상 주사위의 위쪽 수를 가르키며, fourth 의 경우 항상 아랫면을 가르킨다는 점이다.
Command iteration
명령들이 모두 입력 되었을때, command 가 저장된 list 를 순회하며, 결과를 반환한다.
정의 되어있는 directions 함수로, 다음 주사위의 위치를 결정하며, 이에 따른 지도를 벗어나는지에 대한 검증 로직이 추가되어 있다. 만약 지도 바깥으로 나간다면, 해당 명령은 무시하고 다음 명령으로 넘어간다.
만약 나가지 않을시, map의 해당 위치의 값에 따라 동작이 바뀐다. 0일 경우, 해당 칸의 값을 dice 의 아랫면으로, 0 이 아닐 경우, 해당 칸의 값을 dice 의 아랫면에 복사한뒤, 해당 칸은 0 으로 변경된다. 이후, dice 의 윗면 숫자를 출력한다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Dice{
int first;
int second;
int third;
int fourth;
int left;
int right;
public Dice(int first, int second, int third, int fourth, int left, int right){
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
this.left = left;
this.right = right;
}
public void roll(int dir){
Dice tmpDice = new Dice(first, second, third, fourth, left, right);
if(dir == 1){
this.second = tmpDice.left;
this.left = tmpDice.fourth;
this.right = tmpDice.second;
this.fourth = tmpDice.right;
}else if(dir == 2){
this.second = tmpDice.right;
this.left = tmpDice.second;
this.right = tmpDice.fourth;
this.fourth = tmpDice.left;
}else if(dir == 3){
this.second = tmpDice.third;
this.first = tmpDice.second;
this.third = tmpDice.fourth;
this.fourth = tmpDice.first;
}else{
this.first = tmpDice.fourth;
this.second = tmpDice.first;
this.third = tmpDice.second;
this.fourth = tmpDice.third;
}
}
public int getTop(){
return this.second;
}
public int getBottom(){
return this.fourth;
}
public void setBottom(int num){
fourth = num;
}
}
public class Main{
static int N, M, startX, startY, trial;
static int[][] map;
static int[] directions(int dir){
if(dir == 1) return new int[]{0,1};
else if(dir == 2) return new int[]{0,-1};
else if(dir == 3) return new int[] {-1,0};
else return new int[] {1,0};
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] input = bf.readLine().split(" ");
N = Integer.parseInt(input[0]);
M = Integer.parseInt(input[1]);
startX = Integer.parseInt(input[2]);
startY = Integer.parseInt(input[3]);
trial = Integer.parseInt(input[4]);
map = new int[N][M];
for(int i = 0; i<N; i++){
String[] tmp = bf.readLine().split(" ");
for(int j = 0; j<tmp.length; j++){
map[i][j] = Integer.parseInt(tmp[j]);
}
}
String[] commands = bf.readLine().split(" ");
Dice dice = new Dice(0,0,0,0,0,0);
int x = startX;
int y = startY;
for(String command : commands){
int c = Integer.parseInt(command);
int[] direction = directions(c);
int nx = x + direction[0];
int ny = y + direction[1];
if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
dice.roll(c);
if(map[nx][ny] == 0){
map[nx][ny] = dice.getBottom();
}else{
dice.setBottom(map[nx][ny]);
map[nx][ny] = 0;
}
x = nx;
y = ny;
System.out.println(dice.getTop());
}
}
}
'Algorithm' 카테고리의 다른 글
[BOJ : JAVA] 아기상어 (1) | 2024.11.18 |
---|---|
[프로그래머스 / JAVA / Javascript] 표병합 (0) | 2024.10.15 |
[프로그래머스/c++] 사라지는 발판 (0) | 2024.09.23 |
[프로그래머스/C++] 코딩테스트 공부 (0) | 2024.09.19 |
[Programmers/C++] 등산코스 정하기 (1) | 2024.09.16 |