Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. class Percolation {
  2. static int size = 4;
  3. static int length = size*size;
  4. static boolean[] grid = new boolean[length];
  5. static int step = 0;
  6. void init() {
  7. for (int i = 0; i < size; i++){
  8. for (int j = 0; j < size; j++)
  9. grid[i*size+j] = false;
  10. }
  11. }
  12. void print() {
  13. for (int j = 0; j < size; j++){
  14. System.out.println();
  15. for (int i = 0; i < size; i++){
  16. if (grid[i*size+j])
  17. System.out.print('*');
  18. else
  19. System.out.print('-');
  20.  
  21. }
  22. }
  23. }
  24.  
  25. int random_shadow() {
  26. int index = (int)(Math.random()*length);
  27. if(!grid[index]) {
  28. grid[index] = true;
  29. return index;
  30. }
  31. else return random_shadow();
  32.  
  33. }
  34.  
  35. float percolation() {
  36. float summ = 0;
  37. boolean perc=false;
  38. while(!perc) {
  39. System.out.print("un nouveau case noir");
  40. random_shadow();
  41. for (int i = 0; i < length; i++)
  42. if (grid[i]&(i % size != size-1)&(i % size != 0)) {
  43. if(is_percolation(i)){
  44. perc=true;
  45. System.out.println("DABLYAD");
  46. for (int j = 0; j < length; j++)
  47. if(grid[j]) summ++;
  48. summ/=length;
  49. break;
  50. }
  51.  
  52. }
  53. }
  54. return summ;
  55. }
  56.  
  57. boolean is_percolation(int n) {
  58. System.out.print("Is percolation");
  59. boolean[] seen = new boolean[length];
  60. step=0;
  61. if (grid[n]) {
  62. if(detect_path(seen, n, true))
  63. //seen = new boolean[length];
  64. {
  65. step=0;
  66. if (detect_path(seen, n, false))
  67. return true;
  68. }
  69. }
  70. return false;
  71.  
  72. }
  73.  
  74.  
  75. boolean detect_path(boolean[] seen, int n, boolean up){
  76. seen[n] = true;
  77. System.out.println();
  78. System.out.print(step++);
  79. for (int j = 0; j < size; j++){
  80. System.out.println();
  81. for (int i = 0; i < size; i++){
  82. if (seen[i*size+j])
  83. System.out.print('*');
  84. else
  85. System.out.print('-');
  86.  
  87. }
  88. }
  89. System.out.println();
  90. if (((n % size == 0)&up)||((n % size == size-1)&!up)) {
  91. System.out.println("return TRUE FUCK");
  92. System.out.println(size);
  93. System.out.println(n);
  94. return true;
  95. }
  96.  
  97.  
  98. if (n-1 >=0&up) {
  99. if((grid[n-1])&(!seen[n-1])) return detect_path(seen, n-1 , up);
  100. }
  101.  
  102. if (n+1 < length&!up) {
  103. if((grid[n+1])&(!seen[n+1])) return detect_path(seen, n+1 , up);
  104. }
  105.  
  106.  
  107. if (n+size < length) {
  108. if((grid[n+size])&(!seen[n+size])) return detect_path(seen, n+size , up);
  109. }
  110.  
  111. if (n-size >= 0) {
  112. if((grid[n-size])&(!seen[n-size])) return detect_path(seen, n-size , up);
  113. }
  114.  
  115.  
  116. if(up & (((n+1) % size) != size-1))
  117. if(grid[n+1]) {
  118. System.out.print("return down");
  119. return detect_path(seen, n+1 , up);
  120. }
  121.  
  122. if (!up & (((n-1) % size) != 0))
  123. if(grid[n-1]) {
  124. System.out.print("return up");
  125. return detect_path(seen, n-1 , up);
  126. }
  127.  
  128.  
  129. return false;
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137. public static void main(String[] args) {
  138. Percolation p = new Percolation();
  139. p.init();
  140. //System.out.println((int)(Math.random()*(Percolation.length)));
  141. //p.grid = new boolean[]{true,true,true,false,false,false,true,false,false,true,true,true,false,false,true,true};
  142. //p.grid[5]=true;
  143. //p.grid[7]=true;
  144.  
  145.  
  146. float summ = p.percolation();
  147. System.out.println(summ);
  148. boolean[] seen = new boolean[length];
  149. //System.out.println(p.detect_path(seen, 11, true));
  150.  
  151.  
  152. p.print();
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement