Advertisement
MegaVerkruzo

Untitled

Sep 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <queue>
  5. #include <utility>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int n, m;
  12. cin >> n >> m;
  13. vector<vector<int>> a(n + 2, vector<int>(m + 2, 0));
  14. for (int i = 1; i <= n; ++i) {
  15. for (int l = 1; l <= m; ++l) {
  16. cin >> a[i][l];
  17. }
  18. }
  19. map<pair<int, int>, int> used;
  20. long long k = 0;
  21. for (int i = 1; i <= n; ++i) {
  22. for (int l = 1; l <= m; ++l) {
  23. if (used[{i, l}] == 0) {
  24. k++;
  25. queue<pair<int, int>> q;
  26. q.push({ i, l });
  27. while (!q.empty()) {
  28. auto p = q.front();
  29. q.pop();
  30. used[p] = 1;
  31. if (a[p.first][p.second] == a[p.first - 1][p.second] && used[{ p.first - 1, p.second }] == 0) {
  32. q.push({ p.first - 1, p.second });
  33. }
  34. if (a[p.first][p.second] == a[p.first][p.second + 1] && used[{ p.first, p.second + 1 }] == 0) {
  35. q.push({ p.first, p.second + 1 });
  36. }
  37. if (a[p.first][p.second] == a[p.first + 1][p.second] && used[{ p.first + 1, p.second }] == 0) {
  38. q.push({ p.first + 1, p.second });
  39. }
  40. if (a[p.first][p.second] == a[p.first][p.second - 1] && used[{ p.first, p.second - 1 }] == 0) {
  41. q.push({ p.first, p.second - 1 });
  42. }
  43. }
  44. }
  45. }
  46. }
  47. cout << k;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement