60 lines
1.1 KiB
C++
60 lines
1.1 KiB
C++
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
|
||
|
int main() {
|
||
|
ios::sync_with_stdio(0);
|
||
|
cin.tie(0);
|
||
|
|
||
|
string line;
|
||
|
vector<vector<pair<int, set<int>>>> m;
|
||
|
int h = 0;
|
||
|
while (getline(cin, line) && !line.empty()) {
|
||
|
istringstream l(line);
|
||
|
char c;
|
||
|
vector<pair<int, set<int>>> mm;
|
||
|
while (l >> c) {
|
||
|
set<int> s;
|
||
|
if (c == '9') {
|
||
|
s.insert(h);
|
||
|
}
|
||
|
mm.push_back({c - '0', s});
|
||
|
h++;
|
||
|
}
|
||
|
m.push_back(mm);
|
||
|
}
|
||
|
|
||
|
int xm = m.size(), ym = m[0].size();
|
||
|
|
||
|
for (int i = 9; i > 0; i--) {
|
||
|
for (int x = 0; x < xm; x++) {
|
||
|
for (int y = 0; y < ym; y++) {
|
||
|
auto &[n, c] = m[x][y];
|
||
|
if (n != i) {
|
||
|
continue;
|
||
|
}
|
||
|
vector<pair<int, int>> xxyy = {
|
||
|
{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}};
|
||
|
for (auto &[xx, yy] : xxyy) {
|
||
|
if (xx < 0 || yy < 0 || xx >= xm || yy >= ym) {
|
||
|
continue;
|
||
|
}
|
||
|
auto &[nn, cc] = m[xx][yy];
|
||
|
if (nn == n - 1) {
|
||
|
cc.insert(c.begin(), c.end());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int sum = 0;
|
||
|
for (auto &mm : m) {
|
||
|
for (auto &[n, c] : mm) {
|
||
|
if (n == 0) {
|
||
|
sum += c.size();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
cout << sum << '\n';
|
||
|
}
|