반응형

https://www.acmicpc.net/problem/3184

 

3184번: 양

첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.

www.acmicpc.net

 

울타리 내에 속한 영역마다 양과 늑대의 수를 구해주었습니다.

 
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#define MAX 250
using namespace std;

int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };

int r, c, aoc = 0, avc = 0;
string map[MAX];

void bfs(int x, int y) {
	int oc = map[x][y] == 'o';
	int vc = map[x][y] == 'v';
	queue<pair<int, int>> q;
	q.push({ x, y });
	map[x][y] = '#';
	while (!q.empty()) {
		int cx = q.front().first;
		int cy = q.front().second;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = cx + dx[i];
			int ny = cy + dy[i];
			if (nx < 0 || ny < 0 || nx >= r || ny >= c) continue;
			if (map[nx][ny] == '#') continue;
			q.push({ nx, ny });
			oc += map[nx][ny] == 'o';
			vc += map[nx][ny] == 'v';
			map[nx][ny] = '#';
		}
	}
	if (oc > vc) vc = 0;
	else oc = 0;
	aoc += oc;
	avc += vc;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	cin >> r >> c;
	for (int i = 0; i < r; i++) {
		cin >> map[i];
	}

	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			if (map[i][j] != '#') {
				bfs(i, j);
			}
		}
	}
	cout << aoc << " " << avc;
}
반응형

'Algorithm' 카테고리의 다른 글

백준 1259 : 팰린드롬수  (0) 2021.11.20
백준 3009 : 네 번째 점  (0) 2021.11.20
백준 1743 : 음식물 피하기  (0) 2021.11.20
백준 10451 : 순열 사이클  (0) 2021.11.20
백준 1037 : 약수  (0) 2021.11.19

+ Recent posts