Source code for submission s1253

Go to diff to previous submission

Grasshop.java

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5.  
  6. public class Grasshop {
  7. private static BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9. public static int maxX;
  10. public static int maxY;
  11.  
  12. public static void rekurze(int[][] pole, int x, int y, int hodnota) {
  13. if (hodnota > 6) return;
  14. if (x<0) return;
  15. if (x>=maxX) return;
  16. if (y<0) return;
  17. if (y>=maxY) return;
  18.  
  19. else {
  20. if ((pole[x][y] > hodnota) || (pole[x][y] == 0)) {
  21. pole[x][y] = hodnota;
  22. rekurze(pole, x+2, y+1, hodnota+1);
  23. rekurze(pole, x-2, y+1, hodnota+1);
  24. rekurze(pole, x+2, y-1, hodnota+1);
  25. rekurze(pole, x-2, y-1, hodnota+1);
  26. rekurze(pole, x+1, y+2, hodnota+1);
  27. rekurze(pole, x-1, y+2, hodnota+1);
  28. rekurze(pole, x+1, y-2, hodnota+1);
  29. rekurze(pole, x-1, y-2, hodnota+1);
  30. }
  31. }
  32.  
  33. }
  34.  
  35. public static void main(String[] args) {
  36. boolean prvni = true;
  37. while (true) {
  38. try {
  39. String linka;
  40. linka = bfr.readLine();
  41. if (linka == null) break;
  42. String ln[] = linka.split(" ");
  43. if (ln.length < 6) break;
  44.  
  45. if (!prvni) System.out.println();
  46. prvni = false;
  47.  
  48. maxX = Integer.parseInt(ln[0]);
  49. maxY = Integer.parseInt(ln[1]);
  50. int sX = Integer.parseInt(ln[2])-1;
  51. int sY = Integer.parseInt(ln[3])-1;
  52. int cX = Integer.parseInt(ln[4])-1;
  53. int cY = Integer.parseInt(ln[5])-1;
  54.  
  55. if ((sX == cX) && (sY == cY)) {
  56. System.out.print("0");
  57. continue;
  58. }
  59.  
  60. if ((maxX < 2) || (maxY < 2)) {
  61. System.out.print("impossible");
  62. continue;
  63. }
  64.  
  65. int[][] pole = new int[maxX][maxY];
  66.  
  67. rekurze(pole, cX, cY, 0);
  68.  
  69. pole[cX][cY] = 9;
  70. //pole[sX][sY] = 7;
  71. /*
  72. for(int i=0; i<maxY; i++) {
  73. for(int j=0; j<maxX; j++) {
  74. System.out.print(pole[j][i]+" ");
  75. }
  76. System.out.println();
  77. }
  78. */
  79. int pocetSkoku = 0;
  80.  
  81. while (pole[sX][sY] == 0) {
  82. pocetSkoku++;
  83. pole[sX][sY] = 8;
  84. int rozX = cX-sX;
  85. int rozY = cY-sY;
  86.  
  87. if (Math.abs(rozX) + Math.abs(rozY) < 4) {
  88. pocetSkoku = Integer.MAX_VALUE;
  89. break;
  90. }
  91.  
  92. if (Math.abs(rozX) > Math.abs(rozY)) {
  93. int skX, skY;
  94.  
  95. if (rozX > 0) skX = 2;
  96. else skX = -2;
  97.  
  98. if (rozY == 0) {
  99. if ((sY-1) < 0) skY = 1;
  100. else skY = -1;
  101. } else {
  102. if (rozY > 0) skY = 1;
  103. else skY = -1;
  104. }
  105.  
  106. sX += skX;
  107. sY += skY;
  108.  
  109. } else {
  110. int skX, skY;
  111.  
  112. if (rozY > 0) skY = 2;
  113. else skY = -2;
  114.  
  115. if (rozX == 0) {
  116. if ((sX-1) < 0) skX = 1;
  117. else skX = -1;
  118. } else {
  119. if (rozX > 0) skX = 1;
  120. else skX = -1;
  121. }
  122.  
  123. sX += skX;
  124. sY += skY;
  125. }
  126.  
  127. }
  128.  
  129. if (pocetSkoku == Integer.MAX_VALUE) {
  130. System.out.print("impossible");
  131. } else {
  132. pocetSkoku += pole[sX][sY];
  133. System.out.print(pocetSkoku);
  134. }
  135. /*
  136. System.out.println();
  137. for(int i=0; i<maxY; i++) {
  138. for(int j=0; j<maxX; j++) {
  139. System.out.print(pole[j][i]+" ");
  140. }
  141. System.out.println();
  142. }
  143. */
  144. } catch (IOException e) {
  145. break;
  146. }
  147.  
  148. }
  149. }
  150. }
  151.  

Diff to submission s1158

Grasshop.java

--- c4.s1158.cteam113.grasshop.java.0.Grasshop.java
+++ c4.s1253.cteam113.grasshop.java.0.Grasshop.java
@@ -11,5 +11,5 @@
 
         public static void rekurze(int[][] pole, int x, int y, int hodnota) {
-                if (hodnota >= 5) return;
+                if (hodnota > 6) return;
                 if (x<0) return;
                 if (x>=maxX) return;
@@ -34,6 +34,6 @@
 
         public static void main(String[] args) {
+                boolean prvni = true;
                 while (true) {
-
                         try {
                                 String linka;
@@ -42,4 +42,7 @@
                                 String ln[] = linka.split(" ");
                                 if (ln.length < 6) break;
+                                
+                                if (!prvni) System.out.println();
+                                prvni = false;
 
                                 maxX = Integer.parseInt(ln[0]);
@@ -51,10 +54,10 @@
 
                                 if ((sX == cX) && (sY == cY)) {
-                                        System.out.println("0");
+                                        System.out.print("0");
                                         continue;
                                 }
                                 
                                 if ((maxX < 2) || (maxY < 2)) {
-                                        System.out.println("impossible");
+                                        System.out.print("impossible");
                                         continue;
                                 }
@@ -69,5 +72,5 @@
                                 for(int i=0; i<maxY; i++) {
                                         for(int j=0; j<maxX; j++) {
-                                                System.out.print(pole[j][i]);
+                                                System.out.print(pole[j][i]+" ");
                                         }
                                         System.out.println();
@@ -78,8 +81,9 @@
                                 while (pole[sX][sY] == 0) {
                                         pocetSkoku++;
+                                        pole[sX][sY] = 8;
                                         int rozX = cX-sX;
                                         int rozY = cY-sY;
 
-                                        if (Math.abs(rozX) + Math.abs(rozY) < 5) {
+                                        if (Math.abs(rozX) + Math.abs(rozY) < 4) {
                                                 pocetSkoku = Integer.MAX_VALUE;
                                                 break;
@@ -124,13 +128,14 @@
 
                                 if (pocetSkoku == Integer.MAX_VALUE) {
-                                        System.out.println("impossible");
+                                        System.out.print("impossible");
                                 } else {
                                         pocetSkoku += pole[sX][sY];
-                                        System.out.println(pocetSkoku);
+                                        System.out.print(pocetSkoku);
                                 }
 /*
+                                System.out.println();
                                 for(int i=0; i<maxY; i++) {
                                         for(int j=0; j<maxX; j++) {
-                                                System.out.print(pole[j][i]);
+                                                System.out.print(pole[j][i]+" ");
                                         }
                                         System.out.println();
@@ -140,6 +145,6 @@
                                 break;
                         }
+                        
                 }
-
         }
 }