Source code for submission s997

Go to diff to previous submission

Grasshop.java

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6.  
  7.  
  8. public class Grasshop {
  9.  
  10. private static int [][] pole;
  11. private static int x0, y0;
  12. public static void main(String[] args) throws Exception {
  13. String str;
  14. String[] radek;
  15. LinkedList<Boda> list = new LinkedList<Boda>();
  16. int gx,gy,lx,ly;
  17.  
  18. while((str = br.readLine()) != null){
  19. radek = str.split(" ");
  20. x0 = Integer.parseInt(radek[0]);
  21. y0 = Integer.parseInt(radek[1]);
  22. gx = Integer.parseInt(radek[2]);
  23. gy = Integer.parseInt(radek[3]);
  24. lx = Integer.parseInt(radek[4]);
  25. ly = Integer.parseInt(radek[5]);
  26.  
  27. pole = new int [x0][y0];
  28.  
  29. Boda b = new Boda(gx-1, gy-1, 1);
  30. list.clear();
  31. list.addLast(b);
  32.  
  33. int vzdal = -1;
  34. do
  35. {
  36. Boda a = list.getFirst();
  37. list.removeFirst();
  38. int x = a.x;
  39. int y = a.y;
  40.  
  41. if (x == lx-1 && y == ly-1)
  42. {
  43. vzdal = a.v-1;
  44. break;
  45. }
  46.  
  47. if (isValid(x+1, y+2))
  48. {
  49. list.addLast(new Boda(x+1, y+2, a.v+1));
  50. pole[x+1][y+2] = a.v+1;
  51. }
  52. if (isValid(x+1, y-2))
  53. {
  54. list.addLast(new Boda(x+1, y-2, a.v+1));
  55. pole[x+1][y-2] = a.v+1;
  56. }
  57. if (isValid(x-1, y+2))
  58. {
  59. list.addLast(new Boda(x-1, y+2, a.v+1));
  60. pole[x-1][y+2] = a.v+1;
  61. }
  62. if (isValid(x-1, y-2))
  63. {
  64. list.addLast(new Boda(x-1, y-2, a.v+1));
  65. pole[x-1][y-2] = a.v+1;
  66. }
  67. if (isValid(x+2, y+1))
  68. {
  69. list.addLast(new Boda(x+2, y+1, a.v+1));
  70. pole[x+2][y+1] = a.v+1;
  71. }
  72. if (isValid(x+2, y-1))
  73. {
  74. list.addLast(new Boda(x+2, y-1, a.v+1));
  75. pole[x+2][y-1] = a.v+1;
  76. }
  77. if (isValid(x-2, y+1))
  78. {
  79. list.addLast(new Boda(x-2, y+1, a.v+1));
  80. pole[x-2][y+1] = a.v+1;
  81. }
  82. if (isValid(x-2, y-1))
  83. {
  84. list.addLast(new Boda(x-2, y-1, a.v+1));
  85. pole[x-2][y-1] = a.v+1;
  86. }
  87.  
  88. } while(!list.isEmpty());
  89.  
  90.  
  91.  
  92. System.out.println(vzdal == -1 ? "impossible" : vzdal);
  93. }
  94.  
  95. }
  96.  
  97. private static boolean isValid(int x, int y)
  98. {
  99. return x >= 0 &&
  100. x < x0 &&
  101. y >= 0 &&
  102. y < y0 &&
  103. pole[x][y] == 0;
  104. }
  105.  
  106. }
  107.  
  108.  
  109. class Boda
  110. {
  111. public int x;
  112. public int y;
  113. public int v;
  114.  
  115. public Boda(int x, int y, int v)
  116. {
  117. this.x = x;
  118. this.y = y;
  119. this.v = v;
  120. }
  121. }

Diff to submission s766

Grasshop.java

--- c4.s766.cteam115.grasshop.java.0.Grasshop.java
+++ c4.s997.cteam115.grasshop.java.0.Grasshop.java
@@ -2,4 +2,6 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.HashSet;
+import java.util.LinkedList;
 
 
@@ -7,14 +9,16 @@
 
         private static int [][] pole;
+        private static int x0, y0;
         public static void main(String[] args) throws Exception {
                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 String str;
                 String[] radek;
-                int x,y,gx,gy,lx,ly;
+                LinkedList<Boda> list = new LinkedList<Boda>();
+                int gx,gy,lx,ly;
                 
                 while((str = br.readLine()) != null){
                         radek = str.split(" ");
-                        x = Integer.parseInt(radek[0]);
-                        y = Integer.parseInt(radek[1]);
+                        x0 = Integer.parseInt(radek[0]);
+                        y0 = Integer.parseInt(radek[1]);
                         gx = Integer.parseInt(radek[2]);
                         gy = Integer.parseInt(radek[3]);
@@ -22,44 +26,97 @@
                         ly = Integer.parseInt(radek[5]);
                         
-                        pole = new int [x][y];
+                        pole = new int [x0][y0];
                         
-                        rek(gx-1, gy-1, 1);
-                        System.out.println(pole[lx-1][ly-1] == 0 ? "impossible" : pole[lx-1][ly-1]-1);
+                        Boda b = new Boda(gx-1, gy-1, 1);
+                        list.clear();
+                        list.addLast(b);
+
+                        int vzdal = -1;
+                        do
+                        {
+                                Boda a = list.getFirst();
+                                list.removeFirst();
+                                int x = a.x;
+                                int y = a.y;
+
+                                if (x == lx-1 && y == ly-1)
+                                {
+                                        vzdal = a.v-1;
+                                        break;
+                                }
+                                
+                                if (isValid(x+1, y+2))
+                                {
+                                        list.addLast(new Boda(x+1, y+2, a.v+1));
+                                        pole[x+1][y+2] = a.v+1;
+                                }
+                                if (isValid(x+1, y-2))
+                                {
+                                        list.addLast(new Boda(x+1, y-2, a.v+1));
+                                        pole[x+1][y-2] = a.v+1;
+                                }
+                                if (isValid(x-1, y+2))
+                                {
+                                        list.addLast(new Boda(x-1, y+2, a.v+1));
+                                        pole[x-1][y+2] = a.v+1;
+                                }
+                                if (isValid(x-1, y-2))
+                                {
+                                        list.addLast(new Boda(x-1, y-2, a.v+1));
+                                        pole[x-1][y-2] = a.v+1;
+                                }
+                                if (isValid(x+2, y+1))
+                                {
+                                        list.addLast(new Boda(x+2, y+1, a.v+1));
+                                        pole[x+2][y+1] = a.v+1;
+                                }
+                                if (isValid(x+2, y-1))
+                                {
+                                        list.addLast(new Boda(x+2, y-1, a.v+1));
+                                        pole[x+2][y-1] = a.v+1;
+                                }
+                                if (isValid(x-2, y+1))
+                                {
+                                        list.addLast(new Boda(x-2, y+1, a.v+1));
+                                        pole[x-2][y+1] = a.v+1;
+                                }
+                                if (isValid(x-2, y-1))
+                                {
+                                        list.addLast(new Boda(x-2, y-1, a.v+1));
+                                        pole[x-2][y-1] = a.v+1;
+                                }
+                                        
+                        } while(!list.isEmpty());
+                        
+                        
+                        
+                        System.out.println(vzdal == -1 ? "impossible" : vzdal);
                 }
                 
         }
         
-        private static void rek(int x, int y, int cena)
-        {
-                if (pole[x][y] == 0 || pole[x][y] > cena)
-                        pole[x][y] = cena;
-                else
-                        return;
-                //System.out.println(x+" "+y+" "+cena);
-                if (isValid(x+1, y+2))
-                        rek(x+1, y+2, cena+1);
-                if (isValid(x+1, y-2))
-                        rek(x+1, y-2, cena+1);
-                if (isValid(x-1, y+2))
-                        rek(x-1, y+2, cena+1);
-                if (isValid(x-1, y-2))
-                        rek(x-1, y-2, cena+1);
-                if (isValid(x+2, y+1))
-                        rek(x+2, y+1, cena+1);
-                if (isValid(x+2, y-1))
-                        rek(x+2, y-1, cena+1);
-                if (isValid(x-2, y+1))
-                        rek(x-2, y+1, cena+1);
-                if (isValid(x-2, y-1))
-                        rek(x-2, y-1, cena+1);
-        }
-        
         private static boolean isValid(int x, int y)
         {
-                return  x >= 0 &&
-                                x < pole.length &&
+                return  x >= 0 &&
+                                x < x0 &&
                                 y >= 0 &&
-                                y < pole[0].length;
+                                y < y0 &&
+                                pole[x][y] == 0;
         }
 
 }
+
+
+class Boda
+{
+        public int x;
+        public int y;
+        public int v;
+        
+        public Boda(int x, int y, int v)
+        {
+                this.x = x;
+                this.y = y;
+                this.v = v;
+        }
+}
\ No newline at end of file