-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy path10336.cc
49 lines (49 loc) · 1.06 KB
/
10336.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
// https://uva.onlinejudge.org/external/103/10336.pdf
#include<bits/stdc++.h>
using namespace std;
using ii=tuple<int,int>;
using vii=vector<ii>;
using vi=vector<int>;
using vs=vector<string>;
using vb=vector<bool>;
using vvb=vector<vb>;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t,n,m;
cin>>t;
for(int T=1;T<=t;T++){
cin>>n>>m;
vs a(n);
for(int i=0;i<n;i++)cin>>a[i];
vvb s(n,vb(m));
function<void(int,int)>dfs=[&](int y,int x){
s[y][x]=1;
int r[]={-1,0,1,0};
int c[]={0,1,0,-1};
for(int i=0;i<4;i++){
int u=y+r[i],v=x+c[i];
if(u>=0&&u<n&&v>=0&&v<m&&!s[u][v]&&a[u][v]==a[y][x])
dfs(u,v);
}
};
vi r(128);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(!s[i][j]){
dfs(i,j);
r[a[i][j]]++;
}
vii b;
for(int i=0;i<128;i++)
if(r[i])
b.push_back({-r[i],i});
sort(b.begin(),b.end());
cout<<"World #"<<T<<"\n";
for(ii x:b){
int c,d;
tie(c,d)=x;
cout<<char(d)<<": "<<-c<<"\n";
}
}
}