백준
백준 소스코드 [C++] 3184 양
Hani_Levenshtein
2020. 8. 25. 07:42
https://www.acmicpc.net/problem/3184
3184번: 양
문제 미키의 뒷마당에는 특정 수의 양이 있다. 그가 푹 잠든 사이에 배고픈 늑대는 마당에 들어와 양을 공격했다. 마당은 행과 열로 이루어진 직사각형 모양이다. 글자 '.' (점)은 빈 필드를 의미�
www.acmicpc.net
백준 소스코드 [C++] 3184 양
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
int totalwolf=0,totalsheep=0,wolf,sheep;
char arr[252][252];
bool check[252][252];
queue <pair<int, int>> q;
pair<int, int> pp, p[4] = { {1,0},{-1,0},{0,1},{0,-1} };
void bfs() {
wolf = sheep = 0;
while (q.empty() != true) {
pp = q.front();
q.pop();
if (arr[pp.first][pp.second] == 'v') wolf++;
else if (arr[pp.first][pp.second] == 'o') sheep++;
for (int i = 0;i < 4;i++) {
if (arr[pp.first + p[i].first][pp.second + p[i].second] != '#'
&& check[pp.first + p[i].first][pp.second + p[i].second] == true) {
q.push(make_pair(pp.first + p[i].first, pp.second + p[i].second));
check[pp.first + p[i].first][pp.second + p[i].second] = false;
}
}
}
if (wolf < sheep) totalsheep = totalsheep + sheep;
else totalwolf = totalwolf + wolf;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int x,y;
cin >> y >> x;
for (int j = 1;j <= y;j++)
for (int i = 1;i <= x;i++) {
cin >> arr[j][i];
check[j][i]=true;
}
for (int j = 1;j <= y;j++)
for (int i = 1;i <= x;i++) {
if (arr[j][i] != '#' && check[j][i] == true) {
q.push(make_pair(j, i));
check[j][i] = false;
bfs();
}
}
cout << totalsheep << " " << totalwolf;
return 0;
}