-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path10336.cpp
More file actions
executable file
·99 lines (83 loc) · 1.72 KB
/
10336.cpp
File metadata and controls
executable file
·99 lines (83 loc) · 1.72 KB
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
/* Problem: Rank the Languages UVa 10336
Programmer: Md. Mahmud Ahsan
Description: Graph + DFS
Compiled: Visual C++ 7.0
Date: 02-01-06
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MX = 10000;
struct Type{
char id;
int value;
}id[MX];
int dr[] = {0, -1, 0, 1};
int dc[] = {-1, 0, 1, 0};
char graph[MX][MX];
int nc, h, w;
inline bool comp(Type a, Type b){
if (a.value != b.value)
return a.value > b.value;
return a.id < b.id;
}
void init(){
int k = h * w;
for (int i = 0; i < k; ++i)
id[i].value = 0;
}
void setRank(char c, int r){
for (int i = 0; i < nc; ++i){
if (id[i].id == c){
id[i].value += r;
return;
}
}
id[nc].id = c;
id[nc].value = r;
++nc;
}
void dfsVisit(int a, int b){
int i, j, r, c;
char tempC = graph[a][b];
graph[a][b] = 'G';
for (i = 0; i < 4; ++i){
r = dr[i] + a;
c = dc[i] + b;
if (r >= 0 && r < h && c >= 0 && c < w && graph[r][c] == tempC)
dfsVisit(r, c);
}
graph[a][b] = 'B';
}
void dfs(){
int i, j;
for (i = 0; i < h; ++i){
for (j = 0; j < w; ++j){
if (graph[i][j] != 'G' && graph[i][j] != 'B'){
setRank(graph[i][j], 1);
dfsVisit(i, j);
}
}
}
}
int main(){
//freopen("input.txt", "r", stdin);
int test, i, cases = 0;
char temp[100];
cin >> test;
while(test--){
cin >> h >> w;
cin.getline(temp, sizeof(temp)); // eat new line
nc = 0;
init();
for (i = 0; i < h; ++i)
cin.getline(graph[i], sizeof(graph[i]));
dfs();
sort(id, id+nc, comp);
cout << "World #" << ++cases << endl;
for (i = 0; i < nc; ++i)
cout << id[i].id << ": " << id[i].value << endl;
}
return 0;
}