Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <queue>
- #include <utility>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- vector<vector<int>> a(n + 2, vector<int>(m + 2, 0));
- for (int i = 1; i <= n; ++i) {
- for (int l = 1; l <= m; ++l) {
- cin >> a[i][l];
- }
- }
- map<pair<int, int>, int> used;
- long long k = 0;
- for (int i = 1; i <= n; ++i) {
- for (int l = 1; l <= m; ++l) {
- if (used[{i, l}] == 0) {
- k++;
- queue<pair<int, int>> q;
- q.push({ i, l });
- while (!q.empty()) {
- auto p = q.front();
- q.pop();
- used[p] = 1;
- if (a[p.first][p.second] == a[p.first - 1][p.second] && used[{ p.first - 1, p.second }] == 0) {
- q.push({ p.first - 1, p.second });
- }
- if (a[p.first][p.second] == a[p.first][p.second + 1] && used[{ p.first, p.second + 1 }] == 0) {
- q.push({ p.first, p.second + 1 });
- }
- if (a[p.first][p.second] == a[p.first + 1][p.second] && used[{ p.first + 1, p.second }] == 0) {
- q.push({ p.first + 1, p.second });
- }
- if (a[p.first][p.second] == a[p.first][p.second - 1] && used[{ p.first, p.second - 1 }] == 0) {
- q.push({ p.first, p.second - 1 });
- }
- }
- }
- }
- }
- cout << k;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement