Source code for submission s1050

Go to diff to previous submission

Grasshop.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. /*
  7.  * To change this template, choose Tools | Templates and open the template in
  8.  * the editor.
  9.  */
  10. /**
  11.  *
  12.  * @author cteam066
  13.  */
  14. public class Grasshop {
  15.  
  16. public static boolean inGrid(int[][] grid, int col, int row) {
  17. return (col >= 0 && col < grid[0].length && row >= 0 && row < grid.length);
  18. }
  19.  
  20. public static int markNext(int[][] grid, int col, int row, int hops) {
  21.  
  22. int count = 0;
  23. int[] dx = new int[]{1, 2, 2, 1, -1, -2, -2, -1};
  24. int[] dy = new int[]{-2, -1, 1, 2, 2, 1, -1, -2};
  25.  
  26. for (int i = 0; i < 8; i++) {
  27. int col1 = col + dy[i];
  28. int row1 = row + dx[i];
  29.  
  30. if (inGrid(grid, col1, row1) && (grid[row1][col1] == 0 || grid[row1][col1] == Integer.MAX_VALUE)) {
  31. count++;
  32. grid[row1][col1] = hops;
  33. }
  34. }
  35. return count;
  36.  
  37.  
  38.  
  39. }
  40.  
  41. public static void print(int[][] grid) {
  42. for (int i = 0; i < grid.length; i++) {
  43. for (int j = 0; j < grid[0].length; j++) {
  44.  
  45. if (grid[i][j] == -1) {
  46. System.out.print("*");
  47. } else {
  48.  
  49. if (grid[i][j] == Integer.MAX_VALUE) {
  50. System.out.print("#");
  51. } else {
  52.  
  53. System.out.print(grid[i][j]);
  54. }
  55. }
  56. }
  57. System.out.println("");
  58.  
  59. }
  60. System.out.println("");
  61. }
  62.  
  63. /**
  64.   * @param args the command line arguments
  65.   */
  66. public static void main(String[] args) throws IOException {
  67.  
  68.  
  69. String inputLine = br.readLine();
  70.  
  71. while (inputLine != null) {
  72.  
  73.  
  74. String[] input = inputLine.split(" ");
  75.  
  76. int rows = Integer.parseInt(input[0]);
  77. int cols = Integer.parseInt(input[1]);
  78. int startR = Integer.parseInt(input[2]) - 1;
  79. int startC = Integer.parseInt(input[3]) - 1;
  80. int endR = Integer.parseInt(input[4]) - 1;
  81. int endC = Integer.parseInt(input[5]) - 1;
  82.  
  83.  
  84. int[][] grid = new int[rows][cols];
  85. grid[startR][startC] = -1;
  86. grid[endR][endC] = Integer.MAX_VALUE;
  87.  
  88.  
  89. if (startR == endR && startC == endC) {
  90.  
  91. System.out.println(0);
  92. inputLine = br.readLine();
  93. continue;
  94. }
  95.  
  96. int hops = 1;
  97. int marks = markNext(grid, startC, startR, hops);
  98.  
  99. while (grid[endR][endC] == Integer.MAX_VALUE && marks > 0) {
  100. // print(grid);
  101. hops++;
  102. marks = 0;
  103.  
  104. for (int i = 0; i < rows; i++) {
  105. for (int j = 0; j < cols; j++) {
  106.  
  107. if (grid[i][j] == hops - 1) {
  108. marks += markNext(grid, j, i, hops);
  109. }
  110. }
  111. }
  112. }
  113.  
  114. //print(grid);
  115.  
  116. if (marks == 0) {
  117. System.out.println("impossible");
  118. } else {
  119.  
  120. System.out.println(grid[endR][endC]);
  121. }
  122.  
  123.  
  124.  
  125.  
  126. inputLine = br.readLine();
  127. }
  128.  
  129. }
  130. }
  131.  

Diff to submission s1048

Grasshop.java

--- c4.s1048.cteam095.grasshop.java.0.Grasshop.java
+++ c4.s1050.cteam095.grasshop.java.0.Grasshop.java
@@ -96,5 +96,5 @@
 
             int hops = 1;
-            int marks = markNext(grid, startR, startC, hops);
+            int marks = markNext(grid, startC, startR, hops);
 
             while (grid[endR][endC] == Integer.MAX_VALUE && marks > 0) {