반응형

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

 

17837번: 새로운 게임 2

재현이는 주변을 살펴보던 중 체스판과 말을 이용해서 새로운 게임을 만들기로 했다. 새로운 게임은 크기가 N×N인 체스판에서 진행되고, 사용하는 말의 개수는 K개이다. 말은 원판모양이고, 하

www.acmicpc.net

 

 

 

[ 문제풀이 ]

 

1. 현재 말의 위치를 저장할 vector 배열을 만들어 위치를 저장합니다.

 

2. piece 스트럭쳐를 만들어 위치와 방향을 저장한 후 말의 개수만큼 배열을 만들어 각 말의 위치와 방향을 저장해줍니다.

 

3. 말을 움직일 때 남을 말과 움직일 말을 vector배열에 저장해줍니다.

 

4. 움직일 말들을 규칙에 맞게 움직여주고 그 결과를 piece 배열에 갱신해줍니다.

 

5. 움직일 때마다 말이 4개 이상 겹치는지 체크해 주고, 겹치면 턴을 출력해줍니다.

 

6. 만약 턴이 1000번을 넘으면 -1을 출력해줍니다.

 

[ 소스코드 ]

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include<iostream>
#include<vector>
 
using namespace std;
 
struct piece {
    int y;
    int x;
    int dir;
};
 
int N, K;
int m[13][13];
vector<int> cur[13][13];
piece arr[11];
int dy[5= { 0,0,0,-1,1 };
int dx[5= { 0,1,-1,0,0 };
 
vector<int> change(vector<int> vect)    //빨간색으로 이동했을 경우
{
    for (int i = 0; i < vect.size() / 2; i++) {
        int temp = vect[i];
        vect[i] = vect[vect.size() - 1 - i];
        vect[vect.size() - 1 - i] = temp;
    }
 
    return vect;
}
 
void move(int num)        //말의 이동
{
    vector<int> temp;
    vector<int> remain;
    int y = arr[num].y;
    int x = arr[num].x;
    int dir = arr[num].dir;
 
    bool flag = false;
 
    for (int i = 0; i < cur[y][x].size();i++) {
        if (cur[y][x][i] == num) {
            flag = true;
        }
        if (flag == true) {
            temp.push_back(cur[y][x][i]);    //함께 움직일 말
        }
        else {
            remain.push_back(cur[y][x][i]);    //남아 있는 말
        }
    }
 
    int yy = y + dy[dir];
    int xx = x + dx[dir];
 
    if (yy > 0 && yy <= N && xx >0 && xx <= N) {
        if (m[yy][xx] == 0) {    //흰색 칸
            for (auto next : temp) {
                cur[yy][xx].push_back(next);
                arr[next] = { yy,xx,arr[next].dir };
            }
            cur[y][x] = remain;
        }
        else if (m[yy][xx] == 1) {    //빨간색 칸
            temp = change(temp);
            for (auto next : temp) {
                cur[yy][xx].push_back(next);
                arr[next] = { yy,xx,arr[next].dir };
            }
            cur[y][x] = remain;
        }
        else {    //파란색 칸
            if (dir % 2 == 0) {
                dir--;
            }
            else {
                dir++;
            }
            yy = y + dy[dir];
            xx = x + dx[dir];
            if (yy > 0 && yy <= N && xx > 0 && xx <= N) {
                if (m[yy][xx] == 0) {    //흰색 칸
                    for (auto next : temp) {
                        cur[yy][xx].push_back(next);
                        if (next == num) {
                            arr[next] = { yy,xx,dir };
                        }
                        else {
                            arr[next] = { yy,xx,arr[next].dir };
                        }
                    }
                    cur[y][x] = remain;
                }
                else if (m[yy][xx] == 1) {    //빨간색 칸
                    temp = change(temp);
                    for (auto next : temp) {
                        cur[yy][xx].push_back(next);
                        if (next == num) {
                            arr[next] = { yy,xx,dir };
                        }
                        else {
                            arr[next] = { yy,xx,arr[next].dir };
                        }
                    }
                    cur[y][x] = remain;
                }
                else {    //파란색 칸
                    arr[num] = { y,x,dir };
                }
            }
            else {    //벗어나는 경우
                arr[num] = { y,x,dir };
            }
        }
    }
    else {    //벗어나는 경우
        if (dir % 2 == 0) {
            dir--;
        }
        else {
            dir++;
        }
        yy = y + dy[dir];
        xx = x + dx[dir];
        if (yy > 0 && yy <= N && xx > 0 && xx <= N) {
            if (m[yy][xx] == 0) {    //흰색 칸
                for (auto next : temp) {
                    cur[yy][xx].push_back(next);
                    if (next == num) {
                        arr[next] = { yy,xx,dir };
                    }
                    else {
                        arr[next] = { yy,xx,arr[next].dir };
                    }
                }
                cur[y][x] = remain;
            }
            else if (m[yy][xx] == 1) {    //빨간색 칸
                temp = change(temp);
                for (auto next : temp) {
                    cur[yy][xx].push_back(next);
                    if (next == num) {
                        arr[next] = { yy,xx,dir };
                    }
                    else {
                        arr[next] = { yy,xx,arr[next].dir };
                    }
                }
                cur[y][x] = remain;
            }
            else {    //파란색 칸
                arr[num] = { y,x,dir };
            }
        }
    }
}
 
int main()
{
    scanf("%d %d"&N, &K);
 
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            scanf("%d"&m[i][j]);
        }
    }
 
    for (int i = 1; i <= K; i++) {
        int a, b, c;
        scanf("%d %d %d"&a, &b, &c);
        cur[a][b].push_back(i);
        arr[i] = { a,b,c };
    }
 
    int cnt = 0;
 
    while (1) {
        cnt++;
        for (int i = 1; i <= K; i++) {
            move(i);    //말들의 이동
            for (int i = 1; i <= N; i++) {    //말이 4개가 겹쳐있는지 확인
                for (int j = 1; j <= N; j++) {
                    if (cur[i][j].size() >= 4) {
                        printf("%d", cnt);
                        return 0;
                    }
                }
            }
        }
 
        if (cnt > 1000) {    //1000번을 넘은 경우
            printf("%d"-1);
            return 0;
        }
    }
}
cs
반응형

+ Recent posts