-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10336.cpp
More file actions
60 lines (57 loc) · 1.34 KB
/
Copy path10336.cpp
File metadata and controls
60 lines (57 loc) · 1.34 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
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
char a[1000][1000];
int N, M;
struct data {
int x;
char c;
};
void call(int i, int j, char ch) {
if (i < 0 || j < 0 || i >= N || j >= M) return;
if (a[i][j] != ch) return;
a[i][j] = '.';
for (int k = 0; k < 8; k++) {
call(i + dx[k], j + dy[k], ch);
}
return;
}
bool comp(data m, data n) {
if (n.x == m.x)
return m.c < n.c;
else
return m.x > n.x;
}
int main() {
data d[100];
int n, m, tc, t = 1;
cin >> tc;
while (tc--) {
scanf("%d%d", &n, &m);
getchar();
N = n;
M = m;
for (int i = 0; i < n; i++) gets(a[i]);
for (int i = 0; i < 35; i++) d[i].x = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] >= 'a' && a[i][j] <= 'z') {
int k = a[i][j] - 'a';
d[k].x += 1;
d[k].c = a[i][j];
call(i, j, a[i][j]);
}
}
}
sort(d, d + 35, comp);
printf("World #%d\n", t++);
for (int i = 0; i < 35; i++) {
if (d[i].x != 0) {
printf("%c: %d\n", d[i].c, d[i].x);
} else
break;
}
}
return 0;
}