Post

๐Ÿ’›V [๋ฐฑ์ค€ 10026/C++] ์ ๋ก์ƒ‰์•ฝ

BOJ ๋ฌธ์ œ ๋ณด๊ธฐ




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
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

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

char map[101][101];
bool visited[101][101];

void init(int n)
{
	memset(visited, 0, sizeof(visited));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (map[i][j] == 'G') map[i][j] = 'R';
		}
	}
}

int	bfs(int n)
{
	int cnt = 0;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (visited[i][j]) continue;

			visited[i][j] = 1;
			queue<pair<int, int>> q;
			q.push({ i, j });

			while (!q.empty())
			{
				int x = q.front().first;
				int y = q.front().second;
				q.pop();

				for (int i = 0; i < 4; i++)
				{
					int nx = x + dx[i];
					int ny = y + dy[i];

					if (nx < 0 || ny < 0 || nx >= n || ny >= n || map[x][y] != map[nx][ny] || visited[nx][ny]) continue;

					visited[nx][ny] = 1;
					q.push({ nx, ny });
				}
			}
			cnt++;
		}
	}
	
	return cnt;
}

int main()
{
	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			scanf("%1s", &map[i][j]);
		}
	}

	printf("%d ", bfs(n));
	init(n);
	printf("%d", bfs(n));

	return 0;
}


image

This post is licensed under CC BY 4.0 by the author.

ยฉ yejin. Some rights reserved.