Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. Given a 2D matrix of booleans representing a block of land/water
  2. False means that slot has land
  3. True means that slot has water
  4.  
  5. Assign a height to each tile such that:
  6. Water is at level 0
  7. Two adjacent tiles heights differences must be at most 1
  8.  
  9. Find the highest tile (just its height, don’t care about which time)
  10.  
  11.  
  12.  
  13.  
  14.  
  15. T 1 7 F F F
  16. 3 2 8 F F F
  17. 11 10 9 F F F
  18. F F F 13 5 6
  19. F F F 12 4 T
  20.  
  21. T 1 F F
  22. 1 1 1 1
  23. F F 1 T
  24. F F F T
  25.  
  26. 0 1 2 ?
  27. 1 1 2 2
  28. ? ? 1 1
  29. ? ? 1 0
  30.  
  31. 0 1 2 ?
  32. 1 1 2 ?
  33. 2 2 2? ?
  34. ? ? ? 0
  35.  
  36. 0 1 1 T 1 F
  37. 1 1 1 1 1 1
  38. F 1 1 1 T 1
  39. F 1 T 1 1 1
  40. T 1 1 1 1 0
  41.  
  42.  
  43.  
  44. Class pair {
  45. Int x;
  46. Int y;
  47. }
  48.  
  49. bool[][] matrix = ...;
  50. Int heights[][] = new int[matrix.length][matrix[0].length]:
  51.  
  52. Queue<Pair> nextOnesToLookAt;
  53. Loop through matrix
  54. Pair[] waters = …Pair[1][1]
  55. nextOneToLookAt.addAll(waters);
  56.  
  57. While (queue not empty) {
  58.   Pair p = queue.next()
  59.   Pair[] adjacents = GetAdjacents(p)
  60.   queue.addAll(adjacents)
  61.   For (Pair a : adjacent) {
  62.     Pair[] doubleAdjacents = GetAdjacents(a);
  63.     Int LowestHeight = Lowest(doubleAdjacents);
  64.   Heights[a.x][a.y] = LowestHeight + 1;
  65.   }
  66. }
  67.  
  68. // loop through heights find max
  69. Return max
  70.  
  71. Breadth first search
  72.  
  73.  
  74.  
  75. /** Also includes diagonals */
  76. Pair[] GetAdjacent(Pair p, int maxlen, int maxwidth)
  77.   … +1 -1 all that bs
  78.   .. dont go below zero
  79.   .. dont go above or equal to the maxes
  80.   .. dont visit existing visited nodes  == either water by checking boolean, or height nonzero
  81.  
  82. /** Find the lowest height out of this list */
  83. Int Lowest(Pair[] ps) {
  84.   Int[] t= new int[ps.length];
  85.   For (int i = 0; i < ps.length; i++) t[i] =  heights[p.x][p.y]
  86.   Return Arrays.min(t);
  87. }Given a 2D matrix of booleans representing a block of land/water
  88. False means that slot has land
  89. True means that slot has water
  90.  
  91. Assign a height to each tile such that:
  92. Water is at level 0
  93. Two adjacent tiles heights differences must be at most 1
  94.  
  95. Find the highest tile (just its height, don’t care about which time)
  96.  
  97.  
  98.  
  99.  
  100.  
  101. T 1 7 F F F
  102. 3 2 8 F F F
  103. 11 10 9 F F F
  104. F F F 13 5 6
  105. F F F 12 4 T
  106.  
  107. T 1 F F
  108. 1 1 1 1
  109. F F 1 T
  110. F F F T
  111.  
  112. 0 1 2 ?
  113. 1 1 2 2
  114. ? ? 1 1
  115. ? ? 1 0
  116.  
  117. 0 1 2 ?
  118. 1 1 2 ?
  119. 2 2 2? ?
  120. ? ? ? 0
  121.  
  122. 0 1 1 T 1 F
  123. 1 1 1 1 1 1
  124. F 1 1 1 T 1
  125. F 1 T 1 1 1
  126. T 1 1 1 1 0
  127.  
  128.  
  129.  
  130. Class pair {
  131. Int x;
  132. Int y;
  133. }
  134.  
  135. bool[][] matrix = ...;
  136. Int heights[][] = new int[matrix.length][matrix[0].length]:
  137.  
  138. Queue<Pair> nextOnesToLookAt;
  139. Loop through matrix
  140. Pair[] waters = …Pair[1][1]
  141. nextOneToLookAt.addAll(waters);
  142.  
  143. While (queue not empty) {
  144.   Pair p = queue.next()
  145.   Pair[] adjacents = GetAdjacents(p)
  146.   queue.addAll(adjacents)
  147.   For (Pair a : adjacent) {
  148.     Pair[] doubleAdjacents = GetAdjacents(a);
  149.     Int LowestHeight = Lowest(doubleAdjacents);
  150.   Heights[a.x][a.y] = LowestHeight + 1;
  151.   }
  152. }
  153.  
  154. // loop through heights find max
  155. Return max
  156.  
  157. Breadth first search
  158.  
  159.  
  160.  
  161. /** Also includes diagonals */
  162. Pair[] GetAdjacent(Pair p, int maxlen, int maxwidth)
  163.   … +1 -1 all that bs
  164.   .. dont go below zero
  165.   .. dont go above or equal to the maxes
  166.   .. dont visit existing visited nodes  == either water by checking boolean, or height nonzero
  167.  
  168. /** Find the lowest height out of this list */
  169. Int Lowest(Pair[] ps) {
  170.   Int[] t= new int[ps.length];
  171.   For (int i = 0; i < ps.length; i++) t[i] =  heights[p.x][p.y]
  172.   Return Arrays.min(t);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement