Source code for submission s970

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.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. int hops = 1;
  89. int marks = markNext(grid, startR, startC, hops);
  90.  
  91.  
  92. hops++;
  93. while (grid[endR][endC] == Integer.MAX_VALUE && marks > 0) {
  94. // print(grid);
  95. marks = 0;
  96.  
  97. for (int i = 0; i < rows; i++) {
  98. for (int j = 0; j < cols; j++) {
  99.  
  100. if (grid[i][j] == hops - 1) {
  101. marks += markNext(grid, i, j, hops);
  102. }
  103. }
  104. }
  105.  
  106. hops++;
  107. }
  108.  
  109. //print(grid);
  110.  
  111. if (marks == 0) {
  112. System.out.println("impossible");
  113. } else {
  114.  
  115. System.out.println(grid[endR][endC]);
  116. }
  117.  
  118.  
  119.  
  120.  
  121. inputLine = br.readLine();
  122. }
  123.  
  124. }
  125. }
  126.