๐Ÿ“ Coding Test Study/Algorithm Problem

[Baekjoon][C++] 4963๋ฒˆ ์„ฌ์˜ ๊ฐœ์ˆ˜ ๋ฌธ์ œ์„ค๋ช… ๋ฐ ์ฝ”๋“œ

ibelieveinme 2021. 4. 17. 02:57
728x90

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

 

4963๋ฒˆ: ์„ฌ์˜ ๊ฐœ์ˆ˜

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ์ง€๋„์˜ ๋„ˆ๋น„ w์™€ ๋†’์ด h๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. w์™€ h๋Š” 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ h๊ฐœ ์ค„์—๋Š” ์ง€๋„

www.acmicpc.net

0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ์ง€๋„(1์€ ๋•…, 0์€ ๋ฐ”๋‹ค)์—์„œ ์„ฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ

์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์— ๋•…์ด ์žˆ๋‹ค๋ฉด ์„ฌ์œผ๋กœ ํŒ๋‹จ.

์ง€๋„์˜ ๊ฐ€๋กœ, ์„ธ๋กœ๋กœ 0 0์ด ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ์ง€๋„ ์ž…๋ ฅ๊ณผ ์ •๋‹ต ์ถœ๋ ฅ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

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

์ง€๋„์ขŒํ‘œ (0, 0)์—์„œ ๋ถ€ํ„ฐ ํ˜„์žฌ ์œ„์น˜๊ฐ€ ๋•…์ธ์ง€ ํŒ๋‹จํ•˜๊ณ  ๋•…์ด๋ฉด ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.

ํƒ์ƒ‰์€ ์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„ ์„ ์ฒดํฌํ•˜์—ฌ ์ง€๋„ ๋ฒ”์œ„๋‚ด์ด๊ณ  1๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ณณ๋งŒ ํƒ์ƒ‰ํ•œ๋‹ค.

์ขŒํ‘œ๋งŒ ์˜ฎ๊ฒจ์„œ ํƒ์ƒ‰์„ ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ DFS ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

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

2์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ง€๋„๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๊ณ , ํ˜„ ์ขŒํ‘œ์—์„œ ์ƒํ•˜์ขŒ์šฐ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฏ€๋กœ 

O(8N²) = O(N²)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

โ–ท ์ฝ”๋“œ

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

const int MAX = 50;

void SearchMap(int, int);

int width, height;		 //๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ์ง€๋„์˜ ๊ฐ€๋กœ์„ธ๋กœ ํฌ๊ธฐ
int map[MAX][MAX];		//์ง€๋„๋ณ€์ˆ˜
int visited[MAX][MAX];	//๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๋ณ€์ˆ˜
int direction[8][2] = { {0,-1}, {0,1}, {-1,0}, {1,0}, {-1,-1}, {-1,1}, {1,-1}, {1,1} }; //์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„ 

int main() {
	while(1) {
		cin >> width >> height;
		
		if (width == 0 && height == 0) break;		//0, 0 ์ด๋ฉด ๋งˆ์ง€๋ง‰ ๊ฐ’์ด๋ฏ€๋กœ ์ข…๋ฃŒ

		//์ง€๋„ ์ž…๋ ฅ๋ฐ›๊ธฐ
		for (int i = 0; i < height; i++) {			//ํ–‰
			for (int j = 0; j < width; j++) {		//์—ด
				cin >> map[i][j];
			}
		}

		//ํƒ์ƒ‰ gogo. ์žฌํƒ์ƒ‰ํ•  ๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”
		int count = 0;
		memset(visited, false, sizeof(visited));

		for (int i = 0; i < height; i++) {	//ํ–‰
			for (int j = 0; j < width; j++) {//์—ด
				if (map[i][j] && !visited[i][j]) { //ํ˜„์œ„์น˜๊ฐ€ ์„ฌ์ด๊ณ , ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†์œผ๋ฉด ํ˜„์œ„์น˜์—์„œ ํƒ์ƒ‰ ์‹œ์ž‘
					SearchMap(i, j);
					count += 1;
				}
			}
		}
		cout << count << "\n";
	} 
}

void SearchMap(int x, int y) {
	if (!map[x][y] || visited[x][y]) { //โ˜…ํƒ์ƒ‰ ํ•˜๋‹ค๊ฐ€ ์„ฌ์ด ์•„๋‹ˆ๊ฑฐ๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์„ ๋งŒ๋‚˜๋ฉด, ๋”์ด์ƒ ํƒ์ƒ‰ํ•  ํ•„์š” ์—†์œผ๋‹ˆ๊นŒ ๋ฐ”๋กœ ๋น ์ ธ๋‚˜๊ฐ€์ž
		return;
	}
	visited[x][y] = true;

	for (int i = 0; i < 8; i++) {		//์ƒํ•˜์ขŒ์šฐ๋Œ€๊ฐ์„  ํƒ์ƒ‰
		int dx = x + direction[i][0];	//x์ขŒํ‘œ
		int dy = y + direction[i][1];	//y์ขŒํ‘œ
		if (dx >= 0 && dx < height && dy >= 0 && dy < width) {// ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•œ ๊ฐ’์ด ์ง€๋„์˜ ๋ฒ”์œ„๋ฅผ ๋„˜๋Š”์ง€ ์ฒดํฌ
			SearchMap(dx, dy);
		}
	}
}

 

728x90