[URL]
https://www.acmicpc.net/problem/14499
14499번: 주사위 굴리기
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 주사위를 놓은 칸에 쓰여 있는 수는 항상 0이다. 지도의 각 칸에 쓰여 있는 수는 10을 넘지 않는 자연수 또는 0이다. 마
www.acmicpc.net
[풀이 과정]
* 시뮬레이션
1. 4방향에 따라 어떤식으로 주사위의 평면도가 바뀌는지 설정 (dir == 1~ 4)
2. 4방향에 따라 주사위의 바닥면이 범위 안에 있는지 검사
3. 조건에 따라 업데이트
조건1) 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다.
조건 2) 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.
[소스 코드]
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | #include <stdio.h> struct Box { int x; int y; int top; int bottom; int left; int right; int front; int back; }; Box box; int N, M, K; int map[21][21]; void init() { box.top = 0; box.back = 0; box.right = 0; box.left = 0; box.front = 0; box.bottom = 0; scanf("%d %d %d %d %d", &N, &M, &box.x, &box.y, &K); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { scanf("%d", &map[i][j]); } } } bool isInside(int nx, int ny) { if (nx >= 0 && ny >= 0 && nx < N && ny < M) return true; else return false; } void go(int dir) { int nx, ny; int pre_top = box.top; int pre_bottom = box.bottom; int pre_left = box.left; int pre_right = box.right; int pre_front = box.front; int pre_back = box.back; if (dir == 1) //주사위 동쪽으로 굴리자 { nx = box.x; ny = box.y + 1; if (isInside(nx, ny)) // map[N][M] 범위 안에 있는가? { if (map[nx][ny] == 0) //바닥면이 0인가?? { box.top = pre_left; box.bottom = pre_right; box.left = pre_bottom; box.right = pre_top; box.front = pre_front; box.back = pre_back; map[nx][ny] = box.bottom; box.x = nx; box.y = ny; } else { box.top = pre_left; box.bottom = map[nx][ny]; box.left = pre_bottom; box.right = pre_top; box.front = pre_front; box.back = pre_back; map[nx][ny] = 0; box.x = nx; box.y = ny; } printf("%d\n", box.top); } else return; } if (dir == 2) //주사위 서쪽으로 굴리자 { nx = box.x; ny = box.y - 1; if (isInside(nx, ny)) { if (map[nx][ny] == 0) //바닥면이 0인가?? { box.top = pre_right; box.bottom = pre_left; box.left = pre_top; box.right = pre_bottom; box.front = pre_front; box.back = pre_back; map[nx][ny] = box.bottom; box.x = nx; box.y = ny; } else { box.top = pre_right; box.bottom = map[nx][ny]; box.left = pre_top; box.right = pre_bottom; box.front = pre_front; box.back = pre_back; map[nx][ny] = 0; box.x = nx; box.y = ny; } printf("%d\n", box.top); } else return; } if (dir == 3) //주사위 북쪽으로 굴리자 { nx = box.x - 1; ny = box.y; if (isInside(nx, ny)) { if (map[nx][ny] == 0) //바닥면이 0인가?? { box.top = pre_front; box.bottom = pre_back; box.left = pre_left; box.right = pre_right; box.front = pre_bottom; box.back = pre_top; map[nx][ny] = box.bottom; box.x = nx; box.y = ny; } else { box.top = pre_front; box.bottom = map[nx][ny]; box.left = pre_left; box.right = pre_right; box.front = pre_bottom; box.back = pre_top; map[nx][ny] = 0; box.x = nx; box.y = ny; } printf("%d\n", box.top); } else return; } if (dir == 4) //주사위 남쪽으로 굴리자 { nx = box.x + 1; ny = box.y; if (isInside(nx, ny)) { if (map[nx][ny] == 0) //바닥면이 0인가?? { box.top = pre_back; box.bottom = pre_front; box.left = pre_left; box.right = pre_right; box.front = pre_top; box.back = pre_bottom; map[nx][ny] = box.bottom; box.x = nx; box.y = ny; } else { box.top = pre_back; box.bottom = map[nx][ny]; box.left = pre_left; box.right = pre_right; box.front = pre_top; box.back = pre_bottom; map[nx][ny] = 0; box.x = nx; box.y = ny; } printf("%d\n", box.top); } else return; } } int main() { init(); for (int k = 0; k < K; k++) { int dir; scanf("%d", &dir); go(dir); } return 0; } | cs |
'Algorithm > 백준' 카테고리의 다른 글
[BOJ] - 14503. 로봇 청소기 (0) | 2019.04.13 |
---|---|
[BOJ] - 16234. 인구 이동 (0) | 2019.04.12 |
[BOJ] - 14500. 테트로미노 (0) | 2019.04.11 |
[BOJ] 14502. 연구소 (0) | 2019.04.10 |
[BOJ] - 2667. 단지번호붙이기 (0) | 2019.04.09 |