안선생의 개발 블로그

C++ 1303 전쟁 - 전투 본문

백준

C++ 1303 전쟁 - 전투

안선생 2023. 2. 25. 20:22
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 <vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<math.h>
using namespace std;
char g[111][111= { 0 };
bool visit[111][111= {0};
int dx[4= { 0,1,-1,0 };
int dy[4= { 1,0,0,-1 };
int n, m, w = 0, b = 0;
bool wc = 0, bc = 0;
void bfs(int x,int y)
{
    queue <pair<intint>> a;
    a.push({ x,y });
    visit[x][y] = 1;
    while (!a.empty())
    {
        int xx = a.front().first;
        int yy = a.front().second;
 
        a.pop();
        for (int i = 0; i < 4; i++)
        {
            int x1 = xx + dx[i];
            int y1 = yy + dy[i];
 
            if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= m || visit[x1][y1]) continue;
 
            if (g[x1][y1] == 'W' && wc)
            {
                a.push({ x1, y1 });
                visit[x1][y1] = 1;
                w++;
            }
            if (g[x1][y1] == 'B' && bc)
            {
                a.push({ x1, y1 });
                visit[x1][y1] = 1;
                b++;
            }
        }
     }
    
 
}
 
int main()
{
    int white = 0, blue = 0;
    
    cin >> m >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> g[i][j];
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            if (g[i][j] == 'W' && !visit[i][j])
            {
                wc = 1;
                bc = 0;
                w = 1;
                bfs(i, j);
                white += pow(w, 2);
            }
            if (g[i][j] == 'B' && !visit[i][j])
            {
                wc = 0;
                bc = 1;
                b  = 1;
                bfs(i, j);
                blue += pow(b, 2);
            }
        }
    cout << white << " " << blue;
    return 0;
}
 
cs

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

 

1303번: 전쟁 - 전투

첫째 줄에는 전쟁터의 가로 크기 N, 세로 크기 M(1 ≤ N, M ≤ 100)이 주어진다. 그 다음 두 번째 줄에서 M+1번째 줄에는 각각 (X, Y)에 있는 병사들의 옷색이 띄어쓰기 없이 주어진다. 모든 자리에는

www.acmicpc.net

 

'백준' 카테고리의 다른 글

C++ 14940 쉬운 최단거리  (0) 2023.02.25
C++ 13565 침투  (1) 2023.02.25
C++ 16953 A ->B  (0) 2023.02.25
C++ 11123 양 한마리... 양 두마리...  (0) 2023.02.24
C++ 16948 데스나이트  (0) 2023.02.24