728x90
삼성 SW 역량 테스트 기출 문제
https://www.acmicpc.net/problem/14499
문제 그대로를 코드로 구현하면 된다. 다만 주의할 것은 좌표가 (x, y)가 아니라 (y, x)라는 것이다. 따라서 입력받을 때 y, x 순으로 받아야 한다. 나는 그래서 그냥 row, col로 받았다.
#include <iostream>
#include <vector>
using namespace std;
int n, m;
int dice[6] = { 0 };
vector<vector<int>> map;
void toNorth(int& nowRow, int& nowCol, int& floorIndex, int& ceilingIndex, int& westIndex, int& eastIndex, int& northIndex, int& southIndex) {
if (nowRow - 1 < 0) return;
int temp = floorIndex;
floorIndex = northIndex;
northIndex = ceilingIndex;
ceilingIndex = southIndex;
southIndex = temp;
nowRow--;
if (map[nowRow][nowCol] == 0) {
map[nowRow][nowCol] = dice[floorIndex];
}
else {
dice[floorIndex] = map[nowRow][nowCol];
map[nowRow][nowCol] = 0;
}
cout << dice[ceilingIndex] << '\n';
}
void toSouth(int& nowRow, int& nowCol, int& floorIndex, int& ceilingIndex, int& westIndex, int& eastIndex, int& northIndex, int& southIndex) {
if (nowRow + 1 >= n) return;
int temp = floorIndex;
floorIndex = southIndex;
southIndex = ceilingIndex;
ceilingIndex = northIndex;
northIndex = temp;
nowRow++;
if (map[nowRow][nowCol] == 0) {
map[nowRow][nowCol] = dice[floorIndex];
}
else {
dice[floorIndex] = map[nowRow][nowCol];
map[nowRow][nowCol] = 0;
}
cout << dice[ceilingIndex] << '\n';
}
void toEast(int& nowRow, int& nowCol, int& floorIndex, int& ceilingIndex, int& westIndex, int& eastIndex, int& northIndex, int& southIndex) {
if (nowCol + 1 >= m) return;
int temp = westIndex;
westIndex = floorIndex;
floorIndex = eastIndex;
eastIndex = ceilingIndex;
ceilingIndex = temp;
nowCol++;
if (map[nowRow][nowCol] == 0) {
map[nowRow][nowCol] = dice[floorIndex];
}
else {
dice[floorIndex] = map[nowRow][nowCol];
map[nowRow][nowCol] = 0;
}
cout << dice[ceilingIndex] << '\n';
}
void toWest(int& nowRow, int& nowCol, int& floorIndex, int& ceilingIndex, int& westIndex, int& eastIndex, int& northIndex, int& southIndex) {
if (nowCol - 1 < 0) return;
int temp = floorIndex;
floorIndex = westIndex;
westIndex = ceilingIndex;
ceilingIndex = eastIndex;
eastIndex = temp;
nowCol--;
if (map[nowRow][nowCol] == 0) {
map[nowRow][nowCol] = dice[floorIndex];
}
else {
dice[floorIndex] = map[nowRow][nowCol];
map[nowRow][nowCol] = 0;
}
cout << dice[ceilingIndex] << '\n';
}
int main() {
int nowCol, nowRow, k;
cin >> n >> m >> nowRow >> nowCol >> k;
map = vector<vector<int>>(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}
int floorIndex = 0;
int eastIndex = 1;
int ceilingIndex = 2;
int westIndex = 3;
int northIndex = 4;
int southIndex = 5;
for (int i = 0; i < k; i++) {
int direction;
cin >> direction;
switch (direction) {
case 1:
toEast(nowRow, nowCol, floorIndex, ceilingIndex, westIndex, eastIndex, northIndex, southIndex);
break;
case 2:
toWest(nowRow, nowCol, floorIndex, ceilingIndex, westIndex, eastIndex, northIndex, southIndex);
break;
case 3:
toNorth(nowRow, nowCol, floorIndex, ceilingIndex, westIndex, eastIndex, northIndex, southIndex);
break;
case 4:
toSouth(nowRow, nowCol, floorIndex, ceilingIndex, westIndex, eastIndex, northIndex, southIndex);
break;
}
}
return 0;
}
728x90
'알고리즘 문제' 카테고리의 다른 글
[백준] 17779번 게리맨더링 2 (0) | 2020.02.18 |
---|---|
[백준] 17471번 게리맨더링 (0) | 2020.02.18 |
[백준] 9507번 Generations of Tribbles (0) | 2020.02.17 |
[백준] 11057번 오르막 수 (0) | 2020.02.17 |
[백준] 11727번 2×n 타일링 2 (0) | 2020.02.17 |