[BOJ] 14499 주사위 굴리기

Time Lapse :56min 58sec

14499.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <memory.h>
#include <vector>
using namespace std;
int N, M, K;
int MAP[20][20];
int command[1001];
int dx[5] = {0, 1, -1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};
class DICE{
public :
int x;
int y;
int info[4][3];
DICE(){
memset(info,0,sizeof(info));
}
int print() {
return info[1][1];
}
void move(int cmd) {
switch(cmd){
case 1:
swap(info[1][1],info[1][2]);
swap(info[1][0],info[1][1]);
swap(info[3][1],info[1][0]);
break;
case 2:
swap(info[1][1],info[1][0]);
swap(info[1][2],info[1][1]);
swap(info[3][1],info[1][2]);

break;
case 3:
swap(info[1][1],info[0][1]);
swap(info[2][1],info[1][1]);
swap(info[3][1],info[2][1]);
break;
case 4:
swap(info[2][1],info[3][1]);
swap(info[1][1],info[2][1]);
swap(info[0][1],info[1][1]);
break;
}
x += dx[cmd];
y += dy[cmd];
}
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}

};
DICE dice;

void print_dice(){
for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
cout<<dice.info[i][j]<<" ";
}
cout<<endl;
}
}

void play(){
vector<int> answer;
for(int i=0;i<K;i++){
int next_x = dice.x + dx[command[i]];
int next_y = dice.y + dy[command[i]];
if(0<=next_x && next_x<M && 0<=next_y && next_y<N){
dice.move(command[i]);
if(MAP[next_y][next_x]==0)
MAP[next_y][next_x]=dice.info[3][1];
else{
dice.info[3][1]=MAP[next_y][next_x];
MAP[next_y][next_x] = 0;
}
answer.push_back(dice.print());
}
}
for(int i=0;i<answer.size();i++)
cout<<answer[i]<<endl;
}

int main(void){
cin>>N>>M>>dice.y>>dice.x>>K;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
cin>>MAP[i][j];
}
}
for(int i=0;i<K;i++){
cin>>command[i];
}
play();
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/14499/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.