-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3187.cpp
More file actions
95 lines (87 loc) · 1.39 KB
/
3187.cpp
File metadata and controls
95 lines (87 loc) · 1.39 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 3187. 양치기 꿍
// 2019.05.21
// BFS
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
char map[251][251];
bool visit[251][251];
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
int r, c;
int w = 0;
int s = 0;
// 양과 늑대의 수를 반환하는 BFS
pair<int, int> BFS(int x, int y)
{
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
// 범위를 벗어나거나 울타리
if (xx < 0 || yy < 0 || xx >= r || yy >= c || map[xx][yy] == '#')
{
continue;
}
if (!visit[xx][yy])
{
visit[xx][yy] = true;
if (map[xx][yy] == 'v')
{
w++;
}
else if (map[xx][yy] == 'k')
{
s++;
}
BFS(xx, yy);
}
}
return { w,s };
}
int main()
{
int wolves = 0;
int sheep = 0;
cin >> r >> c;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> map[i][j];
if (map[i][j] == 'v')
{
wolves++;
}
else if (map[i][j] == 'k')
{
sheep++;
}
}
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
w = 0;
s = 0;
if (!visit[i][j])
{
visit[i][j] = true;
int first = BFS(i, j).first; //늑대의 수
int second = BFS(i, j).second; // 양의 수
if (first >= second)// 늑대의 승리
{
sheep -= second;
}
else// 양의 승리
{
wolves -= first;
}
}
}
}
cout << sheep << " " << wolves << endl;
return 0;
}