Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. original output:
  2. 1 0 2 2 0 0 3 3 3 3 3 3 3 3 0
  3. 0 0 2 0 0 4 0 0 3 3 3 0 0 0 5
  4. 0 2 2 2 2 0 0 0 0 3 0 5 5 5 5
  5. 6 0 2 2 0 7 0 0 0 0 5 5 0 0 5
  6. 0 0 2 2 0 0 0 0 5 0 0 0 8 0 5
  7. 9 0 2 0 10 0 0 0 5 0 0 5 0 5 5
  8. 9 9 0 0 10 0 5 5 5 5 5 5 5 5 0
  9. 9 0 0 10 10 10 0 0 5 5 5 0 0 5 5
  10. 9 9 0 10 10 10 0 11 0 5 0 0 12 0 0
  11. 0 0 13 0 0 10 10 0 5 5 0 12 12 0 0
  12.  
  13. output that come:
  14. 1 0 2 2 0 0 3 3 3 3 3 3 3 3 0
  15. 0 0 2 0 0 4 0 0 3 3 3 0 0 0 5
  16. 0 2 2 2 2 0 0 0 0 3 0 5 5 5 5
  17. 6 0 2 2 0 7 0 0 0 0 5 5 0 0 5
  18. 0 0 2 2 0 0 0 0 5 0 0 0 9 0 5
  19. 10 0 2 0 11 0 0 0 5 0 0 5 0 5 5
  20. 10 10 0 0 11 0 5 5 5 5 5 5 5 5 0
  21. 10 0 0 11 11 11 0 0 5 5 5 0 0 5 5
  22. 10 10 0 11 11 11 0 12 0 5 0 0 13 0 0
  23. 0 0 14 0 0 11 11 0 5 5 0 13 13 0 0
  24. package percolate;
  25.  
  26. public class Count
  27. {
  28. int i,j,count=0;
  29. /*int[][] matrix = { {1,1,1,0,0,0,0,1,1},
  30. {1,1,1,1,0,1,1,1,1},
  31. {0,1,1,0,0,1,1,0,0},
  32. {1,1,0,1,0,0,1,1,0},
  33. {1,1,1,0,1,0,1,1,0},
  34. {0,0,1,1,0,0,1,1,1},
  35. {0,1,1,1,1,1,0,0,0},
  36. {0,0,0,1,0,0,1,1,1},
  37. {1,0,0,1,0,1,1,1,0}};*/
  38. int[][] matrix = {
  39. {1 ,0 ,1 ,1 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0},
  40. {0 ,0 ,1 ,0 ,0 ,1 ,0 ,0 ,1 ,1 ,1 ,0 ,0 ,0 ,1},
  41. {0 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,1 ,1, 1},
  42. {1 ,0 ,1 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,1 ,1, 0, 0, 1},
  43. {0 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,0, 0, 1, 0, 1},
  44. {1 ,0 ,1 ,0 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,1, 0 ,1, 1},
  45. {1 ,1 ,0 ,0 ,1 ,0 ,1 ,1 ,1 ,1 ,1 ,1, 1 ,1 ,0},
  46. {1 ,0 ,0 ,1 ,1 ,1 ,0 ,0 ,1 ,1 ,1 ,0, 0 ,1, 1},
  47. {1 ,1 ,0 ,1 ,1 ,1 ,0 ,1 ,0 ,1 ,0 ,0 ,1, 0, 0},
  48. {0 ,0 ,1 ,0 ,0 ,1 ,1 ,0 ,1 ,1 ,0 ,1, 1 ,0, 0},
  49.  
  50. };
  51. int row =10,col =15;
  52. int[][] label = new int [row][col];
  53.  
  54. private void operation(int i,int j)
  55. {
  56. // TODO Auto-generated method stub
  57.  
  58. if(i==0 && j==0)
  59. {
  60. count=count+1;
  61. label[i][j]=count;
  62. }
  63. else if (((i-1)>=0) && j==0)
  64. {
  65. left(i,j);
  66. }
  67. else if (((j-1)>=0)&& i==0)
  68. {
  69. above(i,j);
  70. }
  71. else
  72. {
  73. aboveleft(i,j);
  74. }
  75.  
  76.  
  77. }
  78. private void check()
  79. {
  80. // TODO Auto-generated method stub
  81. for (int i = 0; i < row; i++)
  82. {
  83. for (int j = 0; j < col; j++)
  84. {
  85. if(matrix[i][j]==0)
  86. {
  87. label[i][j]=matrix[i][j];
  88. }
  89. else
  90. {
  91. operation(i,j);
  92. }
  93. }
  94. }
  95.  
  96. }
  97. private void left(int a,int b)
  98. {
  99. // TODO Auto-generated method stub
  100. if(matrix[a-1][b]!=0)
  101. {
  102. label[a][b]=label[a-1][b];
  103. }
  104. else
  105. {
  106. count=count+1;
  107. label[a][b]=count;
  108. }
  109.  
  110. }
  111. private void above(int a,int b)
  112. {
  113. // TODO Auto-generated method stub
  114. if (matrix[a][b-1]!=0)
  115. {
  116. label[a][b]=label[a][b-1];
  117. }
  118. else
  119. {
  120. count=count+1;
  121. label[a][b]=count;
  122. }
  123. }
  124. private void aboveleft(int a,int b)
  125. {
  126. // TODO Auto-generated method stub
  127. if (matrix[a][b-1]!=0 && matrix[a-1][b]==0)
  128. {
  129. label[a][b]=label[a][b-1];
  130. }
  131. else if (matrix[a-1][b]!=0 && matrix[a][b-1]==0)
  132. {
  133. label[a][b]=label[a-1][b];
  134. }
  135. else if (matrix[a][b-1]==0 && matrix[a-1][b]==0)
  136. {
  137. count=count+1;
  138. label[a][b]=count;
  139. }
  140. else
  141. {
  142. checklabel(a, b);
  143. }
  144.  
  145. }
  146. private void checklabel(int a, int b)
  147. {
  148. // TODO Auto-generated method stub
  149.  
  150. if(label[a-1][b]>label[a][b-1])
  151. {
  152. label[a][b]=label[a][b-1];
  153. int neww=label[a][b-1];
  154. int old=label[a-1][b];
  155. nonzero(old,neww);
  156. count=count-1;
  157. }
  158. else if (label[a-1][b]==label[a][b-1])
  159. {
  160. label[a][b]=label[a-1][b];
  161.  
  162. }
  163. else
  164. {
  165. label[a][b]=label[a-1][b];
  166. int neww=label[a-1][b];
  167. int old=label[a][b-1];
  168. nonzero(old,neww);
  169. count=count-1;
  170. }
  171.  
  172. }
  173. private void nonzero(int ol,int nw)
  174. {
  175. // TODO Auto-generated method stub
  176. for (int i = 0; i < row; i++)
  177. {
  178. for (int j = 0; j < col; j++)
  179. {
  180. if (label[i][j]==ol)
  181. {
  182. label[i][j]=nw;
  183. backtrace(i,j);
  184. }
  185. }
  186. }
  187. }
  188. private void backtrace(int a,int b)
  189. {
  190. // TODO Auto-generated method stub
  191. for (int i = a; i < row; i++)
  192. {
  193. for (int j = b; j < col; j++)
  194. {
  195.  
  196. }
  197. }
  198.  
  199. }
  200. private void output()
  201. {
  202. // TODO Auto-generated method stub
  203. for (int i = 0; i < row; i++)
  204. {
  205. for (int j = 0; j < col; j++)
  206. {
  207. System.out.print(label[i][j]+" ");
  208. }
  209. System.out.println();
  210. }
  211.  
  212. }
  213. public static void main(String[] args)
  214. {
  215. Count a=new Count();
  216. a.check();
  217. a.output();
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement