-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADASEA - Ada and Island.cc
55 lines (52 loc) · 1.23 KB
/
ADASEA - Ada and Island.cc
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
#include "stdio.h"
#include <iostream>
#include <vector>
using namespace std;
const int nx[4] = {-1, 0, 1, 0};
const int ny[4] = {0, 1, 0, -1};
int w, h;
string m[1010];
vector<int> islands;
int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
int floodfill(const int y, const int x) {
int neighbours = 0;
if (x < 0 || y < 0 || x >= w || y >= h || m[y][x] != '#')
return 0;
m[y][x] = '.';
for (int i = 0; i < 4; i++)
neighbours += floodfill(y + ny[i], x + nx[i]);
return 1 + neighbours;
}//floodfill
int main() {
int T;
int div;
int msize;
long long int res, sum;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &h, &w);
getline(cin, m[0]);
for (int i = 0; i < h; i++)
getline(cin, m[i]);
sum = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (m[i][j] == '#') {
res = floodfill(i, j);
sum += res * res;
}//if
}//for
}//for
msize = w * h;
div = gcd(sum, msize);
sum /= div;
msize /= div;
if (msize == 1)
printf("%lld\n", sum);
else
printf("%lld / %d\n", sum, msize);
}//while
return 0;
}//main