본문 바로가기
Algorithm/문제 풀이 (Problem Solving)

[C++,구현] 백준 14499번 주사위 굴리기 문제풀이

by matters_ 2019. 3. 14.

문제링크

백준 14499번 주사위 굴리기

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 주사위를 놓은 칸에 쓰여 있는 수는 항상 0이다. 지도의 각 칸에 쓰여 있는 수는 10을 넘지 않는 자연수 또는 0이다. 마

www.acmicpc.net

 

구현문제를 풀어보았다.  구현문제가 익숙치 않아 코드도 길고 복잡하게 짜버렸다ㅜㅜ

다른분들의 코드를 보면서 최적화 시켜야겠다.

 

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
#include <cstdio>
int map[21][21];
int dice[6]={0,0,0,0,0,0};// 위,옆왼,아,옆오,옆위,옆아 
int N,M,x,y,K;
 
 
void dice_move(int a){
        int t0,t1,t2,t3,t4,t5;
        switch(a){
        case 1:
            t1=dice[1],t2=dice[2],t3=dice[3],t0=dice[0];
            dice[3]=t0;
            dice[2]=t3;
            dice[1]=t2;
            dice[0]=t1;break;
        case 2:
            t1=dice[1],t2=dice[2],t3=dice[3],t0=dice[0];
            dice[3]=t2;
            dice[2]=t1;
            dice[1]=t0;
            dice[0]=t3;break;
        case 3:
            t0=dice[0],t2=dice[2],t4=dice[4],t5=dice[5];
            dice[4]=t0;
            dice[5]=t2;
            dice[2]=t4;
            dice[0]=t5;break;
        case 4:
            t0=dice[0],t2=dice[2],t4=dice[4],t5=dice[5];
            dice[4]=t2;
            dice[2]=t5;
            dice[5]=t0;
            dice[0]=t4;break;
        }
}
 
int isinrange(int a,int b){
    if(a<0||a>=N||b<0||b>=M) return 0;
    else{x=a;y=b;return 1;}
}
 
void whatisinthemap(){
    if(map[x][y]==0)map[x][y]=dice[2];
    else{dice[2]=map[x][y];map[x][y]=0;}
}
 
int main(){
    
    scanf("%d%d%d%d%d",&N,&M,&x,&y,&K);
 
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++)scanf("%d",&map[i][j]);
    }    
    
    for(int i=0;i<K;i++){
        int num;
        scanf("%d",&num);
        
        if(num==1){
            if(isinrange(x,y+1)){    
                dice_move(num);
                whatisinthemap();    
            }
            else continue;
        } 
        else if(num==2){
            if(isinrange(x,y-1)){    
                dice_move(num);
                whatisinthemap();    
            }
            else continue;
        }
        else if(num==3){
            if(isinrange(x-1,y)){
                dice_move(num);
                whatisinthemap();    
            }
            else continue;
        }
        else {
            if(isinrange(x+1,y)){
                dice_move(num);
                whatisinthemap();    
            }
            else continue;
        }
        printf("%d\n",dice[0]);
cs
함수를 3개 만들었다. 

하나는 주사위가 움직일 때 주사위의 각 방향에 쓰이는 숫자가 달라지게 하는 dice_move함수,
두번째는 주사위가 지도 바깥으로 나가면 안되므로 범위를 계산하는 isinrange함수, 
마지막으로 주사위가 움직인 지도공간에 0이 쓰여져 있으면 주사위의 아랫부분의 숫자가 지도에 쓰이고 0이 아니라면 
지도의 숫자가 주사위의 아랫부분에 씌며 지도의 숫자는 0이 되도록 하는 whatisinthemap함수 총 3개의 함수를 이용해 구현하였다.
 
코드를 작성하며 주의해야할 점은 
첫째로 dice_move함수에서 4개의 숫자가 동시에 움직이므로 모두 다른 변수안에 넣고 대입해줘야하는 점, 
문제를 정확히 읽고 코드를 그에 맞게 구현하였는지(남쪽으로 간다 -> isinrange(x+1,y)),
예외조건을 정확히 구현하였는지(a<0||a>=N||b<0||b>=M) 등을 주의 깊게 다시 확인해야 한다는 것이다. 


댓글