Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Given a 2D matrix of booleans representing a block of land/water
- False means that slot has land
- True means that slot has water
- Assign a height to each tile such that:
- Water is at level 0
- Two adjacent tiles heights differences must be at most 1
- Find the highest tile (just its height, don’t care about which time)
- T 1 7 F F F
- 3 2 8 F F F
- 11 10 9 F F F
- F F F 13 5 6
- F F F 12 4 T
- T 1 F F
- 1 1 1 1
- F F 1 T
- F F F T
- 0 1 2 ?
- 1 1 2 2
- ? ? 1 1
- ? ? 1 0
- 0 1 2 ?
- 1 1 2 ?
- 2 2 2? ?
- ? ? ? 0
- 0 1 1 T 1 F
- 1 1 1 1 1 1
- F 1 1 1 T 1
- F 1 T 1 1 1
- T 1 1 1 1 0
- Class pair {
- Int x;
- Int y;
- }
- bool[][] matrix = ...;
- Int heights[][] = new int[matrix.length][matrix[0].length]:
- Queue<Pair> nextOnesToLookAt;
- Loop through matrix
- Pair[] waters = …Pair[1][1]
- nextOneToLookAt.addAll(waters);
- While (queue not empty) {
- Pair p = queue.next()
- Pair[] adjacents = GetAdjacents(p)
- queue.addAll(adjacents)
- For (Pair a : adjacent) {
- Pair[] doubleAdjacents = GetAdjacents(a);
- Int LowestHeight = Lowest(doubleAdjacents);
- Heights[a.x][a.y] = LowestHeight + 1;
- }
- }
- // loop through heights find max
- Return max
- Breadth first search
- /** Also includes diagonals */
- Pair[] GetAdjacent(Pair p, int maxlen, int maxwidth)
- … +1 -1 all that bs
- .. dont go below zero
- .. dont go above or equal to the maxes
- .. dont visit existing visited nodes == either water by checking boolean, or height nonzero
- /** Find the lowest height out of this list */
- Int Lowest(Pair[] ps) {
- Int[] t= new int[ps.length];
- For (int i = 0; i < ps.length; i++) t[i] = heights[p.x][p.y]
- Return Arrays.min(t);
- }Given a 2D matrix of booleans representing a block of land/water
- False means that slot has land
- True means that slot has water
- Assign a height to each tile such that:
- Water is at level 0
- Two adjacent tiles heights differences must be at most 1
- Find the highest tile (just its height, don’t care about which time)
- T 1 7 F F F
- 3 2 8 F F F
- 11 10 9 F F F
- F F F 13 5 6
- F F F 12 4 T
- T 1 F F
- 1 1 1 1
- F F 1 T
- F F F T
- 0 1 2 ?
- 1 1 2 2
- ? ? 1 1
- ? ? 1 0
- 0 1 2 ?
- 1 1 2 ?
- 2 2 2? ?
- ? ? ? 0
- 0 1 1 T 1 F
- 1 1 1 1 1 1
- F 1 1 1 T 1
- F 1 T 1 1 1
- T 1 1 1 1 0
- Class pair {
- Int x;
- Int y;
- }
- bool[][] matrix = ...;
- Int heights[][] = new int[matrix.length][matrix[0].length]:
- Queue<Pair> nextOnesToLookAt;
- Loop through matrix
- Pair[] waters = …Pair[1][1]
- nextOneToLookAt.addAll(waters);
- While (queue not empty) {
- Pair p = queue.next()
- Pair[] adjacents = GetAdjacents(p)
- queue.addAll(adjacents)
- For (Pair a : adjacent) {
- Pair[] doubleAdjacents = GetAdjacents(a);
- Int LowestHeight = Lowest(doubleAdjacents);
- Heights[a.x][a.y] = LowestHeight + 1;
- }
- }
- // loop through heights find max
- Return max
- Breadth first search
- /** Also includes diagonals */
- Pair[] GetAdjacent(Pair p, int maxlen, int maxwidth)
- … +1 -1 all that bs
- .. dont go below zero
- .. dont go above or equal to the maxes
- .. dont visit existing visited nodes == either water by checking boolean, or height nonzero
- /** Find the lowest height out of this list */
- Int Lowest(Pair[] ps) {
- Int[] t= new int[ps.length];
- For (int i = 0; i < ps.length; i++) t[i] = heights[p.x][p.y]
- Return Arrays.min(t);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement