fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. typedef struct point
  5. {
  6. int x;
  7. int y;
  8. }Point;
  9.  
  10. Point p[100];
  11.  
  12.  
  13. int top=-1;
  14.  
  15. Point pop()
  16. {
  17. return p[top--];
  18. }
  19.  
  20. void push(int x1,int y1)
  21. {
  22. top++;
  23. p[top].x=x1;
  24. p[top].y=y1;
  25. }
  26.  
  27. int temp[500][500];
  28. int ar[500][500];
  29. int ar1[100];
  30.  
  31. int main()
  32. {
  33. int t;
  34. cin >> t;
  35. for(int o=0; o<t; o++)
  36. {
  37. top=-1;
  38. for(int i=0; i<500; i++)
  39. for(int j=0; j<500; j++)
  40. temp[i][j]=0;
  41. for(int i=0; i<100; i++)
  42. ar1[i]=0;
  43. int acnt;
  44. acnt =0;
  45. int n,cnt=0,max;
  46. cnt=0;
  47. cin >> n;
  48. for(int i=1; i<=n; i++)
  49. {
  50. for(int j=1; j<=n; j++)
  51. {
  52. cin >> temp[i][j];
  53. ar1[cnt]=temp[i][j];
  54. cnt++;
  55. }
  56. }
  57.  
  58. max=acnt;
  59. for(int i=0; i<cnt; i++)
  60. {
  61. acnt=0;
  62. for(int j=0; j<500; j++)
  63. for(int k=0; k<500; k++)
  64. ar[j][k]=temp[j][k];
  65.  
  66. for(int j=1; j<=n; j++)
  67. {
  68. for(int k=1; k<=n; k++)
  69. {
  70. if(ar[j][k]<=ar1[i])
  71. ar[j][k]=0;
  72. else
  73. ar[j][k]=1;
  74. }
  75. }
  76.  
  77.  
  78. for(int j=1; j<=n; j++)
  79. {
  80. for(int k=1; k<=n; k++)
  81. {
  82. if(ar[j][k]==1)
  83. {
  84. acnt++;
  85. push(j,k);
  86. bool flag=false;
  87. while(top!=-1)
  88. {
  89. Point t=pop();
  90. if(ar[t.x+1][t.y]==1)
  91. {
  92. push(t.x+1,t.y);
  93. flag=true;
  94. }
  95. if(ar[t.x-1][t.y]==1)
  96. {
  97. push(t.x-1,t.y);
  98. flag=true;
  99. }
  100. if(ar[t.x][t.y+1]==1)
  101. {
  102. push(t.x,t.y+1);
  103. flag=true;
  104. }
  105. if(ar[t.x][t.y-1]==1)
  106. {
  107. push(t.x,t.y-1);
  108. flag=true;
  109. }
  110. ar[t.x][t.y]=0;
  111. flag=false;
  112. }
  113. if(acnt>max)
  114. max=acnt;
  115. }
  116. }
  117. }
  118. }
  119. cout << max << endl;
  120. }
  121. return 0;
  122. }
Success #stdin #stdout 0.02s 5296KB
stdin
2
5
6 8 2 6 2
3 2 3 4 6
6 7 3 3 2
7 2 5 3 6
8 9 5 2 7
4
1 2 3 4
4 3 2 1
2 3 4 1
1 1 1 1
stdout
5
3