문제 해설

 

 simulate 함수를 만들어, 같은 울타리 내에 있는 양과 늑대의 수 중 늑대의 수가 양의 수 이상인 경우 양의 수를 0으로 바꾼 후 리턴해주고, 양의 수가 늑대의 수보다 많을 경우 늑대의 수를 0으로 만들어 리턴해 주었습니다. 단순한 bfs 알고리즘을 통해 문제를 풀 수 있습니다.

 

 

코드

 

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
#include <iostream>
#include <string>
#include <queue>
using namespace std;
 
int r, c;
string a[250];
bool isVisit[250][250];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
pair<intint> simulate(int i, int j) {
 
    int s = 0, w = 0;
    queue<pair<int,int>> q;
    if (a[i][j] == 'o') s++;
    if (a[i][j] == 'v') w++;
    isVisit[i][j] = true;
    q.emplace(i, j);
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if (nx < 0 || nx >= r || ny < 0 || ny >= c || isVisit[nx][ny] == true|| a[nx][ny] == '#'continue;
 
            isVisit[nx][ny] = true;
 
            if (a[nx][ny] == 'o') s++;
            if (a[nx][ny] == 'v') w++;
 
            q.emplace(nx, ny);
        }
    }
 
    if (s > w) w = 0;
    else s = 0;
 
    return make_pair(s, w);
}
 
int main() {
 
    cin >> r >> c;
    memset(isVisit, falsesizeof(isVisit));
 
    for (int i = 0; i < r; i++) {
        cin >> a[i];
    }
    
    int sheep = 0, wolf = 0;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            
            if (isVisit[i][j] == truecontinue;
 
            if (a[i][j] == 'o' || a[i][j] == 'v') {
                auto n = simulate(i, j);
                sheep += n.first;
                wolf += n.second;
            }
        }
    }
    cout << sheep << ' ' << wolf << '\n';
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter
 

 

 
제출 결과
 

 

 

문제 출처

 

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

 

3184번: 양

문제 미키의 뒷마당에는 특정 수의 양이 있다. 그가 푹 잠든 사이에 배고픈 늑대는 마당에 들어와 양을 공격했다. 마당은 행과 열로 이루어진 직사각형 모양이다. 글자 '.' (점)은 빈 필드를 의미하며, 글자 '#'는 울타리를, 'o'는 양, 'v'는 늑대를 의미한다. 한 칸에서 수평, 수직만으로 이동하며 울타리를 지나지 않고 다른 칸으로 이동할 수 있다면, 두 칸은 같은 영역 안에 속해 있다고 한다. 마당에서 "탈출"할 수 있는 칸은 어떤 영역에도 속하지

www.acmicpc.net

 

+ Recent posts