fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int max_n = 102;
  4. int n, a[102][102], visited[102][102], ret = 0;
  5. int dy[] = {-1, 0, 1, 0};
  6. int dx[] = {0, 1, 0, -1};
  7.  
  8. void dfs(int y, int x, int depth){
  9. visited[y][x] = 1;
  10. for(int i = 0; i < 4; i++){
  11. int ny = y + dy[i];
  12. int nx = x + dx[i];
  13. if(ny < 0 || ny >= n || nx < 0 || nx >= n || visited[ny][nx] || a[ny][nx] <= depth) continue;
  14. dfs(ny, nx, depth);
  15. }
  16. }
  17.  
  18. int main(){
  19. cin >> n;
  20. for(int i = 0; i < n; i++){
  21. for(int j = 0; j < n; j++){
  22. cin >> a[i][j];
  23. }
  24. }
  25. for(int d = 1; d <= 100; d++){
  26. int cnt = 0;
  27. fill(&visited[0][0], &visited[0][0] + max_n * max_n, 0);
  28. for(int i = 0; i < n; i++){
  29. for(int j = 0; j < n; j++){
  30. if(!visited[i][j] && a[i][j] > d){
  31. dfs(i, j, d);
  32. cnt++;
  33. }
  34. }
  35. }
  36. ret = max(ret, cnt);
  37. }
  38. cout << ret << '\n';
  39. }
Success #stdin #stdout 0.01s 5300KB
stdin
7
9 9 9 9 9 9 9
9 2 1 2 1 2 9
9 1 8 7 8 1 9
9 2 7 9 7 2 9
9 1 8 7 8 1 9
9 2 1 2 1 2 9
9 9 9 9 9 9 9
stdout
6