fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool visited[99][99] = {false};
  5. int counter = 0;
  6. int m[99][99];
  7. int temp;
  8.  
  9. void bola(int x, int y, int cocok, int M, int N){
  10. if(x < 0 || y < 0 || x >= M || y >= N) {
  11. return;
  12. }
  13. if(!visited[x][y] && m[x][y] == cocok){
  14. counter+= 1;
  15. visited[x][y] = true;
  16. bola(x+1, y, cocok, M, N);
  17. bola(x-1, y, cocok, M, N);
  18. bola(x, y+1, cocok, M, N);
  19. bola(x, y-1, cocok, M, N);
  20. }
  21. }
  22.  
  23. int main() {
  24. int M, N, x, y, cocok;
  25. cin >> M >> N;
  26. for(int i = 0; i < M; i++){
  27. for(int j = 0; j < N; j++){
  28. cin >> m[i][j];
  29. }
  30. }
  31. temp = 0;
  32. for(int i = 1; i < M; i++){
  33. for(int j = 1; j < N; j++){
  34. if (i > 0 && j > 0 && m[i][j] != m[i-1][j-1]){
  35. cocok = m[i-1][j-1];
  36. bola(i-1, j-1, cocok, M, N);
  37. int hasil = counter * (counter-1);
  38. if (hasil > temp){
  39. temp = hasil;
  40. }
  41. counter = 0;
  42. }
  43. }
  44. }
  45.  
  46. cout << temp;
  47. return 0;
  48. }
  49.  
  50.  
  51.  
  52.  
Success #stdin #stdout 0.01s 5436KB
stdin
4 4
1 2 2 1
1 1 3 3
4 4 4 5
5 5 5 5
stdout
6