Advertisement
MegaVerkruzo

sdf

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