-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva10946.cpp
More file actions
48 lines (38 loc) · 1.12 KB
/
Copy pathuva10946.cpp
File metadata and controls
48 lines (38 loc) · 1.12 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
/*
* Contest : UVA
* Problem : 657 - You want what filled?
* Link : https://vjudge.net/problem/UVA-10946
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
string grid[51];
int n, m, caseNum = 0;
void dfs(int i, int j, char c, int &cnt) {
grid[i][j] = '.';
cnt++;
for (auto [dx, dy] : {pair{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
int x = i + dx, y = j + dy;
if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == c) dfs(x, y, c, cnt);
}
}
int main() {
fastio
while (cin >> n >> m, n && m) {
for (int i = 0; i < n; ++i) cin >> grid[i];
vector<pair<char, int>> arr;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (grid[i][j] != '.') {
auto &[ch, cnt] = arr.emplace_back(grid[i][j], 0);
dfs(i, j, ch, cnt);
}
sort(arr.begin(), arr.end(), [](auto &a, auto &b) {
return a.second != b.second ? a.second > b.second : a.first < b.first;
});
cout << "Problem " << ++caseNum << ":\n";
for (auto &[ch, len] : arr) cout << ch << ' ' << len << '\n';
}
return 0;
}