-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneps543.cpp
More file actions
73 lines (57 loc) · 1.27 KB
/
Copy pathneps543.cpp
File metadata and controls
73 lines (57 loc) · 1.27 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
71
72
73
/*
* Contest : Neps
* Problem : 543 - Batalha Naval
* Link : https://neps.academy/br/exercise/543
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
int n, m, k, l, c;
string line;
vector<vector<int>> grid;
vector<int> ships;
int vertical[] = {0, 0, 1, -1};
int horizontal[] = {1, -1, 0, 0};
void dfs(int lin, int col, int id) {
grid[lin][col] = id;
ships[id]++;
for (int i = 0; i < 4; i++) {
int x = lin + vertical[i],
y = col + horizontal[i];
if (x >= 1 && x <= n && y >= 1 && y <= m && grid[x][y] == 0) { dfs(x, y, id); }
}
}
int main() {
fastio
cin >> n >> m;
grid.assign(n + 1, vector<int>(m + 1, 0));
for (int i = 1, j = 1; i <= n; i++) {
cin >> line;
for (auto &ch : line) {
grid[i][j] = (ch == '.' ? -1 : 0);
j++;
}
j = 1;
}
ships.push_back(0);
for (int i = 1, id = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (grid[i][j] == 0) {
ships.push_back(0);
dfs(i, j, id);
id++;
}
}
}
int cnt = 0; cin >> k;
while (k--) {
cin >> l >> c;
if (grid[l][c] >= 1) {
ships[grid[l][c]]--;
if (ships[grid[l][c]] <= 0) { cnt++; }
}
}
cout << cnt << '\n';
return 0;
}