본문 바로가기

Problem/BFS

[C/C++] BOJ 3184 :: 양

BOJ 3184 :: 양



문제 링크 : https://www.acmicpc.net/problem/3184







후압.. 설계한대로 후다다닥 쳐서 15분안에 실수없이 풀었다 매우 뿌듯ㅎ_ㅎ


물론 쉬운 문제이지만..!.... 그래도 완전 멍청이는 아니구나..!




나의 코드



Github : https://github.com/j2wooooo/Daliy_Algorithms/blob/master/Daliy_Algorithms/BOJ_3184/BOJ_3184.cpp




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
#include<iostream>
#include<queue>
using namespace std;
 
int R, C;
 
queue<pair<intint>> loc;
int total_sheep = 0;
int total_wolf = 0;
 
char map[251][251];
bool visited[251][251];
 
int dx[4= { 0,0,1,-1 };
int dy[4= { 1,-1,0,0 };
 
void bfs()
{
    int local_sheep = 0;
    int local_wolf = 0;
 
    while (!loc.empty())
    {
        int qsz = loc.size();
 
        while (qsz--)
        {
            int x = loc.front().first;
            int y = loc.front().second;
            loc.pop();
 
            for (int i = 0; i < 4; i++)
            {
                int mx = x + dx[i];
                int my = y + dy[i];
 
                if (mx < 0 || mx >= R || my < 0 || my > C) continue;
                if (visited[mx][my] || map[mx][my] == '#'continue;
                if (map[mx][my] == 'o') local_sheep++;
                if (map[mx][my] == 'v') local_wolf++;
                visited[mx][my] = true;
                loc.push(make_pair(mx, my));
            }
        }
    }
 
    if (local_sheep > local_wolf) total_wolf -= local_wolf;
    else total_sheep -= local_sheep;
 
    return;
}
 
int main(void)
{
    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] == 'o') total_sheep++;
            else if (map[i][j] == 'v') total_wolf++;
        }
    }
 
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
        {
            if (map[i][j] != '#' && !visited[i][j])
            {
                loc.push(make_pair(i, j));
                bfs();
            }
        }
    }
 
    cout << total_sheep << ' ' << total_wolf;
 
    return 0;
}
cs