-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10336.cpp
96 lines (83 loc) · 2.09 KB
/
10336.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
91
92
93
94
95
96
#include <cstdio>
#include <cstring>
using namespace std;
char map[1002][1002], c;
int cnt[26];
static inline int cmp(int x, int y) { return cnt[x] == cnt[y] ? y - x : cnt[x] - cnt[y]; }
void sortL(int *list, int len)
{
int i = 0;
int j = len - 1;
int tmpS, pivot = list[len / 2];
if (len < 2) return;
while (true) {
while (cmp(list[i], pivot) > 0)
++i;
while (cmp(list[j], pivot) < 0)
--j;
if (i >= j) break;
tmpS = list[i];
list[i] = list[j];
list[j] = tmpS;
}
sortL(list, i);
sortL(list + i, len - i);
}
void explore(int x, int y)
{
if (map[x + 1][y] == c) {
map[x + 1][y] = -1;
explore(x + 1, y);
}
if (map[x][y + 1] == c) {
map[x][y + 1] = -1;
explore(x, y + 1);
}
if (map[x - 1][y] == c) {
map[x - 1][y] = -1;
explore(x - 1, y);
}
if (map[x][y - 1] == c) {
map[x][y - 1] = -1;
explore(x, y - 1);
}
}
int main(void)
{
int times, index, table[128], list[26], len, i, j, m, n;
for (i = 0; i < 26; ++i)
table['a' + i] = i;
for (j = 0; j < 1002; ++j) {
map[0][j] = -1;
map[j][0] = -1;
}
index = 0;
scanf("%d", ×);
while (times--) {
memset(cnt, 0, sizeof(cnt));
scanf("%d %d", &n, &m);
for (j = 1; j < m + 2; ++j)
map[n + 1][j] = -1;
for (i = 1; i <= n; ++i) {
scanf("%s", &map[i][1]);
map[i][0] = -1;
map[i][m + 1] = -1;
}
printf("World #%d\n", ++index);
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j)
if (map[i][j] != -1) {
c = map[i][j];
map[i][j] = -1;
cnt[table[c]]++;
explore(i, j);
}
len = 0;
for (i = 0; i < 26; ++i)
if (cnt[i]) list[len++] = i;
sortL(list, len);
for (i = 0; i < len; ++i)
printf("%c: %d\n", list[i] + 'a', cnt[list[i]]);
}
return 0;
}