fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n, m, x, y;
  7. cin >> n >> m >> x >> y;
  8.  
  9. vector<vector<int>> a(n, vector<int>(m));
  10. for (int i = 0; i < n; i++)
  11. {
  12. for (int j = 0; j < m; j++)
  13. {
  14. cin >> a[i][j];
  15. }
  16. }
  17.  
  18. vector<vector<bool>> visited(n, vector<bool>(m, false));
  19. x--;
  20. y--;
  21.  
  22. queue<pair<int, int>> q;
  23. q.push({x, y});
  24. visited[x][y] = true;
  25.  
  26. int area = 1;
  27. int rowMoves[] = {-1, 1, 0, 0};
  28. int colMoves[] = {0, 0, -1, 1};
  29.  
  30. while (!q.empty())
  31. {
  32. int row = q.front().first;
  33. int col = q.front().second;
  34. q.pop();
  35.  
  36. for (int i = 0; i < 4; i++)
  37. {
  38. int newRow = row + rowMoves[i];
  39. int newCol = col + colMoves[i];
  40.  
  41. if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m)
  42. {
  43. if (a[newRow][newCol] <= a[row][col] && !visited[newRow][newCol])
  44. {
  45. q.push({newRow, newCol});
  46. visited[newRow][newCol] = true;
  47. area++;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. cout << area << endl;
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5476KB
stdin
5 5 1 3
1 2 3 3 2
3 1 2 3 1
2 3 1 2 3
1 2 3 1 2
3 1 2 3 1
stdout
12