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;
}
#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
中주사위 면을 갱신
やろうReference
この問題について(BOJ 14499:サイコロを投げる-C++), 我々は、より多くの情報をここで見つけました https://velog.io/@neity16/BOJ-14499-주사위-굴리기-Cテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol