-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path10336 - Rank the Languages.cpp
64 lines (64 loc) · 1.27 KB
/
10336 - Rank the Languages.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
/**
UVa 10336 - Rank the Languages
by Rico Tiongson
Submitted: September 27, 2013
Accepted 0.019s C++
O(n^2) time
*/
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int n, tc;
int r, c, i, j;
char **s, S;
map<char,int> m;
vector<pair<char,int> > v;
int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};
bool cmp( const pair<char,int>& a, const pair<char,int>& b ){
if( a.second==b.second ) return a.first < b.first;
return a.second > b.second;
}
bool inRange( int x, int y ){
return x>=0 && x<r && y>=0 && y<c;
}
void dfs( int x, int y ){
if( !inRange(x,y) || s[x][y]!=S ) return;
s[x][y] = '.';
for( int k=0; k<4; ++k ){
dfs(x+dx[k],y+dy[k]);
}
}
int main(){
scanf("%d",&n);
while(tc<n){
scanf("%d %d",&r,&c);
s = new char*[r];
for( i=0; i<r; ++i ){
s[i] = new char[c+2];
scanf("%s",s[i]);
}
m.clear();
for( i=0; i<r; ++i ){
for( j=0; j<c; ++j ){
S = s[i][j];
if( S!='.' ){
++m[S];
dfs(i,j);
}
}
}
v.assign(m.begin(),m.end());
sort( v.begin(), v.end(), cmp );
printf("World #%d\n",++tc);
for( i=0; i<v.size(); ++i ){
printf("%c: %d\n",v[i].first,v[i].second);
}
for( i=0; i<r; ++i ){
delete[] s[i];
}
delete[] s;
}
}