이것도 알아야 하네?
[C++] 백준 14499번 주사위 굴리기 (삼성 SW 역량 테스트 기출 문제) 본문
728x90
#include <iostream>
using namespace std;
int cube[7] = {0};
int x, y;
int offset_x[] = {0, 0, -1, 1}; // east west north south
int offset_y[] = {1, -1, 0, 0};
int WithinRange(int x, int y, int N, int M) {
if (x >= 0 && x < N && y >= 0 && y < M) return 1;
else return 0;
}
void Solve(int * graph, int dir, int N, int M) {
int next_x, next_y;
switch(dir) {
case 0: // east;
next_x = x + offset_x[dir];
next_y = y + offset_y[dir];
if (WithinRange(next_x, next_y, N, M)) {
int temp = cube[1];
cube[1] = cube[4]; cube[4] = cube[6];
cube[6] = cube[3]; cube[3] = temp;
if (graph[next_x * M + next_y] == 0) {
graph[next_x * M + next_y] = cube[6];
} else {
cube[6] = graph[next_x * M + next_y];
graph[next_x * M + next_y] = 0;
}
x = next_x; y = next_y;
cout << cube[1] << endl;
}
break;
case 1: // west;
next_x = x + offset_x[dir];
next_y = y + offset_y[dir];
if (WithinRange(next_x, next_y, N, M)) {
int temp = cube[1];
cube[1] = cube[3]; cube[3] = cube[6];
cube[6] = cube[4]; cube[4] = temp;
if (graph[next_x * M + next_y] == 0) {
graph[next_x * M + next_y] = cube[6];
} else {
cube[6] = graph[next_x * M + next_y];
graph[next_x * M + next_y] = 0;
}
x = next_x; y = next_y;
cout << cube[1] << endl;
}
break;
case 2: // north;
next_x = x + offset_x[dir];
next_y = y + offset_y[dir];
if (WithinRange(next_x, next_y, N, M)) {
int temp = cube[1];
cube[1] = cube[5]; cube[5] = cube[6];
cube[6] = cube[2]; cube[2] = temp;
if (graph[next_x * M + next_y] == 0) {
graph[next_x * M + next_y] = cube[6];
} else {
cube[6] = graph[next_x * M + next_y];
graph[next_x * M + next_y] = 0;
}
x = next_x; y = next_y;
cout << cube[1] << endl;
}
break;
case 3: // south;
next_x = x + offset_x[dir];
next_y = y + offset_y[dir];
if (WithinRange(next_x, next_y, N, M)) {
int temp = cube[1];
cube[1] = cube[2]; cube[2] = cube[6];
cube[6] = cube[5]; cube[5] = temp;
if (graph[next_x * M + next_y] == 0) {
graph[next_x * M + next_y] = cube[6];
} else {
cube[6] = graph[next_x * M + next_y];
graph[next_x * M + next_y] = 0;
}
x = next_x; y = next_y;
cout << cube[1] << endl;
}
break;
default:
break;
}
}
int main() {
int N, M, K;
cin >> N >> M >> x >> y >> K;
int *graph = new int[N * M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> graph[i * M + j];
}
}
int dir;
for (int k = 0; k < K; k++) {
cin >> dir;
//dir - 1;
Solve(graph, dir - 1, N, M);
}
delete[] graph;
}
728x90
'프로그래밍 > 알고리즘' 카테고리의 다른 글
[책 리뷰] 다이내믹 프로그래밍 완전 정복: 빠르고 우아한 상향식 문제 풀이법 (0) | 2021.11.19 |
---|---|
[C++] Programmers 미로 탈출 풀이 (2021 카카오 채용연계형 인턴십) (0) | 2021.11.13 |
[C++] 백준 14500번 테트로미노 (삼성 SW 역량 테스트 기출 문제) (0) | 2021.11.12 |
[C++] Programmers 기능개발 풀이 (stack/queue) (0) | 2021.11.10 |
[C++] Programmers 땅따먹기 풀이 (0) | 2021.11.10 |
Comments