Source code for submission s1132

Grasshop.java

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