Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6.  
  7. /**
  8. * Created by Jonathan Nilsfors on 22/09/14.
  9. */
  10. public class Main {
  11.  
  12. public static void main(String[] args) {
  13. int rows, cols;
  14. int matrix[][] = new int[0][];
  15.  
  16. if (args.length < 1 || args.length > 1) {
  17. System.out.println("Wrong argument. Supply a text file.");
  18. return;
  19. }
  20.  
  21. BufferedReader br = null;
  22. try {
  23. br = new BufferedReader(new FileReader(args[0]));
  24. } catch (FileNotFoundException e) {
  25. System.out.println("File not found.");
  26. return;
  27. }
  28.  
  29. try {
  30.  
  31. String line;
  32.  
  33. while ((line = br.readLine()) != null) {
  34. rows = Integer.parseInt(line.substring(0, line.indexOf(" ")).trim());
  35. cols = Integer.parseInt(line.substring(line.indexOf(" "), line.length()).trim());
  36.  
  37. System.out.println("\nInput: ");
  38. System.out.print("rows: " + rows + " ");
  39. System.out.println("cols: " + cols);
  40.  
  41. matrix = new int[rows][cols];
  42.  
  43. for (int i = 0; i < matrix.length; i++) {
  44. String[] parts = br.readLine().split(" ");
  45. int[] n1 = new int[parts.length];
  46. for(int n = 0; n < parts.length; n++) {
  47. n1[n] = Integer.parseInt(parts[n].trim());
  48. System.out.print(n1[n] + " ");
  49. }
  50. System.out.println("");
  51. matrix[i] = n1;
  52. }
  53. System.out.println("\nOutput: ");
  54.  
  55. System.out.println(shortestPathWrapper(matrix, rows - 1, cols - 1));
  56. }
  57.  
  58. } catch (IOException e) {
  59. System.out.println("IO error");
  60. return;
  61. }
  62. finally {
  63. try {
  64. br.close();
  65. } catch (IOException e) {
  66. System.out.println("Error closing file.");
  67. return;
  68. }
  69. }
  70. }
  71.  
  72. public static int shortestPathWrapper(int[][] matrix, int rows, int columns){
  73. int min = Integer.MAX_VALUE;
  74. int[][] memo = new int[rows+1][columns+1];
  75. for (int i = 0; i < memo.length; i++) {
  76. for(int n = 0; n < memo[i].length; n++) {
  77. memo[i][n] = Integer.MAX_VALUE;
  78. }
  79. }
  80. for(int i = 0; i <= rows; i++){
  81. min = Math.min(min, shortestPath(matrix, i, rows, columns, 0, memo));
  82. }
  83. int[] path = new int[columns+1];
  84. int start = Integer.MAX_VALUE;
  85. for (int i = 0; i < memo.length; i++) {
  86. if(memo[i][0] == min)
  87. path[0] = i;
  88. }
  89. for(int i = 1; i <= columns; i++){
  90. path[i] = Math.min(Math.min(memo[path[i] - 1 < 0 ? rows : path[i] - 1][i+1],memo[path[i]][i+1]),
  91. memo[path[i] + 1 > rows ? 0 : path[i] + 1][i+1]);
  92. }
  93. for(int i = 0; i < path.length; i++){
  94. System.out.print(path[i] + " ");
  95. }
  96. return min;
  97. }
  98.  
  99.  
  100. public static int shortestPath(int[][] matrix, int currentRow, int rows, int columns, int currentCol, int[][] memo) {
  101. if(memo[currentRow][currentCol] != Integer.MAX_VALUE){
  102. return memo[currentRow][currentCol];
  103. }
  104. if (columns == currentCol) {
  105. return memo[currentRow][currentCol] = matrix[currentRow][currentCol];
  106. } else {
  107. int up = shortestPath(matrix, currentRow - 1 < 0 ? rows : currentRow - 1, rows, columns, currentCol + 1, memo);
  108. int mid = shortestPath(matrix, currentRow, rows, columns, currentCol + 1, memo);
  109. int down = shortestPath(matrix, currentRow + 1 > rows ? 0 : currentRow + 1, rows, columns, currentCol + 1, memo);
  110. return memo[currentRow][currentCol] = matrix[currentRow][currentCol] + Math.min(Math.min(up,mid), down);
  111. }
  112.  
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement