๐Ÿ“ Coding Test Study/Algorithm Problem

[Baekjoon][C++] 2667๋ฒˆ ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ ๋ฌธ์ œ์„ค๋ช… ๋ฐ ์ฝ”๋“œ

ibelieveinme 2021. 4. 17. 01:13
728x90

โ–ท ๋ฌธ์ œ์„ค๋ช…

 

2667๋ฒˆ: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ

<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ง€๋„๊ฐ€ ์žˆ๋‹ค. 1์€ ์ง‘์ด ์žˆ๋Š” ๊ณณ์„, 0์€ ์ง‘์ด ์—†๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ด ์ง€๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ๊ฒฐ๋œ ์ง‘์˜ ๋ชจ์ž„์ธ ๋‹จ์ง€๋ฅผ ์ •์˜ํ•˜๊ณ , ๋‹จ์ง€์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋ ค ํ•œ๋‹ค. ์—ฌ

www.acmicpc.net

์ง‘์ด ์žˆ๋Š” ๊ณณ์€ 1, ์ง‘์ด ์—†๋Š” ๊ณณ์€ 0์œผ๋กœ ํ‘œํ˜„๋œ ์ง€๋„์ •๋ณด(์ •์‚ฌ๊ฐํ˜•)๋ฅผ ์ฃผ๊ณ ,

๋‹จ์ง€์˜ ์ˆ˜์™€ ๊ฐ ๋‹จ์ง€ ๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ

 

์กฐ๊ฑด) ์—ฐ๊ฒฐ๋œ ์ง‘๋“ค์€ ์ƒํ•˜์ขŒ์šฐ์— ์žˆ๋Š” ์ง‘์ด๋‹ค. ๋Œ€๊ฐ์„  X.

        ์ง€๋„์˜ ํฌ๊ธฐ N์€ 5<=N<=25 ๋ฒ”์œ„์˜ ์ˆ˜์ด๋‹ค.

 

โ–ท ๋ฌธ์ œํ’€์ด

[ ๋ณ€์ˆ˜์ •์˜ ]

๋ฌธ์ œ์—์„œ ์ง€๋„ ์ •๋ณด๋ฅผ N*Nํฌ๊ธฐ๋กœ ์คฌ์œผ๋ฏ€๋กœ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ์ž…๋ ฅ ๋ฐ›์•˜๊ณ , ๋ฐฉ๋ฌธ ๋ฐฐ์—ด ๋˜ํ•œ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

ํ•ด๋‹น ์ง€์ ์—์„œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋กœ direction[4][2] ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

 

[ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - DFS์ด์šฉ ]

1) ์ง€๋„์ •๋ณด๋Š” scanf๋กœ ๊ณต๋ฐฑ์—†๋Š” ์ˆซ์ž๋ฅผ ํ•œ์ค„์”ฉ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. scanf_s("%1d", &map[i][j])

2) ์ง€๋„์˜ (0, 0) ์ขŒํ‘œ๋ถ€ํ„ฐ ์ง‘์ด ์žˆ๋Š” ์ž๋ฆฌ์ด๊ณ  ๋ฐฉ๋ฌธํ•œ ์  ์—†๋Š” ๊ณณ์ด๋ฉด ์ง‘์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค.

3) ํ˜„์žฌ ์œ„์น˜(0, 0)์—์„œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์‚ดํŽด๋ณธ๋‹ค. ์ƒํ•˜์ขŒ์šฐ(๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ด 4๋ฒˆ)๋ฅผ ํƒ์ƒ‰ํ•  ๋• ์ง€๋„์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋งŒ ํƒ์ƒ‰ํ•œ๋‹ค.

if (dx >= 0 && dx < map_size && dy >= 0 && dy < map_size)

4) ํƒ์ƒ‰ํ•  ๋•Œ๋งˆ๋‹ค ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ์‹œํ•ด์ฃผ๊ณ (visited[x][y] = 1) ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ๋Š˜๋ ค์ค€๋‹ค(houseNum++).

5) ์ด๋™ํ•œ ์ƒ or ํ•˜ or ์ขŒ or ์šฐ ์ขŒํ‘œ ์œ„์น˜๊ฐ€ ์ง‘์ด ์•„๋‹ˆ๊ฑฐ๋‚˜ ๋ฐฉ๋ฌธํ•œ์ ์ด ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊นŠ์ด ํƒ์ƒ‰ํ•œ๋‹ค. SearchHouse(dx, dy)

6) ์ง‘์ด ์•„๋‹ˆ๊ฑฐ๋‚˜ ๋ฐฉ๋ฌธํ•œ์ ์ด ์žˆ์œผ๋ฉด returnํ•œ๋‹ค.(์ด์ „ ๋‹จ๊ณ„๋กœ ๋Œ์•„๊ฐ) if (map[x][y] == 0 || visited[x][y])

7) ๋Œ์•„์™€์„œ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฒกํ„ฐ๋ฐฐ์—ด(vector<int> houseGroup)์— ๋„ฃ์–ด์ค€๋‹ค.

7) ์—ฐ๊ฒฐ๋œ ๋‹จ์ง€ ๋‚ด์—์„œ DFS๋กœ ๋ชจ๋‘ ํƒ์ƒ‰ํ–ˆ์œผ๋ฉด ๋‹จ์ง€์˜ ์ˆ˜๋ฅผ 1 ๋Š˜๋ ค์ค€๋‹ค. houseGroupNum++

8) ๋งˆ์ง€๋ง‰ ์ง€๋„ ์ขŒํ‘œ๊นŒ์ง€ ํƒ์ƒ‰์„ ํ•˜๋ฉด ๋‹จ์ง€ ์ˆ˜์™€ ๊ฐ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ(sort() ํ•จ์ˆ˜ ์ด์šฉ)์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

โ–ท ์‹œ๊ฐ„๋ณต์žก๋„

์ง€๋„ ํฌ๊ธฐ๋งŒํผ 2์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๊ณ ,  ๊ฐ ์ž๋ฆฌ๋งˆ๋‹ค 4๋ฒˆ์”ฉ ํƒ์ƒ‰ํ•˜๋ฏ€๋กœ O(4N²) = O(N²)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

โ–ท ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 26;

void SearchHouse(int, int);

int map[MAX][MAX];
bool visited[MAX][MAX];

int map_size;
int direction[4][2] = { {0,-1}, {1,0}, {-1,0}, {0,1}, }; //์ƒํ•˜์ขŒ์šฐ
int houseGroupNum;	//๋‹จ์ง€์ˆ˜
int houseNum;		//๋‹จ์ง€๋‚ด์˜ ์ง‘ ์ˆ˜

int main() {
	//Input
	cin >> map_size;
	for (int i = 0; i < map_size; i++) {
		for (int j = 0; j < map_size; j++) {
			scanf_s("%1d", &map[i][j]); // ๊ณต๋ฐฑ์—†๋Š” ์ˆซ์ž๋ฅผ ํ•œ์ค„์”ฉ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฒ•
		}
	}

	//Search
	vector<int> houseGroup;
	for (int i = 0; i < map_size; i++) {
		for (int j = 0; j < map_size; j++) {
			if (map[i][j] == 1 && !visited[i][j]) {
				SearchHouse(i,j);
				houseGroup.push_back(houseNum);
				houseGroupNum++;
				houseNum = 0;
			}
		}
	}

	//Output
	cout << houseGroupNum << "\n";
	sort(houseGroup.begin(), houseGroup.end());
	for (int i = 0; i < houseGroup.size(); i++) {
		cout << houseGroup[i] << "\n";
	}
}

void SearchHouse(int x, int y) {
	if (map[x][y] == 0 || visited[x][y]) {
		return;
	}

	visited[x][y] = 1;
	houseNum++;

	for (int i = 0; i < 4; i++) {
		int dx = x + direction[i][0];
		int dy = y + direction[i][1];
		if (dx >= 0 && dx < map_size && dy >= 0 && dy < map_size) {
			SearchHouse(dx, dy);
		}
	}
}
728x90