곽로그
[백준 14499, Java] 주사위 굴리기 본문
반응형
1. 실수한 것
- mx<0 my<0 mx>=N my>=N
2. 처음 접근을 잘 못한 것
주사위를 굴릴 때 변하는 것, 변하지 않는 것
- 전개도를 돌림
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
class Dice{
int x;
int y;
int[] diceValue;
public static int UP =0;
public static int BACK =1;
public static int RIGHT = 2;
public static int LEFT = 3;
public static int FRONT = 4;
public static int BOTTOM =5;
Dice(int x, int y){
this.x = x;
this.y = y;
diceValue = new int[6];
}
}
public class Main {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static int[][] map;
public static int N;
public static int M;
public static int[] command;
public static int K;
public static int[] dx = {0,0,-1,1};
public static int[] dy = {1,-1,0,0};
public static void main(String[] args) throws Exception{
StringTokenizer st;
st = new StringTokenizer(br.readLine()," ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
int x = Integer.parseInt(st.nextToken());
int y= Integer.parseInt(st.nextToken());
Dice dice = new Dice(x,y);
K = Integer.parseInt(st.nextToken());
command = new int[K];
for(int r= 0; r<N; r++) {
st = new StringTokenizer(br.readLine()," ");
for(int c=0;c<M;c++) {
map[r][c] = Integer.parseInt(st.nextToken());
}
}
st= new StringTokenizer(br.readLine());
for(int c =0; c<K;c++) {
command[c] =Integer.parseInt(st.nextToken())-1;
}
for(int c= 0 ;c<command.length;c++) {
int upValue = rollDice(dice,command[c]);
if(upValue!=-1) {
bw.write(String.valueOf(upValue)+"\n");
}
}
bw.flush();
bw.close();
}
public static int rollDice(Dice dice, int direction) throws Exception {
int mx = dice.x+dx[direction];
int my= dice.y+dy[direction];
if(mx<0 || my<0 || mx>=N || my>=M) {
return -1;
}
else {
dice.x = mx;
dice.y = my;
int bottomValue = changeDiceValue(dice,direction);
int mapValue = map[mx][my];
if(mapValue==0) {
map[mx][my] = bottomValue;
}
else {
dice.diceValue[Dice.BOTTOM] = mapValue;
map[mx][my] =0;
}
return dice.diceValue[Dice.UP];
}
}
public static int changeDiceValue(Dice dice, int direction) {
int[] oldDiceValue = dice.diceValue;
int[] newDiceValue = new int[6];
if(direction==0) {
//동
newDiceValue[Dice.UP] = oldDiceValue[Dice.LEFT];
newDiceValue[Dice.RIGHT] = oldDiceValue[Dice.UP];
newDiceValue[Dice.LEFT] = oldDiceValue[Dice.BOTTOM];
newDiceValue[Dice.BOTTOM] = oldDiceValue[Dice.RIGHT];
newDiceValue[Dice.FRONT] = oldDiceValue[Dice.FRONT];
newDiceValue[Dice.BACK] = oldDiceValue[Dice.BACK];
dice.diceValue = newDiceValue;
return newDiceValue[Dice.BOTTOM];
}
else if(direction==1) {
//서
newDiceValue[Dice.UP] = oldDiceValue[Dice.RIGHT];
newDiceValue[Dice.RIGHT] = oldDiceValue[Dice.BOTTOM];
newDiceValue[Dice.LEFT] = oldDiceValue[Dice.UP];
newDiceValue[Dice.BOTTOM] = oldDiceValue[Dice.LEFT];
newDiceValue[Dice.FRONT] = oldDiceValue[Dice.FRONT];
newDiceValue[Dice.BACK] = oldDiceValue[Dice.BACK];
dice.diceValue = newDiceValue;
return newDiceValue[Dice.BOTTOM];
}
else if(direction==2) {
//북
newDiceValue[Dice.UP] = oldDiceValue[Dice.FRONT];
newDiceValue[Dice.BACK] = oldDiceValue[Dice.UP];
newDiceValue[Dice.FRONT] = oldDiceValue[Dice.BOTTOM];
newDiceValue[Dice.BOTTOM] = oldDiceValue[Dice.BACK];
newDiceValue[Dice.LEFT] = oldDiceValue[Dice.LEFT];
newDiceValue[Dice.RIGHT] = oldDiceValue[Dice.RIGHT];
dice.diceValue = newDiceValue;
return newDiceValue[Dice.BOTTOM];
}
else if(direction ==3) {
//
newDiceValue[Dice.UP] = oldDiceValue[Dice.BACK];
newDiceValue[Dice.BACK] = oldDiceValue[Dice.BOTTOM];
newDiceValue[Dice.FRONT] = oldDiceValue[Dice.UP];
newDiceValue[Dice.BOTTOM] = oldDiceValue[Dice.FRONT];
newDiceValue[Dice.LEFT] = oldDiceValue[Dice.LEFT];
newDiceValue[Dice.RIGHT] = oldDiceValue[Dice.RIGHT];
dice.diceValue = newDiceValue;
return newDiceValue[Dice.BOTTOM];
}
return -1;
}
public static void printMap(int[][] mapToPrint) {
for(int r=0;r<N; r++) {
for(int c=0;c<M;c++) {
System.out.printf("%2d",mapToPrint[r][c]);
}
System.out.println();
}
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
백준 19236 청소년 상어 (Java) (0) | 2020.10.17 |
---|---|
[백준 15686, Java] 치킨배달 (0) | 2020.10.15 |
[백준 12100, Java] 2046(easy) (0) | 2020.10.14 |
[백준 16236,Java] 아기상어 (0) | 2020.10.11 |
[백준14891, Java] 톱니바퀴 (0) | 2020.10.07 |
Comments