Source code for submission s988

Go to diff to previous submission

Grasshop.java

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

Diff to submission s785

Grasshop.java

--- c4.s785.cteam094.grasshop.java.0.Grasshop.java
+++ c4.s988.cteam094.grasshop.java.0.Grasshop.java
@@ -1,3 +1,5 @@
 import java.util.Scanner;
+import java.util.List;
+import java.util.LinkedList;
 
 public class Grasshop {
@@ -21,4 +23,6 @@
                                 ly = sc.nextInt() - 1;
 
+                                List<Position> list = new LinkedList<Position>();
+
 
                                 int[][] map = new int[x][y];
@@ -29,14 +33,27 @@
                                 }
                                 map[gx][gy] = 0;
+                                list.add(new Position(gx, gy));
+
+                                Position p;
+
+
                                 
-                                boolean change = true;
-                                while (change) {
                                 
-                                        change = false;         
-                                        for (int i = 0; i < x; i++) {
-                                                for (int j = 0; j < y; j++) {
-                                                        change = jump(map, i, j) || change;     
+                                while (true) {
+                                
+                                        
+                                        
+                                        p = list.get(0);
+                                        
+                                                                                        
+                                        jump(map, p.x, p.y, list);
 
-                                                }
+                                        list.remove(p); 
+                                                
+                                        if (list.isEmpty()) {
+                                                break;
+                                        }
+                                        if (map[lx][ly] < Integer.MAX_VALUE) {
+                                                break;
                                         }       
 
@@ -76,16 +93,13 @@
         }
 
-        public static boolean jump(int[][] map, int a, int b) {
+        public static void jump(int[][] map, int a, int b, List<Position> list) {
                 int dx = 0;
                 int dy = 0;
 
-                boolean change = false;
+                
 
                 //System.out.println("Solving.....   "  + a + ", " + b) ;
 
-                if (map[a][b] == Integer.MAX_VALUE) {
-                        return false;
-                        
-                }
+        
 
                 
@@ -108,5 +122,6 @@
                                         map[newX][newY] = map[a][b] + 1;
                                         //System.out.println(a + ", " + b + " changed ") ;
-                                        change = true;
+                                
+                                        list.add(new Position(newX, newY));
                                 }       
                         } 
@@ -115,5 +130,5 @@
                 }
 
-                return change;
+                
 
         }
@@ -126,5 +141,17 @@
                 return valid;   
         }
-}                       
+}       
+
+class Position {
+        int x;
+        int y;
+
+        Position(int x, int y) {
+                this.x = x;
+                this.y = y;
+        }               
+
+
+}