[BOJ-Code] 21611 - 마법사 상어와 블리자드

Posted by DHniyeo Blog on April 10, 2024

문제 링크

💡 구현/시뮬레이션

Memory 2044KB Time 8ms Code Length 4848B

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//	입력: N, M
//	N*N 맵
//	M개의 줄에 d와 s 주어짐
//	출력 : 1x폭발한 1번구슬 + 2x폭발한 2번구슬 개수 + 3x 폭발한 3번구슬 개수

#include<iostream>
#include<cstring>
using namespace std;

int N, M;
int ball_map[49][49];
int straight[2500]; // 일자 배열
struct info {
	int d, s;
};
struct loc {
	int y, x;
};
info todo[100];
int ballcnt[4]; // 1,2,3만씀
int dy[] = { -1,1,0,0 };
int dx[] = { 0,0,-1,1 };

int spin_dy[] = { 0,1,0,-1 }; // 왼 아래 오른 위
int spin_dx[] = { -1,0,1,0 };

void init() {
	memset(ballcnt, 0, sizeof(ballcnt));
	cin >> N >> M;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> ball_map[i][j];
		}
	}
	for (int i = 0; i < M; i++) {
		int d, s;
		cin >> d >> s;
		d--;
		todo[i] = { d,s };
	}
}
void cnt_ball_in_straight(int idx) {
	if (straight[idx] == 1) {
		ballcnt[1]++;
	}
	else if (straight[idx] == 2) {
		ballcnt[2]++;
	}
	else if (straight[idx] == 3) {
		ballcnt[3]++;
	}
}
void blizard(int d, int s) {
	loc shark = { N / 2, N / 2 };
	for (int i = 1; i <= s; i++) {
		int ny = shark.y + dy[d] * i;
		int nx = shark.x + dx[d] * i;
		ball_map[ny][nx] = 0;
	}
}
void pushstraight() { // 배열 댕김
	for (int i = 1; i < N*N; i++) {
		if (straight[i] != 0) {
			int nowi = i;
			while (1) {
				int ni = nowi - 1;
				if (ni < 1) break;
				if (straight[ni] != 0) break;
				straight[ni] = straight[nowi];
				straight[nowi] = 0;
				nowi = ni;
			}
		}
	}
}
void makestaright() {
	loc now = { N / 2, N / 2 };
	int adder = 1;
	int dir = 0;

	int times = 0;
	int limit_times = 1;
	int cnt = 0;

	int num = 0;
	while (1) {
		int ny = now.y + spin_dy[dir];
		int nx = now.x + spin_dx[dir];
		if (ny == 0 && nx == -1) break;

		num++;
		times++;

		straight[num] = ball_map[ny][nx];

		if (times == limit_times) { // 정해진 길이 만큼 움직이고
			cnt++;
			times = 0;
			dir = (dir + 1) % 4;
		}
		if (cnt == 2) { // 2번 움직였다면 limit + 1
			cnt = 0;
			limit_times += 1;
		}
		now.y = ny;
		now.x = nx;
	}
	pushstraight();
}

bool breakstragiht() {
	int num = 0;
	int cnt = 1;
	int flag = false;
	int lastpoint = 0;
	for (int i = 1; i < N*N; i++) {
		lastpoint = i;
		if (straight[i] == 0)break;
		if (num != straight[i]) {
			if (cnt >= 4) {
				flag = true;
				for (int j = i - cnt; j < i; j++) {
					cnt_ball_in_straight(j);
					straight[j] = 0;
				}

			}
			num = straight[i];
			cnt = 1; // cnt 초기화
		}
		else {
			cnt++;
		}
	}
	if (cnt >= 4) { // 마지막에 한번 더해줘야함
		flag = true;
		for (int j = lastpoint - cnt; j < lastpoint; j++) {
			cnt_ball_in_straight(j);
			straight[j] = 0;
		}
	}
	pushstraight();

	return flag;
}
void makenewstraight() {
	int newstraight[2500] = { 0 };
	int newi = 1;

	int num = straight[1];
	int cnt = 0;
	int lastpoint = 0;
	for (int i = 1; i < N*N; i++) {
		if (straight[i] == 0)break;
		if (newi >= N * N) break;
		if (num != straight[i]) {
			newstraight[newi++] = cnt;
			newstraight[newi++] = num;
			num = straight[i];
			cnt = 1; // cnt 초기화
		}
		else {
			cnt++;
		}
	}
	newstraight[newi++] = cnt;
	newstraight[newi++] = num;

	memset(straight, 0, sizeof(straight));
	for (int i = 1; i < N*N; i++) {
		straight[i] = newstraight[i];
	}
}
void makeball_map() {
	loc now = { N / 2, N / 2 };
	int adder = 1;
	int dir = 0;

	int times = 0;
	int limit_times = 1;
	int cnt = 0;

	int num = 0;
	while (1) {
		int ny = now.y + spin_dy[dir];
		int nx = now.x + spin_dx[dir];
		if (ny == 0 && nx == -1) break;

		num++;
		times++;

		ball_map[ny][nx] = straight[num];

		if (times == limit_times) { // 정해진 길이 만큼 움직이고
			cnt++;
			times = 0;
			dir = (dir + 1) % 4;
		}
		if (cnt == 2) { // 2번 움직였다면 limit + 1
			cnt = 0;
			limit_times += 1;
		}
		now.y = ny;
		now.x = nx;
	}
}




int getscore() {
	//출력: 1x폭발한 1번구슬 + 2x폭발한 2번구슬 개수 + 3x 폭발한 3번구슬 개수
	int sum = 0;
	sum = ballcnt[1] + ballcnt[2] * 2 + ballcnt[3] * 3;
	return sum;
}
void printstraight() {
	cout << endl;
	for (int i = 0; i < N*N; i++) {
		cout << straight[i] << " ";
	}
}
void printballmap() {
	cout << endl;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cout << ball_map[i][j] << " ";
		}
		cout << endl;
	}
}
int main() {
	init();
	for (int i = 0; i < M; i++) {
		info now = todo[i];
		//1. d방향으로 거리 s 이하인 모든칸에 얼음파편 던지기
		blizard(now.d, now.s);
		//printballmap();
		// 맵 일자 배열로 재구성.
		makestaright();
		//printstraight();

		//2. 연속된 숫자 폭파
		while (1) {
			if (!breakstragiht()) break;
			//printstraight();
		}

		//3. 연속된 숫자파악해서 구슬의 개수
		makenewstraight();
		//printstraight();
		//구슬 번호로 맵 재구성하기.
		makeball_map();
		//printballmap();
	}
	cout << getscore() << endl;
}

이 코드는 N*N 크기의 맵에서 M개의 작업을 수행하는 프로그램이다. 각 작업은 방향 d와 거리 s를 입력으로 받아 해당 방향으로 거리 s 이하의 칸에 있는 구슬을 제거하는 것이다.

프로그램은 다음과 같은 과정을 거친다:

초기화: N, M을 입력받고 맵에 구슬 정보를 저장한다.

각 작업을 수행하면서 구슬을 제거하고, 맵을 일자 배열로 재구성한다.

연속된 숫자를 폭파시키고, 각 숫자별로 폭파한 구슬의 개수를 센다.

새로운 일자 배열을 만들어 구슬의 번호와 개수를 저장하고, 이를 다시 맵에 반영한다.

모든 작업을 마치면 1번 구슬의 개수에 1을 곱하고, 2번 구슬의 개수에 2를 곱하고, 3번 구슬의 개수에 3을 곱한 뒤 이들을 모두 더한 값을 출력한다.