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