Skip to content

Commit 24b4889

Browse files
authored
Merge pull request #307 from sam-gorithm/SeoYeonLee/14499
주사위 굴리기 / 골드 4 / 136ms
2 parents 7838037 + e90445a commit 24b4889

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

src/N14499/N14499_seoyeon.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package N14499;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class N14499_seoyeon {
9+
10+
static int M, N, K, x, y;
11+
static int[][] map = new int[20][20];
12+
// 주사위 정의(처음 0)
13+
// 윗면 북 동 서 남 아래
14+
static int[] dice = new int[7];
15+
// 동 서 북 남
16+
static int[] dr = {0, 0, 0, -1, 1};
17+
static int[] dc = {0, 1, -1, 0, 0};
18+
19+
public static void main(String[] args) throws IOException {
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
N = Integer.parseInt(st.nextToken());
24+
M = Integer.parseInt(st.nextToken());
25+
x = Integer.parseInt(st.nextToken());
26+
y = Integer.parseInt(st.nextToken());
27+
K = Integer.parseInt(st.nextToken());
28+
29+
for (int i = 0; i < N; i++) {
30+
st = new StringTokenizer(br.readLine());
31+
for (int j = 0; j < M; j++) {
32+
map[i][j] = Integer.parseInt(st.nextToken());
33+
}
34+
}
35+
36+
st = new StringTokenizer(br.readLine());
37+
int inst = 0;
38+
39+
while (st.hasMoreTokens()) {
40+
inst = Integer.parseInt(st.nextToken());
41+
solve(inst);
42+
}
43+
}
44+
45+
private static void solve(int inst) {
46+
47+
// 1. 다음 위치 계산:
48+
int nr = x + dr[inst];
49+
int nc = y + dc[inst];
50+
51+
52+
// 2. 경계 검사:
53+
if (nr < 0 || nr >= N || nc < 0 || nc >= M) {
54+
return;
55+
}
56+
57+
// 4. (경계를 벗어나지 않았으므로) 주사위를 굴립니다.
58+
// 각 case 문 안에서 값 교환(swap)에 필요한 임시 변수(temp)를 선언하고 사용합니다.
59+
60+
switch (inst) {
61+
case 1: // 동
62+
int curr = dice[1];
63+
dice[1] = dice[4];
64+
dice[4] = dice[6];
65+
dice[6] = dice[3];
66+
dice[3] = curr;
67+
break;
68+
69+
case 2: // 서
70+
curr = dice[1];
71+
dice[1] = dice[3];
72+
dice[3] = dice[6];
73+
dice[6] = dice[4];
74+
dice[4] = curr;
75+
break;
76+
77+
case 3: // 북
78+
79+
// 굴리기 전 윗면(1) 값을 임시 변수(temp)에 저장합니다.
80+
// 새 윗면(1)에 <- 현재 남쪽면(5) 값을 넣습니다.
81+
// 새 남쪽면(5)에 <- 현재 아랫면(6) 값을 넣습니다.
82+
// 새 아랫면(6)에 <- 현재 북쪽면(2) 값을 넣습니다.
83+
// 새 북쪽면(2)에 <- 임시 변수(temp)에 저장했던 (구)윗면 값을 넣습니다.
84+
curr = dice[1];
85+
dice[1] = dice[5];
86+
dice[5] = dice[6];
87+
dice[6] = dice[2];
88+
dice[2] = curr;
89+
break;
90+
91+
case 4: // 남
92+
93+
curr = dice[1];
94+
dice[1] = dice[2];
95+
dice[2] = dice[6];
96+
dice[6] = dice[5];
97+
dice[5] = curr;
98+
99+
break;
100+
}
101+
102+
// 5. (switch 문이 끝난 후) 지도와 상호작용합니다.
103+
// 이동한 새 위치(nx, ny)의 지도 칸(map[nx][ny]) 값을 확인합니다.
104+
// (if 문 사용)
105+
if(map[nr][nc]==0){
106+
map[nr][nc]=dice[6];
107+
}
108+
else{
109+
dice[6]=map[nr][nc];
110+
map[nr][nc]=0;
111+
}
112+
113+
x= nr;
114+
y= nc;
115+
116+
System.out.println(dice[1]);
117+
}
118+
119+
120+
}

0 commit comments

Comments
 (0)