๐Ÿ™‡โ€โ™€๏ธ[Silver II] ์„ฌ์˜ ๊ฐœ์ˆ˜ - 4963

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2044 KB, ์‹œ๊ฐ„: 0 ms

๋ถ„๋ฅ˜

๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰, ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 5์ผ 23:40:11

๋ฌธ์ œ ์„ค๋ช…

์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ์„ฌ๊ณผ ๋ฐ”๋‹ค ์ง€๋„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์„ฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•œ ์ •์‚ฌ๊ฐํ˜•๊ณผ ๊ฐ€๋กœ, ์„ธ๋กœ ๋˜๋Š” ๋Œ€๊ฐ์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์‚ฌ๊ฐํ˜•์€ ๊ฑธ์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์‚ฌ๊ฐํ˜•์ด๋‹ค.

๋‘ ์ •์‚ฌ๊ฐํ˜•์ด ๊ฐ™์€ ์„ฌ์— ์žˆ์œผ๋ ค๋ฉด, ํ•œ ์ •์‚ฌ๊ฐํ˜•์—์„œ ๋‹ค๋ฅธ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๊ฑธ์–ด์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ง€๋„๋Š” ๋ฐ”๋‹ค๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ์œผ๋ฉฐ, ์ง€๋„ ๋ฐ–์œผ๋กœ ๋‚˜๊ฐˆ ์ˆ˜ ์—†๋‹ค.

์ž…๋ ฅ

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

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ h๊ฐœ ์ค„์—๋Š” ์ง€๋„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 1์€ ๋•…, 0์€ ๋ฐ”๋‹ค์ด๋‹ค.

์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰ ์ค„์—๋Š” 0์ด ๋‘ ๊ฐœ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, ์„ฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

BFS๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

๋น„์Šทํ•œ ์œ ํ˜•์ด ๋งŽ์•„์„œ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋Œ€๊ฐ์„ ๊นŒ์ง€ ๊ณ ๋ คํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ dy, dx๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์—ˆ๋‹ค.

int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };
int dx[] = { 1, 0, -1, 0, 1, -1, 1, -1 };

BFS๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

int w = -1, h = -1, res = 0;
int board[51][51];
int discovered[51][51];

int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };
int dx[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
void BFS(pair<int, int> here)
{
	if (board[here.first][here.second] == 0)
		return;

	queue<pair<int, int>> q;
	q.push(here);

	if (board[here.first][here.second] == 1 && discovered[here.first][here.second] == 0)
		res++;
	discovered[here.first][here.second] = 1;

	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

		for (int i = 0; i < 8; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > h || nx > w)
				continue;
			if (discovered[ny][nx] == 1)
				continue;
			if (board[ny][nx] == 0)
				continue;

			q.push({ ny, nx });
			discovered[ny][nx] = 1;
		}
	}
}

์„ฌ์—์„œ ๋ฐ”๋‹ค๋Š” ๊ฑด๋„ˆ์•ผํ•˜๋‹ˆ๊นŒ BFS๋ฅผ ํ•  ๋–„ ๋ฐ”๋‹ค๋ฉด ๋ฐ”๋กœ ๋๋ƒˆ๋‹ค.

๋˜ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ• ๋•Œ๋„ ์ฒซ ๋ฐœ๊ฒฌ์ด๋ฉด์„œ ์„ฌ์ด์—ฌ์•ผ res๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค.

solveํ•จ์ˆ˜๋Š” ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค.

void solve()
{
	while (true)
	{
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		for (int i = 1; i <= h; ++i)
		{
			for (int j = 1; j <= w; ++j)
			{
				cin >> board[i][j];
			}
		}

		::memset(discovered, 0, sizeof(discovered));
		res = 0;
		for (int i = 1; i <= h; ++i)
		{
			for (int j = 1; j <= w; ++j)
			{
				BFS({ i, j });
			}
		}

		cout << res << '\n';
	}
}

w, h๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ณ  board๋ฅผ ๊ทธ๋ฆฐ ๋‹ค์Œ,
discovered๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

board๋ฅผ ๋ชจ๋‘ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์œ„์น˜์— BFS๋ฅผ ๋‹ค ๋Œ๋ฆฐ๋‹ค.

๊ทธ ํ›„ res๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ด๋‹ค.

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>

using namespace std;

int w = -1, h = -1, res = 0;
int board[51][51];
int discovered[51][51];

int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };
int dx[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
void BFS(pair<int, int> here)
{
	if (board[here.first][here.second] == 0)
		return;

	queue<pair<int, int>> q;
	q.push(here);

	if (board[here.first][here.second] == 1 && discovered[here.first][here.second] == 0)
		res++;
	discovered[here.first][here.second] = 1;

	while (q.empty() == false)
	{
		here = q.front();
		q.pop();

		for (int i = 0; i < 8; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > h || nx > w)
				continue;
			if (discovered[ny][nx] == 1)
				continue;
			if (board[ny][nx] == 0)
				continue;

			q.push({ ny, nx });
			discovered[ny][nx] = 1;
		}
	}
	
}

void solve()
{
	while (true)
	{
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		for (int i = 1; i <= h; ++i)
		{
			for (int j = 1; j <= w; ++j)
			{
				cin >> board[i][j];
			}
		}

		::memset(discovered, 0, sizeof(discovered));
		res = 0;
		for (int i = 1; i <= h; ++i)
		{
			for (int j = 1; j <= w; ++j)
			{
				BFS({ i, j });
			}
		}

		cout << res << '\n';
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: