[백준 14499번 C/C++] 주사위 굴리기
[백준 14499번 C/C++] 주사위 굴리기

https://www.acmicpc.net/problem/14499
해결전략
구현
x, y 좌표가 대다수의 코딩테스트 문제의 반대여서 헷갈렸다. 문제를 이해하는데 많은 시간이 걸렸다.
이동할 때마다 주사위가 구른다. ' 북 >> 남 >> 서 >> 동' 순서로 주사위는 굴러간다.
정답코드
#include <iostream> #include <vector> using namespace std; int n, m; int startX, startY; int k; // 명령의 개수 vector<vector<int>> v; vector<int> dice(6); // [0] // [4][1][5] // [2] // [3] void Roll(int dir) { vector<int> tmp(6); if (dir == 1){ // 동 tmp[0] = dice[0]; tmp[1] = dice[4]; tmp[2] = dice[2]; tmp[3] = dice[5]; tmp[4] = dice[3]; tmp[5] = dice[1]; } else if (dir == 2){ // 서 tmp[0] = dice[0]; tmp[1] = dice[5]; tmp[2] = dice[2]; tmp[3] = dice[4]; tmp[4] = dice[1]; tmp[5] = dice[3]; } else if (dir == 3) { // 북 tmp[0] = dice[1]; tmp[1] = dice[2]; tmp[2] = dice[3]; tmp[3] = dice[0]; tmp[4] = dice[4]; tmp[5] = dice[5]; } else if (dir == 4) { // 남 tmp[0] = dice[3]; tmp[1] = dice[0]; tmp[2] = dice[1]; tmp[3] = dice[2]; tmp[4] = dice[4]; tmp[5] = dice[5]; } dice = tmp; } void Move(int dir) { int x = startX; int y = startY; if (dir == 1){ y++; } else if (dir == 2) { y--; } else if (dir == 3) { x--; } else if (dir == 4) { x++; } // 범위를 벋어난 경우 if (x < 0 || n <= x || y < 0 || m <= y) return; // 범위 내인 경우 Roll(dir); // 이동한 위치가 지도 내일 경우, 주사위 굴림 처리 if (v[x][y] == 0){ // 이동한 위치가 지도 내일 경우, 주사위 굴림 처리 v[x][y] = dice[1]; } else{ // 지도의 해당 칸이 0인 경우, 주사위의 바닥면을 지도에 복사 dice[1] = v[x][y]; v[x][y] = 0; } startX = x; startY = y; cout << dice[3] << "\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> startX >> startY >> k; v.resize(n, vector<int>(m)); int input; for (int x = 0; x < n; x++){ for (int y = 0; y < m; y++){ cin >> input; v[x][y] = input; } } int dir; for (int i = 0; i < k; i++){ cin >> dir; Move(dir); } return 0; }
'⭐ 코딩테스트 > 백준' 카테고리의 다른 글
[백준 15685번 C/C++] 드래곤 커브 (0) | 2024.05.07 |
---|---|
[백준 16236번 C/C++] 아기 상어 (0) | 2024.05.04 |
[백준 1520번 C/C++] 내리막 길 (0) | 2024.04.23 |
[백준 7682번 C/C++] 틱택토 (1) | 2024.04.20 |
[백준 15864번 C/C++] 사다리 조작 (1) | 2024.04.19 |
댓글
이 글 공유하기
다른 글
-
[백준 15685번 C/C++] 드래곤 커브
[백준 15685번 C/C++] 드래곤 커브
2024.05.07 -
[백준 16236번 C/C++] 아기 상어
[백준 16236번 C/C++] 아기 상어
2024.05.04 -
[백준 1520번 C/C++] 내리막 길
[백준 1520번 C/C++] 내리막 길
2024.04.23 -
[백준 7682번 C/C++] 틱택토
[백준 7682번 C/C++] 틱택토
2024.04.20
댓글을 사용할 수 없습니다.