백준 문제 풀이 - 14499

14499 주사위 굴리기 문제

내가 푼 로직

객체지향적으로 풀어보기로 결정하였다.
먼저 주사위 클래스를 만들고, 해당 클래스에는 주사위의 상태와 돌리는 메서드를 정의했다.
또 다른 클래스로 지도 클래스를 만들었는데, 해당 지도 클래스는 전체 지도와 주사위의 현위치를 저장하고 있다.
방향을 입력받아 해당 방향으로 이동 가능한지 확인하는 메서드와 실제로 이동하는 메서드를 구현했다.

이후 메인에서 이동하는 명령어를 입력받아, 해당 위치로 이동가능한지 확인후 주사위의 위치를 옮기고, 주사위도 굴린다.
그 후, 바닥의 값을 판단하여 주사위 값을 바닥에 복사할지, 바닥의 값을 주사위에 복사할지 판단하여 실행한다.

고생

정말 별거 아니지만 문제를 제대로 읽지 않아 문제가 발생하였다.
입력으로 오는 순서가 X Y 순서가 아닌 Y X 순서로 들어온다.
그래서 처음에 배열 인덱스 관련된 익셉션이 떳고, 이를 해결하였더니 로컬에서 모두 정상적으로 잘 돌아갔다.
백준에 제출을 해 보았는데 틀렸다고 나왔다.
2시간이나 모든 코드를 뒤져보았는데 틀린곳이 없었다.. 분명 잘돌아가야 하는데?
나중에 알고보니.. 시작하는 주사위의 좌표가 주어진 3개의 예제에서는 모두 (0, 0) (1, 1) 과 같이 동일한 예제여서 문제가 없었던 것이었다.
내가 실수로 배열을 접근하는데 있어서 [x][y] 로 접근을 해버린 것이다. 배열은 [y][x]로 접근해야한다.
지난번에도 비슷한 실수를 했던것 같은데 또 이런 실수를 반복하게 되었다.
그래서 다시는 이런 일이 생기지 않도록 이 글을 쓰며 되새기고 있다. ㅠㅠ

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
* Created by sanghyoun on 2017. 4. 16..
*/

public class B14499 {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
String[] input = br.readLine().split(" ");
int ySize = Integer.parseInt(input[0]);
int xSize = Integer.parseInt(input[1]);
int[] defaultPos = new int[]{Integer.parseInt(input[3]), Integer.parseInt(input[2])};
int commandSize = Integer.parseInt(input[4]);
int[][] map = new int[ySize][xSize];
for (int i = 0; i < ySize; i++) {
String[] mapInput = br.readLine().split(" ");
for (int k = 0; k < xSize; k++) {
map[i][k] = Integer.parseInt(mapInput[k]);
}
}
String[] commands = br.readLine().split(" ");
/*************** 입력끝 ***************/
Dice dice = new Dice();
DiceMap diceMap = new DiceMap(xSize, ySize, defaultPos[0], defaultPos[1], map);
for (int i = 0; i < commandSize; i++) {
int comm = Integer.parseInt(commands[i]);
if (diceMap.isValid(comm)) {
diceMap.move(comm);
dice.move(comm);
int curNum = diceMap.getCurNum();
if (curNum == 0) {
diceMap.setCurNum(dice.getBottom());
} else {
dice.setBottom(curNum);
diceMap.setCurNum(0);
}
bw.write(Integer.toString(dice.getUpper()));
bw.newLine();
} else {
continue;
}
}
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}

class Dice {
private int upper;
private int bottom;
private int left;
private int right;
private int up;
private int down;

public Dice() {
upper = 0;
bottom = 0;
up = 0;
left = 0;
right = 0;
down = 0;
}

public void move(int direction) {
// 우 1 좌 2 위 3 아래 4
int temp;
switch (direction) {
case 1:
// up과 down은 변화없음
temp = bottom;
bottom = right;
right = upper;
upper = left;
left = temp;
break;
case 2:
// up과 down 은 변화없음
temp = bottom;
bottom = left;
left = upper;
upper = right;
right = temp;
break;
case 3:
// left와 right 변화없음
temp = bottom;
bottom = up;
up = upper;
upper = down;
down = temp;
break;
case 4:
// left와 right 변화없음
temp = bottom;
bottom = down;
down = upper;
upper = up;
up = temp;
break;
}
}

public int getUpper() {
return upper;
}

public int getBottom() {
return bottom;
}

public void setBottom(int num) {
this.bottom = num;
}
}

class DiceMap {
private int maxXpos;
private int maxYpos;
private int xPos;
private int yPos;
private int[][] map;

public DiceMap(int x, int y, int curX, int curY, int[][] map) {
this.maxXpos = x - 1;
this.maxYpos = y - 1;
this.xPos = curX;
this.yPos = curY;
this.map = map;
}

public boolean isValid(int direction) {
// 우 1 좌 2 위 3 아래 4
switch (direction) {
case 1:
return xPos + 1 <= maxXpos;
case 2:
return xPos - 1 >= 0;
case 3:
return yPos - 1 >= 0;
case 4:
return yPos + 1 <= maxYpos;
}
return false;
}

public void move(int direction) {
switch (direction) {
case 1:
xPos++;
break;
case 2:
xPos--;
break;
case 3:
yPos--;
break;
case 4:
yPos++;
break;
}
}

public int getCurNum() {
return map[yPos][xPos];
}

public void setCurNum(int num) {
map[yPos][xPos] = num;
}
}

댓글