Guest User

Untitled

a guest
May 26th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.Scanner;
  8.  
  9. // The picture shows an example of 4 rows and 3 columns of hexagons. The numbers represent the height of each tile. The height of the ground outside the hexagons is always 0.
  10.  
  11. // Pesho has to enter the upper-left tile pass and exit from the bottom-right one. When he passes through two different heights his bike receives damage equal to their difference. Help Pesho minimize the damage his bike receives.
  12. // Input
  13.  
  14. // Input is read from the console
  15. // On the first line an integer number R is given
  16. // the number of rows
  17. // On the second line an integer number C is given
  18. // the number of columns
  19. // On each of the next R lines C numbers will be given
  20. // the heights of the tiles
  21. // separated by spaces
  22.  
  23. // Output
  24.  
  25. // Output should be printed to the console
  26. // On single line print the minimal the damage Pesho's bike can receive
  27. // Output with two digits of precision
  28.  
  29. // Constraints
  30.  
  31. // 0 < R, C <= 500
  32. // -100 <= height of a cell <= 100
  33. // Sample Input
  34. // 4
  35. // 3
  36. // 2 3 1
  37. // 5 4 6
  38. // 9 0 3
  39. // 8 5 2
  40.  
  41. // Pesho enters cell 2 -> receiving 2 damage
  42. // then goes to cell 3 -> receiving 1 damage
  43. // then goes to cell 4 -> receiving 1 damage
  44. // then goes to cell 3 -> receiving 1 damage
  45. // then goes to cell 2 -> receiving 1 damage
  46. // finally exits cell 2 -> receives 2 damage
  47.  
  48. // total damage -> 8
  49.  
  50. public class BikeDamage {
  51. static boolean recursion = false;
  52. static boolean[] visited = new boolean[50];
  53.  
  54. static void path(int source, LinkedList<Edge>[] adj,int B, float[][] matrix, float maxCount, int R, int C) {
  55.  
  56. int count = 0;
  57.  
  58. if(source == B) {
  59. recursion = true;
  60. System.out.println(maxCount + matrix[R-1][C-1] + matrix[0][0]);
  61. } else {
  62.  
  63. int dest = 0;
  64.  
  65. LinkedList<Edge> l = adj[source];
  66. float min = Integer.MAX_VALUE;
  67. int minDest = 0;
  68.  
  69. for (Edge i : l) {
  70.  
  71. if(min > i.weight) {
  72. min = i.weight;
  73. minDest = i.destination;
  74. }
  75. }
  76.  
  77.  
  78.  
  79. maxCount += min;
  80.  
  81. source = minDest;
  82. path(source, adj, B, matrix, maxCount, R, C);
  83.  
  84. }
  85.  
  86. }
  87.  
  88. public static void main(String args[]) throws IOException {
  89. Scanner sc = new Scanner(System.in);
  90. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  91.  
  92. int R = sc.nextInt();
  93. int C = sc.nextInt();
  94.  
  95. float[][] hexagon = new float[R][C];
  96.  
  97. float[] junk = new float[C];
  98.  
  99. Graph graph = new Graph(R*C);
  100.  
  101. for(int i=0;i < R; i++) {
  102. String[] in = br.readLine().split(" ");
  103. for(int k=0; k < C; k++) {
  104.  
  105. junk[k] = Float.parseFloat(in[k]);
  106. }
  107. for(int j=0; j<C; j++) {
  108. hexagon[i][j] = junk[j];
  109. }
  110. }
  111.  
  112. for(int i=0; i<R; i++) {
  113. for(int j=0; j<C; j++) {
  114. System.out.print(hexagon[i][j] + " ");
  115. }
  116. System.out.println();
  117. }
  118.  
  119. for(int i=0; i<R; i++) {
  120. for(int j=0; j<C; j++) {
  121. int currentNode = i * (R - 1) + j;
  122. // Even Rows / Zero Column
  123. if (i == R - 1 && j == C - 1) {
  124. break;
  125. }
  126.  
  127. if (j == 0 && i % 2 == 0) {
  128. // Right Neighbor
  129. graph.addEgde(currentNode, i * (R - 1) + j + 1, Math.abs(hexagon[i][j] - hexagon[i][j + 1]));
  130. // Down Neighbor
  131. graph.addEgde(currentNode, (i + 1) * (R - 1) + j, Math.abs(hexagon[i][j] - hexagon[i + 1][j]));
  132. // Last Column
  133. } else if (j == C - 1) {
  134. // Down Neighbor
  135. graph.addEgde(currentNode, (i + 1) * (R - 1) + j, Math.abs(hexagon[i][j] - hexagon[i + 1][j]));
  136. // Diagonal Neighbor
  137. graph.addEgde(currentNode, (i + 1) * (R - 1) + (j - 1), Math.abs(hexagon[i][j] - hexagon[i + 1][j - 1]));
  138. // Last Row
  139. } else if (i == R - 1) {
  140. // Right Neighbor
  141. graph.addEgde(currentNode, (i) * (R - 1) + j + 1, Math.abs(hexagon[i][j] - hexagon[i][j + 1]));
  142. // hexagon inside
  143. } else if (i % 2 == 0) {
  144. // Right Neighbor
  145. graph.addEgde(currentNode, i * (R - 1) + j + 1, Math.abs(hexagon[i][j] - hexagon[i][j + 1]));
  146. // Down Neighbor
  147. graph.addEgde(currentNode, (i + 1) * (R - 1) + j, Math.abs(hexagon[i][j] - hexagon[i + 1][j]));
  148. // Diagonal Neighbor
  149. graph.addEgde(currentNode, (i + 1) * (R - 1) + (j - 1), Math.abs(hexagon[i][j] - hexagon[i + 1][j - 1]));
  150.  
  151. } else { // ODD ROWS
  152. // Right Neighbor
  153. graph.addEgde(currentNode, i * (R - 1) + j + 1, Math.abs(hexagon[i][j] - hexagon[i][j + 1]));
  154. // Down Neighbor
  155. graph.addEgde(currentNode, (i + 1) * (R - 1) + j, Math.abs(hexagon[i][j] - hexagon[i + 1][j]));
  156. // Diagonal Neighbor
  157. graph.addEgde(currentNode, (i + 1) * (R - 1) + (j + 1), Math.abs(hexagon[i][j] - hexagon[i + 1][j + 1]));
  158. }
  159.  
  160.  
  161. }
  162. }
  163. int B = R*C - 1;
  164.  
  165. path(0, graph.adjacencylist, B, hexagon,0, R, C);
  166.  
  167. System.out.println();
  168.  
  169.  
  170. }
  171. }
Add Comment
Please, Sign In to add comment