-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva657.cpp
133 lines (103 loc) · 2.28 KB
/
uva657.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
int W, H;
vector<string> image;
int dfs[55][55];
int dfs_dice[55][55];
vector<pair<int,int> > neighbours(int row, int col)
{
vector<pair<int,int> > rez;
rez.push_back(make_pair(row-1, col));
rez.push_back(make_pair(row, col-1));
rez.push_back(make_pair(row, col+1));
rez.push_back(make_pair(row+1, col));
return rez;
}
void flood_dice(int row, int col)
{
dfs_dice[row][col] = 1;
vector<pair<int,int> > rez = neighbours(row, col);
for(int i = 0; i < rez.size(); i++)
{
if(rez[i].first >= 0 && rez[i].second >= 0 && rez[i].first < H && rez[i].second < W)
{
if(dfs_dice[rez[i].first][rez[i].second] == -1 && image[rez[i].first][rez[i].second] == 'X')
flood_dice(rez[i].first, rez[i].second);
}
}
}
int flood_fill(int row, int col)
{
if(image[row][col] == '.')
return 0;
vector<pair<int,int> > rez = neighbours(row, col);
int dices = 0;
if(image[row][col] == 'X' && dfs_dice[row][col] == -1)
{
dices += 1;
flood_dice(row, col);
}
dfs[row][col] = 1;
for(int i = 0; i < rez.size(); i++)
{
if(rez[i].first >= 0 && rez[i].second >= 0 && rez[i].first < H && rez[i].second < W)
{
if(dfs[rez[i].first][rez[i].second] == -1)
{
if(image[rez[i].first][rez[i].second] != '.')
dices += flood_fill(rez[i].first, rez[i].second);
}
}
}
return dices; /* number of dices on connected component */
}
int main()
{
vector<int> dices;
int tc = 0;
while(true)
{
cin >> W >> H;
if(W == 0 && H == 0)
break;
tc++;
dices.clear();
image.clear();
image.resize(H);
for(int i = 0; i < image.size(); i++)
cin >> image[i];
memset(dfs, -1, sizeof(dfs));
memset(dfs_dice, -1, sizeof(dfs));
for(int row = 0; row < H; row++)
{
for(int col = 0; col < W; col++)
{
if(dfs[row][col] == -1)
{
int nr = flood_fill(row, col);
if(nr != 0)
dices.push_back(nr);
}
}
}
std::sort(dices.begin(), dices.end());
cout << "Throw " << tc << endl;
for(int i = 0; i < dices.size(); i++)
{
if(i != dices.size() - 1)
cout << dices[i] << " ";
else
cout << dices[i] << endl;
}
cout << endl;
}
return 0;
}