안선생의 개발 블로그
C++ 3184 양 본문
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
|
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
using namespace std;
char g[501][501] = {0};
bool visit[501][501] = {0};
int dx[4] = { 0,1,-1,0 };
int dy[4] = { 1,0,0,-1 };
int n, m,k;
int wolf = 0;
int sheep = 0;
void bfs(int x,int y)
{
queue <pair<int,int>> a;
a.push({ x, y });
visit[x][y] = 1;
if (g[x][y] == 'v')
wolf++;
else if (g[x][y] == 'o')
sheep++;
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]!= '#')
{
if (g[x1][y1] == 'v')
wolf++;
else if (g[x1][y1] == 'o')
sheep++;
a.push({ x1, y1 });
visit[x1][y1] = 1;
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cin >> g[i][j];
}
int w = 0, s = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (g[i][j] != '#' && !visit[i][j])
{
wolf = sheep = 0;
bfs(i, j);
if (wolf >= sheep)
sheep = 0;
else
wolf = 0;
w += wolf;
s += sheep;
}
}
}
cout << s << " " << w;
return 0;
}
|
cs |
https://www.acmicpc.net/problem/3184
3184번: 양
첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.
www.acmicpc.net
'백준' 카테고리의 다른 글
C++ 11123 양 한마리... 양 두마리... (0) | 2023.02.24 |
---|---|
C++ 16948 데스나이트 (0) | 2023.02.24 |
C++ 1697 숨바꼭질 (0) | 2023.02.23 |
C++ 7576 토마토 (0) | 2023.02.18 |
C++ 7652 나이트의 이동 (0) | 2023.02.18 |