Source code for submission s1143

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 >= 5) 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. while (true) {
  37.  
  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. maxX = Integer.parseInt(ln[0]);
  46. maxY = Integer.parseInt(ln[1]);
  47. int sX = Integer.parseInt(ln[2])-1;
  48. int sY = Integer.parseInt(ln[3])-1;
  49. int cX = Integer.parseInt(ln[4])-1;
  50. int cY = Integer.parseInt(ln[5])-1;
  51.  
  52. if ((sX == cX) && (sY == cY)) {
  53. System.out.println("0");
  54. continue;
  55. }
  56.  
  57. if ((maxX < 2) || (maxY < 2)) {
  58. System.out.println("impossible");
  59. continue;
  60. }
  61.  
  62. int[][] pole = new int[maxX][maxY];
  63.  
  64. rekurze(pole, cX, cY, 0);
  65.  
  66. pole[cX][cY] = 9;
  67. //pole[sX][sY] = 7;
  68. /*
  69. for(int i=0; i<maxY; i++) {
  70. for(int j=0; j<maxX; j++) {
  71. System.out.print(pole[j][i]);
  72. }
  73. System.out.println();
  74. }
  75. */
  76. int pocetSkoku = 0;
  77.  
  78. while (pole[sX][sY] == 0) {
  79. pocetSkoku++;
  80. pole[sX][sY] = 8;
  81. int rozX = cX-sX;
  82. int rozY = cY-sY;
  83.  
  84. if (Math.abs(rozX) + Math.abs(rozY) < 2) {
  85. pocetSkoku = Integer.MAX_VALUE;
  86. break;
  87. }
  88.  
  89. if (Math.abs(rozX) > Math.abs(rozY)) {
  90. int skX, skY;
  91.  
  92. if (rozX > 0) skX = 2;
  93. else skX = -2;
  94.  
  95. if (rozY == 0) {
  96. if ((sY-1) < 0) skY = 1;
  97. else skY = -1;
  98. } else {
  99. if (rozY > 0) skY = 1;
  100. else skY = -1;
  101. }
  102.  
  103. sX += skX;
  104. sY += skY;
  105.  
  106. } else {
  107. int skX, skY;
  108.  
  109. if (rozY > 0) skY = 2;
  110. else skY = -2;
  111.  
  112. if (rozX == 0) {
  113. if ((sX-1) < 0) skX = 1;
  114. else skX = -1;
  115. } else {
  116. if (rozX > 0) skX = 1;
  117. else skX = -1;
  118. }
  119.  
  120. sX += skX;
  121. sY += skY;
  122. }
  123.  
  124. }
  125.  
  126. if (pocetSkoku == Integer.MAX_VALUE) {
  127. System.out.println("impossible");
  128. } else {
  129. pocetSkoku += pole[sX][sY];
  130. System.out.println(pocetSkoku);
  131. }
  132. /*
  133. for(int i=0; i<maxY; i++) {
  134. for(int j=0; j<maxX; j++) {
  135. System.out.print(pole[j][i]);
  136. }
  137. System.out.println();
  138. }
  139. */
  140. } catch (IOException e) {
  141. break;
  142. }
  143. }
  144.  
  145. }
  146. }
  147.  

Diff to submission s1132

Grasshop.java

--- c4.s1132.cteam113.grasshop.java.0.Grasshop.java
+++ c4.s1143.cteam113.grasshop.java.0.Grasshop.java
@@ -3,6 +3,4 @@
 import java.io.InputStreamReader;
 
-import org.omg.PortableInterceptor.INACTIVE;
-
 
 public class Grasshop {
@@ -43,4 +41,5 @@
                                 if (linka == null) break;
                                 String ln[] = linka.split(" ");
+                                if (ln.length < 6) break;
 
                                 maxX = Integer.parseInt(ln[0]);
@@ -140,4 +139,5 @@
 */
                         } catch (IOException e) {
+                                break;
                         }
                 }