반응형

https://www.acmicpc.net/problem/14499

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지

www.acmicpc.net

 

 

[ 문제풀이 ]

 

이 문제에서는 x가 세로, y가 가로이기 때문에 잘 읽어보고 문제를 푸셔야 합니다.

 

1. 지도와 현재 주사위의 좌표를 입력받습니다.

 

2. 방향에 따라 주사위를 굴려주시고, 주사위의 윗면은 다른 변수에 저장하여 따로 관리해줍니다.

 

3. 바닥이 0이라면 주사위에 있는 숫자를 복사해주고, 아니라면 바닥에 있는 숫자를 주사위에 복사하고 바닥은 0으로 갱신해줍니다.

 

4. 따로 저장해 놓은 윗면의 값을 출력해줍니다.

 

[ 소스 코드 ]

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
#include<iostream>
 
using namespace std;
 
int N, M, x, y, K;
int arr[20][20];
int dx[4= { 0,0,-1,1 };
int dy[4= { 1,-1,0,0 };
int dice[3][3];
int temp1;
int temp2;
 
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"&arr[i][j]);
        }
    }
 
    for (int i = 0; i < K; i++) {
        int dir;
        scanf("%d"&dir);
        int yy = y + dy[dir - 1];    //주사위가 움직일 좌표
        int xx = x + dx[dir - 1];
        if (xx >= 0 && xx < N && yy >= 0 && yy < M) {
            if (dir == 1) {    //동쪽
                if (arr[xx][yy] != 0) {
                    dice[1][2= arr[xx][yy];
                    arr[xx][yy] = 0;
                }
                else {
                    arr[xx][yy] = dice[1][2];
                }
                temp1 = dice[1][0];
                for (int j = 0; j < 2; j++) {
                    dice[1][j] = dice[1][j + 1];
                }
                dice[1][2= temp2;
                temp2 = temp1;
            }
            else if (dir == 2) {    //서쪽
                if (arr[xx][yy] != 0) {
                    dice[1][0= arr[xx][yy];
                    arr[xx][yy] = 0;
                }
                else {
                    arr[xx][yy] = dice[1][0];
                }
                temp1 = dice[1][2];
                for (int j = 2; j > 0; j--) {
                    dice[1][j] = dice[1][j - 1];
                }
                dice[1][0= temp2;
                temp2 = temp1;
            }
            else if (dir == 3) {    //북쪽
                if (arr[xx][yy] != 0) {
                    dice[0][1= arr[xx][yy];
                    arr[xx][yy] = 0;
                }
                else {
                    arr[xx][yy] = dice[0][1];
                }
                temp1 = dice[2][1];
                for (int j = 2; j > 0; j--) {
                    dice[j][1= dice[j - 1][1];
                }
                dice[0][1= temp2;
                temp2 = temp1;
            }
            else {        //남쪽
                if (arr[xx][yy] != 0) {
                    dice[2][1= arr[xx][yy];
                    arr[xx][yy] = 0;
                }
                else {
                    arr[xx][yy] = dice[2][1];
                }
                temp1 = dice[0][1];
                for (int j = 0; j < 2; j++) {
                    dice[j][1= dice[j + 1][1];
                }
                dice[2][1= temp2;
                temp2 = temp1;
            }
            printf("%d\n", temp2);
            y = yy;
            x = xx;
        }
    }
}
cs
반응형

+ Recent posts