Algorithm 문제풀이
[BOJ] 백준 14499 - 주사위굴리기 (JAVA)
cjsong
2019. 6. 4. 22:30
시물레이션
주사위가 동,서,남,북 움직이는 경우에 따라서 규칙을 찾아내고 check함수로 경계값 검사했습니다.
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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
private static int dotX;
private static int dotY;
private static int[][] map;
private static int row;
private static int col;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
row = Integer.parseInt(st.nextToken());
col = Integer.parseInt(st.nextToken());
map = new int[row][col];
dotX = Integer.parseInt(st.nextToken());
dotY = Integer.parseInt(st.nextToken());
int commandNum = Integer.parseInt(st.nextToken());
for (int i = 0; i < map.length; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < map[i].length; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
StringBuilder sb = new StringBuilder();
int[][] dot = new int[4][3];
st = new StringTokenizer(br.readLine(), " ");
while (st.hasMoreTokens()) {
int command = Integer.parseInt(st.nextToken());
switch (command) {
case 1:
if (check(dotX, dotY+1)) {
int a = dot[1][2];
int b = dot[1][0];
int c = dot[1][1];
int d = dot[3][1];
dot[1][1] = b;
dot[3][1] = a;
dot[1][2] = c;
dot[1][0] = d;
if (map[dotX][++dotY] == 0) {
map[dotX][dotY] = dot[3][1];
} else {
dot[3][1] = map[dotX][dotY];
map[dotX][dotY] =0;
}
}
break;
case 2:
if (check(dotX, dotY-1)) {
int a = dot[1][2];
int b = dot[1][0];
int c = dot[1][1];
int d = dot[3][1];
dot[1][1] = a;
dot[3][1] = b;
dot[1][0] = c;
dot[1][2] = d;
if (map[dotX][--dotY] == 0) {
map[dotX][dotY] = dot[3][1];
} else {
dot[3][1] = map[dotX][dotY];
map[dotX][dotY] =0;
}
}
break;
case 3:
if (check(dotX -1 , dotY)) {
int temp = dot[3][1];
for (int i = 3; i > 0; i--) {
dot[i][1] = dot[i - 1][1];
}
dot[0][1] = temp;
if (map[--dotX][dotY] == 0) {
map[dotX][dotY] = dot[3][1];
} else {
dot[3][1] = map[dotX][dotY];
map[dotX][dotY] =0;
}
}
break;
case 4:
if (check(dotX +1, dotY)) {
int temp = dot[0][1];
for (int i = 0; i <3 ; i++) {
dot[i][1] = dot[i+1][1];
}
dot[3][1] = temp;
if (map[++dotX ][dotY] == 0) {
map[dotX ][dotY] = dot[3][1];
} else {
dot[3][1] = map[dotX][dotY];
map[dotX][dotY] =0;
}
}
break;
default:
break;
}
}
System.out.println(sb.toString());
}// end of main
public static boolean check(int x, int y) {
if(x >=0 && y >=0 && x < row && y < col ) {
return true;
}
return false;
}
}// end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |