Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<int>> field;
- vector<vector<bool>> used;
- int n, m;
- bool ok(int x, int y) {
- return (0 <= x && x < n && 0 <= y && y < m);
- }
- vector<pair<int, int>> steps{{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
- bool dfs(int x, int y) {
- used[x][y] = true;
- bool res = true;
- for (const auto& step :steps) {
- int nx = x + step.first, ny = y + step.second;
- if (ok(nx, ny)) {
- if (field[nx][ny] < field[x][y]) {
- res = false;
- } else if (!used[nx][ny] && field[nx][ny] == field[x][y]) {
- res = (dfs(nx, ny) && res);
- }
- }
- }
- return res;
- }
- int main() {
- cin >> n >> m;
- field.assign(n, vector<int>(m));
- for (auto& row : field) {
- for (auto& elem : row) cin >> elem;
- }
- used.assign(n, vector<bool>(m, false));
- int cnt = 0;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- if (!used[i][j] && dfs(i, j)) ++cnt;
- }
- }
- cout << cnt << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment