๊ธฐ๋ก๋ฐฉ

BOJ_16234 : ์ธ๊ตฌ ์ด๋™ ๋ณธ๋ฌธ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

BOJ_16234 : ์ธ๊ตฌ ์ด๋™

Soom_1n 2022. 6. 4. 02:39

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

16234๋ฒˆ: ์ธ๊ตฌ ์ด๋™

N×Nํฌ๊ธฐ์˜ ๋•…์ด ์žˆ๊ณ , ๋•…์€ 1×1๊ฐœ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋•…์—๋Š” ๋‚˜๋ผ๊ฐ€ ํ•˜๋‚˜์”ฉ ์กด์žฌํ•˜๋ฉฐ, rํ–‰ c์—ด์— ์žˆ๋Š” ๋‚˜๋ผ์—๋Š” A[r][c]๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์ธ์ ‘ํ•œ ๋‚˜๋ผ ์‚ฌ์ด์—๋Š” ๊ตญ๊ฒฝ์„ ์ด ์กด์žฌํ•œ๋‹ค. ๋ชจ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๋•… ํฌ๊ธฐ(N)์™€ ์ธ๊ตฌ ์ตœ์†Œ, ์ตœ๋Œ€๊ฐ’(L,R)์ด ์ฃผ์–ด์ง€๊ณ , 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ์˜ ์ธ๊ตฌ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ์ธ๊ตฌ์ˆ˜์˜ ์ฐจ์ด๊ฐ€ L, R ๊ฐ’ ์‚ฌ์ด๋ผ๋ฉด, ๊ตญ๊ฒฝ์„ ์„ ์—ฐ๋‹ค.
    • ๊ตญ๊ฒฝ์„ ์ด ์—ด๋ ค์„œ ํ†ตํ–‰์ด ๊ฐ€๋Šฅํ•œ ๋‚˜๋ผ๋“ค์„ ํ•œ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ๋Š”๋‹ค.
    • ํ•œ ๊ทธ๋ฃน์˜ ํ‰๊ท  ์ธ๊ตฌ์ˆ˜๋ฅผ ๊ทธ๋ฃน๋‚ด ๋ชจ๋“  ์นธ์— ์ฑ„์šด๋‹ค. (์ธ๊ตฌ๋ฅผ ํ‰์ค€ํ™”ํ•œ๋‹ค)
    • ์ด ํ‰์ค€ํ™” ์ž‘์—…์€ BFS๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
  • ๊ตญ๊ฒฝ์„ ์ด ํ•œ ๊ณณ์ด๋ผ๋„ ์—ด๋ ธ๋‹ค๋ฉด, ์ธ๊ตฌ ์ด๋™์ด ์žˆ์œผ๋ฏ€๋กœ ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚˜์•ผ ํ•œ๋‹ค.

 

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

#include<iostream>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

int BFS(vector<vector<int>>& A, int& i, int& j, vector<vector<int>>& group,
			int& G_count, int& L, int& R, vector<vector<int>>& same_G_list) {
	int sum = 0;
	queue<vector<int>> q;
	vector<int> pair(2);
	pair[0] = i;
	pair[1] = j;

	q.push(pair);
	group[i][j] = G_count;

	while (!q.empty()) {
		pair = q.front();
		int now_i = pair[0];
		int now_j = pair[1];
		sum += A[now_i][now_j];
		same_G_list[G_count].push_back(now_i);
		same_G_list[G_count].push_back(now_j);
		//printf("pair[0] : %d    pair[1] : %d     G_count : %d\n", pair[0], pair[1], G_count);

		// left
		int next_i = now_i;
		int next_j = now_j - 1;
		if (next_j >= 0 && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		//top
		next_i = now_i - 1;
		next_j = now_j;
		if (next_i >= 0 && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		//right
		next_i = now_i;
		next_j = now_j + 1;
		if (next_j < A.size() && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		//bottom
		next_i = now_i + 1;
		next_j = now_j;
		if (next_i < A.size() && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		q.pop();
	}

	return sum;
}

int main(void) {
	int N, L, R, day = -1, G_count;
	cin >> N >> L >> R;
	vector<vector<int>> A(N, vector<int>(N, 0));

	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> A[i][j];

	bool flag = true;
	while (flag) {
		flag = false;
		vector<vector<int>> group(N, vector<int>(N, 0)); // ์†Œ์† ๊ทธ๋ฃน
		vector<int> groupSum(N * N + 1, 0);
		vector<vector<int>> same_G_list(N * N + 1, vector<int>());
		G_count = 0;

		// ๋ชจ๋“  ๋‚˜๋ผ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋ฉฐ BFS ์ค€๋น„
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (group[i][j] == 0) {	// ์†Œ์† ๊ทธ๋ฃน์ด ์—†๋‹ค๋ฉด BFS ์‹œ์ž‘
					G_count++;
					groupSum[G_count] = BFS(A, i, j, group, 
							G_count, L, R, same_G_list);

					if (groupSum[G_count] > A[i][j])
						flag = true;
						// flag = true๋ฉด ๋‹ค์Œ ๋‚  ๊ฒ€์‚ฌ๋ฅผ ๋‹ค์‹œ ํ•ด์•ผํ•จ
				}
			}
		}

		// ๊ทธ๋ฃน ํ‰๊ท  ์ฒ˜๋ฆฌ
		for (int g = 1; g <= G_count; g++)
		{
			int size = same_G_list[g].size();
			int avg = groupSum[g] / (size / 2);

			for (int c = 0; c < size; c+=2)
			{
				A[same_G_list[g][c]][same_G_list[g][c + 1]] = avg;
			}
		}
		day++;
	}
	cout << day;

	return 0;
}

 

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

์ฝ”๋“œ๊ฐ€ ๊ฝค ๊ธธ๊ณ  ๋ณต์žกํ•˜๋ฏ€๋กœ ๋Š์–ด์„œ ์„ค๋ช…ํ•œ๋‹ค.

 

๋จผ์ € mainํ•จ์ˆ˜์˜ while๋ฌธ ๊ฒ‰ ๋ถ€๋ถ„์ด๋‹ค.

int main(void) {
	int N, L, R, day = -1, G_count;
	cin >> N >> L >> R;
	vector<vector<int>> A(N, vector<int>(N, 0));

	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> A[i][j];

	bool flag = true;
	while (flag) {
		// ์ƒ๋žต
	}
	cout << day;

	return 0;
}
  • N, L, R, A ๋Š” ๋ฌธ์ œ์˜ ์ •์˜์™€ ๊ฐ™๋‹ค. (A๋Š” vector๋ฅผ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ–ˆ๋‹ค)
  • day : ์ธ๊ตฌ ์ด๋™์ด ์ผ์–ด๋‚˜๋Š” ์ผ์ž ์ˆ˜
  • G_count : ๊ทธ๋ฃน ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธฐ๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ (1~N*N)
  • flag : while๋ฌธ ๋‚ด์—์„œ ๊ตญ๊ฒฝ์„ ์ด ์—ด๋ฆฌ๋ฉด true

๋‹ค์Œ์€ while๋ฌธ ์•ˆ์ชฝ์ด๋‹ค.

    while (flag) {
        flag = false;
        vector<vector<int>> group(N, vector<int>(N, 0)); // ์†Œ์† ๊ทธ๋ฃน
        vector<int> groupSum(N * N + 1, 0);
        vector<vector<int>> same_G_list(N * N + 1, vector<int>());
        G_count = 0;

        // ๋ชจ๋“  ๋‚˜๋ผ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋ฉฐ BFS ์ค€๋น„
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (group[i][j] == 0) {	// ์†Œ์† ๊ทธ๋ฃน์ด ์—†๋‹ค๋ฉด BFS ์‹œ์ž‘
                    G_count++;
                    groupSum[G_count] = BFS(A, i, j, group, 
                            G_count, L, R, same_G_list);

                    if (groupSum[G_count] > A[i][j])
                        flag = true;
                        // flag = true๋ฉด ๋‹ค์Œ ๋‚  ๊ฒ€์‚ฌ๋ฅผ ๋‹ค์‹œ ํ•ด์•ผํ•จ
                }
            }
        }

        // ๊ทธ๋ฃน ํ‰๊ท  ์ฒ˜๋ฆฌ
        for (int g = 1; g <= G_count; g++)
        {
            int size = same_G_list[g].size();
            int avg = groupSum[g] / (size / 2);

            for (int c = 0; c < size; c+=2)
            {
                A[same_G_list[g][c]][same_G_list[g][c + 1]] = avg;
            }
        }
        day++;
    }
  • flag๋Š” ๋ฐ”๋กœ false๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ๊ตญ๊ฒฝ์„ ์ด ์—ด๋ฆฐ๊ฒŒ ํ™•์ธ๋˜๋ฉด true๋กœ ๋ฐ”๊พผ๋‹ค.
  • group : BFS์˜ ๋ฐฉ๋ฌธ ํ™•์ธ์šฉ ๋ฆฌ์ŠคํŠธ ๊ฒธ, ์†Œ์†๋œ ๊ทธ๋ฃน์„ ๊ธฐ๋กํ•˜๋Š” vector ์ด๋‹ค. ( 0์ด๋ฉด ๋ฏธ๋ฐฉ๋ฌธ )
  • groupSum : ๊ฐ ๊ทธ๋ฃน ๋ณ„ ํ‰๊ท ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ด ํ•ฉ์„ ๊ธฐ๋กํ•˜๋Š” vector ์ด๋‹ค. ( ์ตœ๋Œ€ ๊ฐ’์€ G_count์˜ ์ตœ๋Œ€๊ฐ’์ธ N*N )
  • same_G_list : ๊ฐ™์€ ๊ทธ๋ฃน์˜ ์ขŒํ‘œ๋ฅผ ๊ธฐ๋กํ•œ vector์ด๋‹ค.
    ( i, j๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค. 3์ค‘์ฒฉ์€ ์•ˆ๋˜๊ณ  pair๋ฅผ ๊ตณ์ด ๋„ฃ๊ณ ์‹ถ์ง„ ์•Š์•„์„œ... )
  • ์ฒซ 2์ค‘ for๋ฌธ์€ ๋‚˜๋ผ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ๊ทธ๋ฃน์— ์†ํ•˜์ง€ ์•Š์€ ๊ณณ์„ BFS์˜ ์‹œ์ž‘์ ์œผ๋กœ ์‚ผ๋Š” ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
    • BFS์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ํ•œ ๊ทธ๋ฃน์˜ ์ด ํ•ฉ์„ ๋ฐ›๋Š”๋ฐ, ์‹œ์ž‘ ๋‚˜๋ผ์™€ ๊ฐ™๋‹ค๋ฉด ๊ทธ๋ฃน์— ๋‚˜๋ผ๊ฐ€ 1๊ฐœ์ธ ๊ฒƒ ์ด๋ฏ€๋กœ ๊ตญ๊ฒฝ์„ ์ด ์—ด๋ฆฌ์ง€ ์•Š์•˜๋‹ค๊ณ  ํŒ๋‹จํ•œ๋‹ค.
    • ์›๋ž˜๋Š” void๋‚˜ bool๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์‹์œผ๋กœ ์งฐ๋Š”๋ฐ ๋งˆ์ง€๋ง‰ ํ‰๊ท ๊ฐ’ ์ฒ˜๋ฆฌ์—์„œ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด sum๊ณผ list๋ฅผ ์ถ”๊ฐ€ํ•˜๋‹ค ๋ณด๋‹ˆ ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค.
  • ๋‘๋ฒˆ์งธ 2์ค‘ for๋ฌธ์€ ๊ทธ๋ฃน๋“ค์„ ๊ฐ ํ‰๊ท ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค.
    • ์›๋ž˜์ฝ”๋“œ๋Š” ๋ชจ๋“  ๋‚˜๋ผ๋ฅผ 1~G_count ๊นŒ์ง€ ๋Š˜๋ ค๊ฐ€๋ฉฐ ๋ฐฉ๋ฌธํ•˜๊ณ  ํ‰๊ท ๊ฐ’ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š”๋ฐ, ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์ ธ์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๊ณ , ์œ„์™€ ๊ฐ™์ด ์ดํ•ฉ๊ณผ list๋ฅผ ๊ฐ€๋กํ•˜๋„๋ก ๋ณ€๊ฒฝํ–ˆ๋‹ค.
    • ๋‚˜๋ผ์˜ ์ขŒํ‘œ 2๊ฐœ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์—ˆ์œผ๋ฏ€๋กœ 2์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉฐ ํ‰๊ท ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

๋‹ค์Œ์€ BFS ํ•จ์ˆ˜์ด๋‹ค.

int BFS(vector<vector<int>>& A, int& i, int& j, vector<vector<int>>& group,
			int& G_count, int& L, int& R, vector<vector<int>>& same_G_list) {
	int sum = 0;
	queue<vector<int>> q;
	vector<int> pair(2);
	pair[0] = i;
	pair[1] = j;

	q.push(pair);
	group[i][j] = G_count;

	while (!q.empty()) {
		pair = q.front();
		int now_i = pair[0];
		int now_j = pair[1];
		sum += A[now_i][now_j];
		same_G_list[G_count].push_back(now_i);
		same_G_list[G_count].push_back(now_j);
		//printf("pair[0] : %d    pair[1] : %d     G_count : %d\n", pair[0], pair[1], G_count);

		// left
		int next_i = now_i;
		int next_j = now_j - 1;
		if (next_j >= 0 && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		//top
		next_i = now_i - 1;
		next_j = now_j;
		if (next_i >= 0 && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		//right
		next_i = now_i;
		next_j = now_j + 1;
		if (next_j < A.size() && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		//bottom
		next_i = now_i + 1;
		next_j = now_j;
		if (next_i < A.size() && group[next_i][next_j] == 0 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) >= L 
					&& abs(A[now_i][now_j] - A[next_i][next_j]) <= R) {
			pair[0] = next_i;
			pair[1] = next_j;
			q.push(pair);
			group[next_i][next_j] = G_count;
		}

		q.pop();
	}

	return sum;
}
  •  BFS ๋ถ€๋ถ„์„ main() ์•ˆ์— ๊ตฌํ˜„ํ•˜๋ฉด ๋„ˆ๋ฌด ๊ผฌ์ผ ๊ฒƒ ๊ฐ™์•„ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค.
    (ํ•„์š”ํ•œ ์ธ์ž๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋ถ€ ๋„ฃ๊ฒŒ๋˜์„œ ์ข€ ์ง€์ €๋ถ„ํ•ด์กŒ๋‹ค...)
  • ํ˜„์žฌ ์ขŒํ‘œ์˜ ๊ทธ๋ฃน ์ดํ•ฉ์„ ๋”ํ•ด์ฃผ๊ณ , ๊ทธ๋ฃน ๋ชฉ๋ก์—๋„ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ, ์œ„์ชฝ, ์˜ค๋ฅธ์ชฝ, ์•„๋ž˜์ชฝ์„ ํƒ์ƒ‰ํ•œ๋‹ค. 
    • vector ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    •  group[][] == 0  : ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋˜ (๊ทธ๋ฃน์ด ์—†๋Š”) ๋‚˜๋ผ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
    • L ~ R ๋ฒ”์œ„ ๋‚ด์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
    • ์ขŒํ‘œ๋ฅผ ํ์— ๋„ฃ๊ณ , ๊ทธ๋ฃน๋„ ํ‘œ์‹œํ•œ๋‹ค.

์˜ˆ์ œ 4, 5 ํ’€์ด ๊ณผ์ •...!


๐Ÿ”ธ end ๐Ÿ”ธ

  • BFS๋ฅผ ์ ์šฉํ•ด๋ณด๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ๋ผˆ๋Œ€๋ฅผ ์ฝ”๋”ฉํ•˜๋Š” ๊ฑด ์˜ค๋ž˜๊ฑธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค.
  • ์ž์ž˜ํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋งŽ์•˜๊ณ , ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๊ฒŒ ์กฐ๊ธˆ ํž˜๋“ค์—ˆ๋‹ค.
  • ํ’€์ด ๊ณผ์ • ์‚ฌ์ง„์ฒ˜๋Ÿผ ์†์œผ๋กœ ํ’€์–ด๋ณด๋‹ˆ ๋ฌธ์ œ ์ดํ•ด๊ฐ€ ๋น ๋ฅด๊ฒŒ ๋˜์—ˆ๋‹ค.
728x90