fork download
  1. const maxn = 700;
  2. var n,m:word;
  3. a: array[0..maxn+1,0..maxn+1] of word;
  4. visit: array[0..maxn+1,0..maxn+1] of boolean;
  5. procedure enter;
  6. var i,j:word;
  7. begin
  8. readln(n,m);
  9. for i:=1 to n do
  10. begin
  11. for j:=1 to m do
  12. begin
  13. read(a[i,j]);
  14. visit[i,j]:=false;
  15. end;
  16. readln;
  17. end;
  18. end;
  19. function dfs(x,y:word):boolean;
  20. var p,pp:boolean;
  21. begin
  22. p:=true;
  23. visit[x,y]:=true;
  24. if (x>1) and (a[x-1,y]>a[x,y]) then p:=false;
  25. if p and (x<n) and (a[x+1,y]>a[x,y]) then p:=false;
  26. if p and (y>1) and (a[x,y-1]>a[x,y]) then p:=false;
  27. if p and (y<m) and (a[x,y+1]>a[x,y]) then p:=false;
  28. if p and (x>1) and (y>1) and (a[x-1,y-1]>a[x,y]) then p:=false;
  29. if p and (x>1) and (y<m) and (a[x-1,y+1]>a[x,y]) then p:=false;
  30. if p and (x<n) and (y>1) and (a[x+1,y-1]>a[x,y]) then p:=false;
  31. if p and (x<n) and (y<m) and (a[x+1,y+1]>a[x,y]) then p:=false;
  32. if not p then
  33. begin
  34. dfs:=p;
  35. if (x>1) and (a[x-1,y]=a[x,y]) and (not visit[x-1,y]) then dfs(x-1,y);
  36. if (x<n) and (a[x+1,y]=a[x,y]) and (not visit[x+1,y]) then dfs(x+1,y);
  37. if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then dfs(x,y-1);
  38. if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
  39. if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
  40. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
  41. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  42. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  43. end;
  44. if (x>1) and (a[x-1,y]=a[x,y]) and (not visit[x-1,y]) then p:=p and dfs(x-1,y);
  45. if not p then
  46. begin
  47. dfs:=p;
  48. if (x<n) and (a[x+1,y]=a[x,y]) and (not visit[x+1,y]) then dfs(x+1,y);
  49. if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then dfs(x,y-1);
  50. if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
  51. if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
  52. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
  53. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  54. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  55. end;
  56. if (x<n) and (a[x+1,y]=a[x,y]) and (not visit[x+1,y]) then p:=p and dfs(x+1,y);
  57. if not p then
  58. begin
  59. dfs:=p;
  60. if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then dfs(x,y-1);
  61. if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
  62. if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
  63. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
  64. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  65. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  66. end;
  67. if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then p:=p and dfs(x,y-1);
  68. if not p then
  69. begin
  70. dfs:=p;
  71. if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
  72. if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
  73. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
  74. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  75. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  76. end;
  77. if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then p:=p and dfs(x,y+1);
  78. if not p then
  79. begin
  80. dfs:=p;
  81. if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
  82. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
  83. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  84. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  85. end;
  86. if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then p:=p and dfs(x-1,y-1);
  87. if not p then
  88. begin
  89. dfs:=p;
  90. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
  91. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  92. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  93. end;
  94. if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then p:=p and dfs(x-1,y+1);
  95. if not p then
  96. begin
  97. dfs:=p;
  98. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
  99. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  100. end;
  101. if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then p:=p and dfs(x+1,y-1);
  102. if not p then
  103. begin
  104. dfs:=p;
  105. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  106. end;
  107. if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then p:=p and dfs(x+1,y+1);
  108. dfs:=p;
  109. end;
  110. procedure initvisit;
  111. var i,j:word;
  112. begin
  113. for i:=1 to n do
  114. for j:=1 to m do
  115. visit[i,j]:=false;
  116. end;
  117. procedure printvisit;
  118. var i,j:word;
  119. begin
  120. for i:=1 to n do
  121. begin
  122. for j:=1 to m do
  123. write(visit[i,j]:5,' ');
  124. writeln;
  125. end;
  126. end;
  127. procedure solve;
  128. var i,j,res:word;
  129. p:boolean;
  130. begin
  131. res:=0;
  132. for i:=1 to n do
  133. for j:=1 to m do
  134. if not visit[i,j] then
  135. if dfs(i,j) then
  136. begin
  137. inc(res);
  138. end;
  139. writeln(res);
  140. end;
  141. begin
  142. enter;
  143. solve;
  144. end.
  145.  
  146.  
  147.  
Success #stdin #stdout 0s 1732KB
stdin
8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0
stdout
3