728x90
https://www.acmicpc.net/problem/3187
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<char>> arr(n, vector<char>(m));
vector<vector<bool>> visited(n, vector<bool>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
if (arr[i][j] == '#') visited[i][j] = true;
}
}
int surviveSheep = 0;
int surviveWolf = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j]) {
int wolf = 0;
int sheep = 0;
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (arr[cur.first][cur.second] == 'v') wolf++;
else if (arr[cur.first][cur.second] == 'k') sheep++;
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second]) {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second]) {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1]) {
q.push(make_pair(cur.first, cur.second - 1));
}
if (cur.second + 1 < m && !visited[cur.first][cur.second + 1]) {
q.push(make_pair(cur.first, cur.second + 1));
}
}
}
if (sheep > wolf) {
surviveSheep += sheep;
}
else {
surviveWolf += wolf;
}
}
}
}
cout << surviveSheep << " " << surviveWolf;
return 0;
}
728x90
'알고리즘 문제' 카테고리의 다른 글
[백준] 3671번 산업 스파이의 편지 (0) | 2020.02.20 |
---|---|
[백준] 11006번 남욱이의 닭장 (0) | 2020.02.18 |
[백준] 11724번 연결 요소의 개수 (0) | 2020.02.18 |
[백준] 17779번 게리맨더링 2 (0) | 2020.02.18 |
[백준] 17471번 게리맨더링 (0) | 2020.02.18 |