-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask9b.cpp
More file actions
54 lines (50 loc) · 1.64 KB
/
task9b.cpp
File metadata and controls
54 lines (50 loc) · 1.64 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
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>> &caves, int curRow, int curCol, int numRow, int numCol, int curNum) {
caves[curRow][curCol] = curNum;
int horiz[4] = {1, -1, 0, 0};
int verti[4] = {0, 0, 1, -1};
int newRow, newCol;
for (int i = 0; i < 4; ++i) {
newRow = curRow + verti[i];
newCol = curCol + horiz[i];
if (newRow >= 0 && newRow < numRow && newCol >= 0 && newCol < numCol) {
if (caves[newRow][newCol] >= 0 && caves[newRow][newCol] != 9) {
dfs(caves, newRow, newCol, numRow, numCol, curNum);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
string line;
vector<vector<int>> caves;
while (getline(cin, line)) {
caves.push_back(vector<int> (line.size()));
for (int i = 0; i < line.size(); ++i) {
caves[caves.size() - 1][i] = line[i] - '0';
}
}
int numRow = caves.size(), numCol = caves[0].size();
int curNum = -1;
for (int i = 0; i < numRow; ++i) {
for (int j = 0; j < numCol; ++j) {
if (caves[i][j] >= 0 && caves[i][j] != 9) {
dfs(caves, i, j, numRow, numCol, curNum);
curNum--;
}
}
}
vector<int> ans(abs(curNum), 0);
for (int i = 0; i < numRow; ++i) {
for (int j = 0; j < numCol; ++j) {
if (caves[i][j] < 0) {
ans[abs(caves[i][j])]++;
}
}
}
sort(ans.begin(), ans.end());
int res = ans[ans.size() - 1] * ans[ans.size() - 2] * ans[ans.size() - 3];
cout << res;
return 0;
}