-
Notifications
You must be signed in to change notification settings - Fork 1
/
10336.cpp
111 lines (103 loc) · 1.69 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma warning(disable : 4786)
#include<stdio.h>
#include<string.h>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int, int> pi;
struct store{
char c;
int cnt;
};
map<char, int> M;
int H, W, sz;
char s[1000][1000];
char str[10000], ch;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
struct store ans[25];
int isValid(char c)
{
return (c>='a' && c<='z');
}
void input(int m, int n)
{
sz = 0;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
while(scanf("%c", &ch))
{
if(isValid(ch))
{
s[i][j] = ch;
if(M.find(ch) == M.end())
{
M[ch] = sz++;
ans[M[ch]].c = ch;
}
break;
}
}
}
}
}
bool comp(struct store a, struct store b)
{
if(a.cnt == b.cnt)
return a.c<b.c;
return a.cnt>b.cnt;
}
void setzero()
{
for(int i=0; i<25; i++)
ans[i].cnt = 0;
}
void dfs(pi S, char ch)
{
pi u;
s[S.first][S.second] = '1';
for(int i=0; i<4; i++)
{
u.first = S.first + dr[i];
u.second = S.second + dc[i];
if(u.first<0 || u.second<0 || u.first>=H || u.second>=W)continue;
if(s[u.first][u.second] != '1' && s[u.first][u.second]==ans[M[ch]].c)
dfs(u, s[u.first][u.second]);
}
}
int main()
{
// freopen("10336.txt", "r", stdin);
int t, i, j;
pi S;
scanf("%d", &t);getchar();
for(i=1; i<=t; i++)
{
scanf("%d%d", &H, &W);getchar();
input(H, W);
setzero();
for(j=0; j<H; j++)
{
for(int k=0; k<W; k++)
{
if(s[j][k] != '1')
{
ans[M[s[j][k]]].cnt++;
S.first = j;
S.second = k;
dfs(S, s[j][k]);
}
}
}
sort(ans, ans+sz, comp);
printf("World #%d\n", i);
for(j=0; j<sz; j++)
printf("%c: %d\n", ans[j].c, ans[j].cnt);
M.clear();
}
return 0;
}