fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m;
  5. int a[705][705];
  6. bool used[705][705];
  7.  
  8. int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
  9. int dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
  10.  
  11. bool bfs(int sx, int sy) {
  12. queue<pair<int,int>> q;
  13. q.push({sx, sy});
  14. used[sx][sy] = true;
  15.  
  16. int h = a[sx][sy];
  17. bool is_peak = true;
  18.  
  19. while (!q.empty()) {
  20. auto [x, y] = q.front();
  21. q.pop();
  22.  
  23. for (int k = 0; k < 8; k++) {
  24. int u = x + dx[k];
  25. int v = y + dy[k];
  26.  
  27. if (u < 1 || u > n || v < 1 || v > m) continue;
  28.  
  29. if (a[u][v] > h)
  30. is_peak = false;
  31.  
  32. if (!used[u][v] && a[u][v] == h) {
  33. used[u][v] = true;
  34. q.push({u, v});
  35. }
  36. }
  37. }
  38.  
  39. return is_peak;
  40. }
  41.  
  42. int main() {
  43. ios::sync_with_stdio(false);
  44. cin.tie(nullptr);
  45.  
  46. cin >> n >> m;
  47. for (int i = 1; i <= n; i++)
  48. for (int j = 1; j <= m; j++)
  49. cin >> a[i][j];
  50.  
  51. int cnt = 0;
  52.  
  53. for (int i = 1; i <= n; i++) {
  54. for (int j = 1; j <= m; j++) {
  55. if (!used[i][j]) {
  56. if (bfs(i, j))
  57. cnt++;
  58. }
  59. }
  60. }
  61.  
  62. cout << cnt << "\n";
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0.01s 5284KB
stdin
8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0
stdout
3