본문 바로가기
study/백준

[백준 문제풀이] 114499 주사위 굴리기 java 풀이

by SHplusR 2023. 8. 9.

**접근법 :** 

**1) 어떻게 풀 것인가?**
필요한것 : 주사위를 한번 굴릴때마다 주사위의 번호가 전개도 상 어디로 이동하는지

 

계속 리뉴얼해야함.
이 문제는 딱히 알고리즘이 필요하다기 보다는 문제를 이해하고(특히 주사위) 푸는 능력이 중요하다.

 

**2) 시간복잡도**
2초이다...근데 딱히 알고리즘을 쓰는게 아니라서 신경 안 쓰고 풀었다.

 

**3) 공간복잡도**
512mb로 공간은 넉넉한편인것같다

 

**4) 풀면서 놓쳤던점**
처음엔 주사위 아래에 있는 수가 어떻게 이동하는지만 저장하고 사용하려했는데 곧 모든수를 생각해야한다는것을 알게되었다. ( 주사위가 굴려질때마다 전개도를 바꿔야함)
그래서 주사위가 동/서/남/북 방향으로 굴려질때마다 직접 그려가며 위치를 저장했다.

 

**5) 이 문제를 통해 얻어갈 것**
문제가 이해가 안 되서 정말 30분은 문제 텍스트만 읽었던것같다.
이해능력을 기르자...
그리고 문제를 잘 읽자!!! 이 문제는 좌표의 기준도 특이하고 여러 조건이 많아서 문제를 잘 읽고 완전히 이해한 후에야 풀리는 문제인것같다.
import java.io.*;

public class Main{
static int n,m,x,y,k;
static int[][] maps;
static int[] dice;
static String[] dirStirngs;
static StringBuilder sb;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sb = new StringBuilder();
String[] strings = br.readLine().split(" ");
n= Integer.parseInt(strings[0]);
m= Integer.parseInt(strings[1]);
x= Integer.parseInt(strings[2]);
y= Integer.parseInt(strings[3]);
k= Integer.parseInt(strings[4]);
maps = new int[n][m];
dice = new int[6];



for(int i=0; i<n;i++){
String[] mapStirngs = br.readLine().split(" ");
for(int j=0; j<m; j++){
maps[i][j] = Integer.parseInt(mapStirngs[j]);
}
}

dirStirngs = br.readLine().split(" "); //4 4 4 1 3 3 3 2
togame(x,y);
System.out.println(sb);
}
static void togame(int a, int b){
for(int i=0;i<k;i++){
switch (dirStirngs[i]){
case "1":
//동
if(b+1<m){
doeast();
sb.append(dice[4]+" ");
int mapNum = maps[a][b+1];
if(mapNum != 0){
dice[5] = mapNum;
maps[a][b+1] = 0;
}
else{
maps[a][b+1] = dice[5];
}
b+=1;
break;
}
else{ break;}
case "2":
//서
if(b-1>=0){
dowest();
sb.append(dice[4]+" ");
int mapNum = maps[a][b-1];
if(mapNum != 0){
dice[5] = mapNum;
maps[a][b-1] = 0;
}
else{
maps[a][b-1] = dice[5];
}
b-=1;
break;
}
else{ break;}
case "3":
//북
if(a-1>=0){
donorth();
sb.append(dice[4]+" ");
int mapNum = maps[a-1][b];
if(mapNum != 0){
dice[5] = mapNum;
maps[a-1][b] = 0;
}
else{
maps[a-1][b] = dice[5];
}
a -=1;
break;
}
else{ break;}
case "4":
//남
if(a+1<n){
dosouth();
sb.append(dice[4]+" ");
int mapNum = maps[a+1][b];
if(mapNum != 0){
dice[5] = mapNum;
maps[a+1][b] = 0;
}
else{
maps[a+1][b] = dice[5];
}
a+=1;
break;
}
else{ break;}

default:
break;
}
}
}

static void doeast(){
int temp0 = dice[0];
int temp1 = dice[1];
int temp4 = dice[4];
int temp5 = dice[5];
dice[0] = temp4;
dice[1] = temp5;
dice[4] = temp1;
dice[5] = temp0;
}
static void dowest(){
int temp0 = dice[0];
int temp1 = dice[1];
int temp4 = dice[4];
int temp5 = dice[5];
dice[0] = temp5;
dice[1] = temp4;
dice[4] = temp0;
dice[5] = temp1;

}
static void dosouth(){
int temp2 = dice[2];
int temp3 = dice[3];
int temp4 = dice[4];
int temp5 = dice[5];
dice[2] = temp4;
dice[3] = temp5;
dice[4] = temp3;
dice[5] = temp2;
}
static void donorth(){
int temp2 = dice[2];//남
int temp3 = dice[3];//북
int temp4 = dice[4];//위
int temp5 = dice[5];//아래
dice[2] = temp5;
dice[3] = temp4;
dice[4] = temp2;
dice[5] = temp3;
}
}