-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabship.cpp
90 lines (87 loc) · 2.88 KB
/
abship.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
83
84
85
86
87
88
89
90
//
// Created by Aaron Zhu on 2021-08-03.
//
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define boost() cin.tie(0); cin.sync_with_stdio(0)
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
using namespace std;
struct coord{int x, y;};
#define MAX 501
char grid[MAX][MAX];
int ships[MAX][MAX];
int counter[MAX * MAX / 2];
int original[MAX * MAX / 2];
int r, c, s;
int oC, cC;
int n = 1;
double total;
queue<coord> q;
int moves[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int main() {
boost();
cin >> r >> c >> s;
for (int i = 0; i < r; i += 1) for (int j = 0; j < c; j += 1) cin >> grid[i][j];
for (int i = 0; i < r; i += 1)
for (int j = 0; j < c; j += 1) {
if (grid[i][j] == 'X' && !ships[i][j]) {
q.push((coord) {i, j});
ships[i][j] = n;
while (!q.empty()) {
coord tmp = q.front(); q.pop();
int a = tmp.x;
int b = tmp.y;
for (int k = 0; k < 4; k += 1) {
int x = moves[k][0] + a;
int y = moves[k][1] + b;
if (0 <= x && x < r && 0 <= y && y < c && grid[x][y] == 'X' && !ships[x][y]) {
ships[x][y] = n;
q.push((coord) {x, y});
}
}
}
n += 1;
}
}
// create original window
for (int a = 0; a < s; a += 1) {
for (int b = 0; b < s; b += 1) {
if (ships[a][b] != 0) {
original[ships[a][b]] += 1;
if (original[ships[a][b]] == 1) oC += 1;
}
}
}
for (int i = s - 1; i < r; i += 1) {
copy(begin(original), end(original), begin(counter));
cC = oC;
total += cC;
for (int j = s; j < c; j += 1) {
for (int k = i - s + 1; k <= i; k += 1) {
if (ships[k][j - s] != 0) {
counter[ships[k][j - s]] -= 1; // remove
if (counter[ships[k][j - s]] == 0) cC -= 1;
}
if (ships[k][j] != 0) {
counter[ships[k][j]] += 1; // add
if (counter[ships[k][j]] == 1) cC += 1;
}
}
total += cC;
}
for (int j = 0; j < s; j += 1) {
if (ships[i - s + 1][j] != 0) {
original[ships[i - s + 1][j]] -= 1;
if (original[ships[i - s + 1][j]] == 0) oC -= 1;
}
if (ships[i + 1][j] != 0) {
original[ships[i + 1][j]] += 1;
if (original[ships[i + 1][j]] == 1) oC += 1;
}
}
}
cout << setprecision(8) << total / (double) ((r - s + 1) * (c - s + 1)) << "\n";
return 0;
}