Source code for submission s785

Go to diff to previous submission

Grasshop.java

  1. import java.util.Scanner;
  2.  
  3. public class Grasshop {
  4.  
  5. static int x;
  6. static int y;
  7. static int gx;
  8. static int gy;
  9. static int lx;
  10. static int ly;
  11.  
  12. public static void main(String[] args) throws Exception {
  13. Scanner sc = new Scanner(System.in);
  14. while (sc.hasNextInt()) {
  15.  
  16. x = sc.nextInt();
  17. y = sc.nextInt();
  18. gx = sc.nextInt() - 1;
  19. gy = sc.nextInt() - 1;
  20. lx = sc.nextInt() - 1;
  21. ly = sc.nextInt() - 1;
  22.  
  23.  
  24. int[][] map = new int[x][y];
  25. for (int i = 0; i < x; i++) {
  26. for (int j = 0; j < y; j++) {
  27. map[i][j] = Integer.MAX_VALUE;
  28. }
  29. }
  30. map[gx][gy] = 0;
  31.  
  32. boolean change = true;
  33. while (change) {
  34.  
  35. change = false;
  36. for (int i = 0; i < x; i++) {
  37. for (int j = 0; j < y; j++) {
  38. change = jump(map, i, j) || change;
  39.  
  40. }
  41. }
  42.  
  43. /*
  44. if (map[lx][ly] < Integer.MAX_VALUE) {
  45. change = false;
  46. System.out.println(map[lx][ly]);
  47.  
  48. }
  49.  
  50.  
  51. for (int j = 0; j < y; j++) {
  52. for (int i = 0; i < x; i++) {
  53. if (map[i][j] == Integer.MAX_VALUE) {
  54. System.out.print(" - ");
  55. } else {
  56. System.out.print(map[i][j] + " ");
  57. }
  58. }
  59. System.out.println();
  60. }
  61. System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  62. */
  63.  
  64. }
  65. if (map[lx][ly] == Integer.MAX_VALUE) {
  66. System.out.println("impossible");
  67. }
  68. if (map[lx][ly] < Integer.MAX_VALUE) {
  69.  
  70. System.out.println(map[lx][ly]);
  71.  
  72. }
  73.  
  74.  
  75. }
  76. }
  77.  
  78. public static boolean jump(int[][] map, int a, int b) {
  79. int dx = 0;
  80. int dy = 0;
  81.  
  82. boolean change = false;
  83.  
  84. //System.out.println("Solving..... " + a + ", " + b) ;
  85.  
  86. if (map[a][b] == Integer.MAX_VALUE) {
  87. return false;
  88.  
  89. }
  90.  
  91.  
  92.  
  93. for (int k=0; k < 8; k++) {
  94. if (k == 0) {dx = -2; dy = -1;}
  95. if (k == 1) {dx = -2; dy = 1;}
  96. if (k == 2) {dx = -1; dy = -2;}
  97. if (k == 3) {dx = -1; dy = 2;}
  98. if (k == 4) {dx = 1; dy = -2;}
  99. if (k == 5) {dx = 1; dy = 2;}
  100. if (k == 6) {dx = 2; dy = -1;}
  101. if (k == 7) {dx = 2; dy = 1;}
  102.  
  103.  
  104. int newX = a + dx;
  105. int newY = b + dy;
  106. if (isValid(newX, newY)) {
  107. if (map[a][b] + 1 < map[newX][newY]) {
  108. map[newX][newY] = map[a][b] + 1;
  109. //System.out.println(a + ", " + b + " changed ") ;
  110. change = true;
  111. }
  112. }
  113.  
  114.  
  115. }
  116.  
  117. return change;
  118.  
  119. }
  120.  
  121. public static boolean isValid(int a, int b) {
  122.  
  123. boolean valid = (a >= 0 && a < x && b >=0 && b < y);
  124.  
  125. //System.out.println(a + ", " + b + " : " + valid);
  126. return valid;
  127. }
  128. }
  129.  
  130.  
  131.  
  132.  

Diff to submission s765

Grasshop.java

--- c4.s765.cteam094.grasshop.java.0.Grasshop.java
+++ c4.s785.cteam094.grasshop.java.0.Grasshop.java
@@ -41,4 +41,5 @@
                                         }       
 
+/*
                                         if (map[lx][ly] < Integer.MAX_VALUE) {
                                                 change = false;
@@ -47,5 +48,5 @@
                                         }
 
-                                        /*
+                                        
                                         for (int j = 0; j < y; j++) {
                                                 for (int i = 0; i < x; i++) {
@@ -65,4 +66,10 @@
                                         System.out.println("impossible");
                                 }
+                                        if (map[lx][ly] < Integer.MAX_VALUE) {
+                                                
+                                                System.out.println(map[lx][ly]);
+                                                
+                                        }
+
                         
                 }