-
Notifications
You must be signed in to change notification settings - Fork 0
/
UvA 10336.cpp
86 lines (54 loc) · 1.22 KB
/
UvA 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
#include <bits/stdc++.h>
#define pb push_back
#define lp(i,n) for(int i=0;i<n;i++)
using namespace std;
char grid[1000][1000];
int visited[1000][1000];
bool wayToSort(pair<char,int> a, pair<char,int>b){
if(a.second>b.second) return true;
if(a.second==b.second && a.first<b.first) return true;
return false;
}
void dfs(int i,int j, char c,int n ,int m){
if(grid[i][j]!=c || visited[i][j]==1|| i<0|| j<0||i>=n ||j>=m){
return;
}
visited[i][j]=1;
dfs(i-1,j,c,n,m);
dfs(i+1,j,c,n,m);
dfs(i,j-1,c,n,m);
dfs(i,j+1,c,n,m);
}
int main ()
{
int t;
cin>>t;
int cnt=1;
while(t--){
int n,m;
cin>>n>>m;
string s;
lp(i,n){
cin>>s;
lp(j,m){
visited[i][j]=0;
grid[i][j]=s[j];
}
}
map<char,int> track;
lp(i,n){
lp(j,m){
if(visited[i][j]==0){
track[grid[i][j]]++;
dfs(i,j,grid[i][j],n,m);
}
}
}
vector<pair<char,int> > vec(track.begin(),track.end());
sort(vec.begin(),vec.end(),wayToSort);
printf("World #%d\n", cnt++);
for(auto x:vec){
printf("%c: %d\n", x.first, x.second);
}
}
}