Skip to content

Commit 200b88d

Browse files
authored
Merge pull request #1256 from AlgorithmWithGod/Seol-JY
[20251028] BOJ / G4 / 주사위 굴리기 / 설진영
2 parents 055b0d9 + d75e1c6 commit 200b88d

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static class Dice {
7+
int top, bottom, front, back, left, right;
8+
9+
public Dice() {
10+
this.top = 0;
11+
this.bottom = 0;
12+
this.front = 0;
13+
this.back = 0;
14+
this.left = 0;
15+
this.right = 0;
16+
}
17+
18+
public void rollEast() {
19+
int temp = top;
20+
top = left;
21+
left = bottom;
22+
bottom = right;
23+
right = temp;
24+
}
25+
26+
public void rollWest() {
27+
int temp = top;
28+
top = right;
29+
right = bottom;
30+
bottom = left;
31+
left = temp;
32+
}
33+
34+
public void rollNorth() {
35+
int temp = top;
36+
top = front;
37+
front = bottom;
38+
bottom = back;
39+
back = temp;
40+
}
41+
42+
public void rollSouth() {
43+
int temp = top;
44+
top = back;
45+
back = bottom;
46+
bottom = front;
47+
front = temp;
48+
}
49+
50+
public int getTop() {
51+
return top;
52+
}
53+
54+
public int getBottom() {
55+
return bottom;
56+
}
57+
58+
public void setBottom(int value) {
59+
this.bottom = value;
60+
}
61+
}
62+
63+
static class Position {
64+
int x, y;
65+
66+
public Position(int x, int y) {
67+
this.x = x;
68+
this.y = y;
69+
}
70+
71+
public Position move(int direction) {
72+
switch(direction) {
73+
case 1:
74+
return new Position(x, y + 1);
75+
case 2:
76+
return new Position(x, y - 1);
77+
case 3:
78+
return new Position(x - 1, y);
79+
case 4:
80+
return new Position(x + 1, y);
81+
default:
82+
return this;
83+
}
84+
}
85+
86+
public boolean isValid(int n, int m) {
87+
return x >= 0 && x < n && y >= 0 && y < m;
88+
}
89+
}
90+
91+
public static void main(String[] args) throws IOException {
92+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
93+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
94+
95+
StringTokenizer st = new StringTokenizer(br.readLine());
96+
int n = Integer.parseInt(st.nextToken());
97+
int m = Integer.parseInt(st.nextToken());
98+
int x = Integer.parseInt(st.nextToken());
99+
int y = Integer.parseInt(st.nextToken());
100+
int k = Integer.parseInt(st.nextToken());
101+
102+
int[][] map = new int[n][m];
103+
for (int i = 0; i < n; i++) {
104+
st = new StringTokenizer(br.readLine());
105+
for (int j = 0; j < m; j++) {
106+
map[i][j] = Integer.parseInt(st.nextToken());
107+
}
108+
}
109+
110+
Dice dice = new Dice();
111+
Position pos = new Position(x, y);
112+
113+
st = new StringTokenizer(br.readLine());
114+
for (int i = 0; i < k; i++) {
115+
int direction = Integer.parseInt(st.nextToken());
116+
Position nextPos = pos.move(direction);
117+
118+
if (!nextPos.isValid(n, m)) {
119+
continue;
120+
}
121+
122+
switch(direction) {
123+
case 1:
124+
dice.rollEast();
125+
break;
126+
case 2:
127+
dice.rollWest();
128+
break;
129+
case 3:
130+
dice.rollNorth();
131+
break;
132+
case 4:
133+
dice.rollSouth();
134+
break;
135+
}
136+
137+
pos = nextPos;
138+
139+
if (map[pos.x][pos.y] == 0) {
140+
map[pos.x][pos.y] = dice.getBottom();
141+
} else {
142+
dice.setBottom(map[pos.x][pos.y]);
143+
map[pos.x][pos.y] = 0;
144+
}
145+
146+
bw.write(dice.getTop() + "\n");
147+
}
148+
149+
bw.flush();
150+
bw.close();
151+
br.close();
152+
}
153+
}
154+
```

0 commit comments

Comments
 (0)