-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·70 lines (56 loc) · 1.42 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·70 lines (56 loc) · 1.42 KB
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 <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> OFFSETS = {
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
};
void tag(const vector<vector<char>> &G, vector<vector<int>> &ID, int r, int c, int id)
{
queue<pair<int, int>> Q;
Q.push({r, c});
ID[r][c] = id;
while (!Q.empty())
{
auto [r, c] = Q.front();
Q.pop();
for (auto &[ro, co] : OFFSETS)
{
r += ro;
c += co;
if (0 <= r && r < G.size() && 0 <= c && c < G[0].size())
if ((G[r][c] == 'L' || G[r][c] == 'C') && ID[r][c] == 0)
{
ID[r][c] = id;
Q.push({r, c});
}
r -= ro;
c -= co;
}
}
}
int main()
{
int nrows, ncols;
cin >> nrows >> ncols;
vector<vector<char>> G(nrows, vector<char>(ncols, 'C'));
for (int r = 0; r < nrows; ++r)
for (int c = 0; c < ncols; ++c)
{
char tmp;
cin >> tmp;
if (tmp == 'W')
G[r][c] = 'W';
if (tmp == 'L')
G[r][c] = 'L';
}
vector<vector<int>> ID(nrows, vector<int>(ncols, 0));
int id = 1;
for (int r = 0; r < nrows; ++r)
for (int c = 0; c < ncols; ++c)
if (G[r][c] == 'L' && ID[r][c] == 0)
tag(G, ID, r, c, id++);
cout << id - 1 << endl;
return 0;
}