Source code for submission s1158

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. int rozX = cX-sX;
  81. int rozY = cY-sY;
  82.  
  83. if (Math.abs(rozX) + Math.abs(rozY) < 5) {
  84. pocetSkoku = Integer.MAX_VALUE;
  85. break;
  86. }
  87.  
  88. if (Math.abs(rozX) > Math.abs(rozY)) {
  89. int skX, skY;
  90.  
  91. if (rozX > 0) skX = 2;
  92. else skX = -2;
  93.  
  94. if (rozY == 0) {
  95. if ((sY-1) < 0) skY = 1;
  96. else skY = -1;
  97. } else {
  98. if (rozY > 0) skY = 1;
  99. else skY = -1;
  100. }
  101.  
  102. sX += skX;
  103. sY += skY;
  104.  
  105. } else {
  106. int skX, skY;
  107.  
  108. if (rozY > 0) skY = 2;
  109. else skY = -2;
  110.  
  111. if (rozX == 0) {
  112. if ((sX-1) < 0) skX = 1;
  113. else skX = -1;
  114. } else {
  115. if (rozX > 0) skX = 1;
  116. else skX = -1;
  117. }
  118.  
  119. sX += skX;
  120. sY += skY;
  121. }
  122.  
  123. }
  124.  
  125. if (pocetSkoku == Integer.MAX_VALUE) {
  126. System.out.println("impossible");
  127. } else {
  128. pocetSkoku += pole[sX][sY];
  129. System.out.println(pocetSkoku);
  130. }
  131. /*
  132. for(int i=0; i<maxY; i++) {
  133. for(int j=0; j<maxX; j++) {
  134. System.out.print(pole[j][i]);
  135. }
  136. System.out.println();
  137. }
  138. */
  139. } catch (IOException e) {
  140. break;
  141. }
  142. }
  143.  
  144. }
  145. }
  146.  

Diff to submission s1143

Grasshop.java

--- c4.s1143.cteam113.grasshop.java.0.Grasshop.java
+++ c4.s1158.cteam113.grasshop.java.0.Grasshop.java
@@ -78,9 +78,8 @@
                                 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) < 2) {
+                                        if (Math.abs(rozX) + Math.abs(rozY) < 5) {
                                                 pocetSkoku = Integer.MAX_VALUE;
                                                 break;