안선생의 개발 블로그
C++ 11123 양 한마리... 양 두마리... 본문
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
|
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
char g[111][111] = { 0 };
bool visit[111][111] = { 0 };
int dx[4] = { 0,1,-1,0 };
int dy[4] = { 1,0,0,-1 };
int n, m, k;
void bfs(int x, int y)
{
queue <pair<int, int>> a;
a.push({ x, y });
visit[x][y] = 1;
while (!a.empty())
{
int xx = a.front().first;
int yy = a.front().second;
a.pop();
for (int i = 0; i < 4; i++)
{
int x1 = xx + dx[i];
int y1 = yy + dy[i];
if (x1 <0 || x1>=n || y1<0 || y1 >= m || visit[x1][y1]) continue;
if (g[x1][y1] == '#')
{
a.push({ x1, y1 });
visit[x1][y1] = 1;
}
}
}
}
int main()
{
cin >> k;
int cnt = 0;
while (k--)
{
cin >> n >> m;
cnt = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cin >> g[i][j];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (g[i][j] == '#' && !visit[i][j])
{
bfs(i, j);
cnt++;
}
}
}
cout << cnt << "\n";
memset(g, 0, sizeof(g));
memset(visit, 0, sizeof(visit));
}
return 0;
}
|
cs |
https://www.acmicpc.net/problem/11123
11123번: 양 한마리... 양 두마리...
얼마전에 나는 불면증에 시달렸지... 천장이 뚫어져라 뜬 눈으로 밤을 지새우곤 했었지. 그러던 어느 날 내 친구 광민이에게 나의 불면증에 대해 말했더니 이렇게 말하더군. "양이라도 세봐!"
www.acmicpc.net
'백준' 카테고리의 다른 글
C++ 1303 전쟁 - 전투 (0) | 2023.02.25 |
---|---|
C++ 16953 A ->B (0) | 2023.02.25 |
C++ 16948 데스나이트 (0) | 2023.02.24 |
C++ 3184 양 (1) | 2023.02.24 |
C++ 1697 숨바꼭질 (0) | 2023.02.23 |