BOJ 14499 : 주사위 굴리기 - C++
주사위 굴리기
코드
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
#include <deque>
#include <numeric>
#include <map>
#define ll long long
using namespace std;
// 0928 ~ 1057
int dice[7] ={0, 1,2,3,4,5,6}; // up, back, right, left, front. bottom
int dice_num[7] = {0, 0,0,0,0,0,0};
int dr[4] = {0, 0, -1, 1}; // 동 서 북 남
int dc[4] = {1, -1, 0, 0};
int board[22][22];
int N,M,r,c,K,d;
void changeDir(int d){
int tmp[7] = {0,};
/* 동쪽으로 주사위 Turn */
if(d == 1){
tmp[3] = dice[1]; // up -> right
tmp[2] = dice[2]; // back -> back
tmp[6] = dice[3]; // right -> bottom
tmp[1] = dice[4]; // left -> up
tmp[5] = dice[5]; // front -> front
tmp[4] = dice[6]; // bottom -> left
}else if(d == 2){
/* 서쪽 */
tmp[4] = dice[1]; // up -> left
tmp[2] = dice[2]; // back -> back
tmp[1] = dice[3]; // right -> up
tmp[6] = dice[4]; // left -> bottom
tmp[5] = dice[5]; // front -> front
tmp[3] = dice[6]; // bottom -> right
}else if(d == 3){
/* 북쪽 */
tmp[2] = dice[1]; // up -> back
tmp[6] = dice[2]; // back -> bottom
tmp[3] = dice[3]; // right -> right
tmp[4] = dice[4]; // left -> left
tmp[1] = dice[5]; // front -> up
tmp[5] = dice[6]; // bottom -> front
}else if(d == 4){
/* 남쪽 */
tmp[5] = dice[1]; // up -> front
tmp[1] = dice[2]; // back -> up
tmp[3] = dice[3]; // right -> right
tmp[4] = dice[4]; // left -> left
tmp[6] = dice[5]; // front -> bottom
tmp[2] = dice[6]; // bottom -> back
}
for(int i=1;i<=6;i++)
dice[i] = tmp[i];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M >> r >> c >> K;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin >> board[i][j];
while(K--)
{
cin >> d;
int nr = r + dr[d-1];
int nc = c + dc[d-1];
/* 범위를 넘으면 pass */
if(nr <0 or nc<0 or nr>=N or nc>=M) continue;
changeDir(d);
/* MAP -> 주사위 */
if(board[nr][nc] != 0){
dice_num[dice[6]] = board[nr][nc];
board[nr][nc] = 0;
}else{
/* 주사위 -> MAP */
board[nr][nc] = dice_num[dice[6]];
}
r = nr;
c = nc;
cout << dice_num[dice[1]] << '\n';
}
return 0;
}
- 핵심
주사위를 굴릴 때
마다 up / back / right / left / front / bottom
에 있는 주사위 면을 갱신
해줘한다
Author And Source
이 문제에 관하여(BOJ 14499 : 주사위 굴리기 - C++), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@neity16/BOJ-14499-주사위-굴리기-C
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
#include <cstdio> #include <vector> #include <queue> #include <iostream> #include <cmath> #include <algorithm> #include <set> #include <deque> #include <numeric> #include <map> #define ll long long using namespace std; // 0928 ~ 1057 int dice[7] ={0, 1,2,3,4,5,6}; // up, back, right, left, front. bottom int dice_num[7] = {0, 0,0,0,0,0,0}; int dr[4] = {0, 0, -1, 1}; // 동 서 북 남 int dc[4] = {1, -1, 0, 0}; int board[22][22]; int N,M,r,c,K,d; void changeDir(int d){ int tmp[7] = {0,}; /* 동쪽으로 주사위 Turn */ if(d == 1){ tmp[3] = dice[1]; // up -> right tmp[2] = dice[2]; // back -> back tmp[6] = dice[3]; // right -> bottom tmp[1] = dice[4]; // left -> up tmp[5] = dice[5]; // front -> front tmp[4] = dice[6]; // bottom -> left }else if(d == 2){ /* 서쪽 */ tmp[4] = dice[1]; // up -> left tmp[2] = dice[2]; // back -> back tmp[1] = dice[3]; // right -> up tmp[6] = dice[4]; // left -> bottom tmp[5] = dice[5]; // front -> front tmp[3] = dice[6]; // bottom -> right }else if(d == 3){ /* 북쪽 */ tmp[2] = dice[1]; // up -> back tmp[6] = dice[2]; // back -> bottom tmp[3] = dice[3]; // right -> right tmp[4] = dice[4]; // left -> left tmp[1] = dice[5]; // front -> up tmp[5] = dice[6]; // bottom -> front }else if(d == 4){ /* 남쪽 */ tmp[5] = dice[1]; // up -> front tmp[1] = dice[2]; // back -> up tmp[3] = dice[3]; // right -> right tmp[4] = dice[4]; // left -> left tmp[6] = dice[5]; // front -> bottom tmp[2] = dice[6]; // bottom -> back } for(int i=1;i<=6;i++) dice[i] = tmp[i]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> r >> c >> K; for(int i=0;i<N;i++) for(int j=0;j<M;j++) cin >> board[i][j]; while(K--) { cin >> d; int nr = r + dr[d-1]; int nc = c + dc[d-1]; /* 범위를 넘으면 pass */ if(nr <0 or nc<0 or nr>=N or nc>=M) continue; changeDir(d); /* MAP -> 주사위 */ if(board[nr][nc] != 0){ dice_num[dice[6]] = board[nr][nc]; board[nr][nc] = 0; }else{ /* 주사위 -> MAP */ board[nr][nc] = dice_num[dice[6]]; } r = nr; c = nc; cout << dice_num[dice[1]] << '\n'; } return 0; }
- 핵심
주사위를 굴릴 때
마다up / back / right / left / front / bottom
에 있는주사위 면을 갱신
해줘한다
Author And Source
이 문제에 관하여(BOJ 14499 : 주사위 굴리기 - C++), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@neity16/BOJ-14499-주사위-굴리기-C저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)