Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <utility>
- using namespace std;
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- vector<vector<short>> a(n + 2, vector<short>(m + 2, 0));
- for (int i = 1; i <= n; ++i) {
- for (int l = 1; l <= m; ++l) {
- cin >> a[i][l];
- }
- }
- vector<vector<bool>> used(n + 2, vector<bool>(m + 2, 0));
- 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<short, short>> q;
- q.push({ i, l });
- while (!q.empty()) {
- auto p = q.front();
- q.pop();
- used[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 });
- }
- 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