Source code for submission s967

Go to diff to previous submission

grasshop.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Scanner;
  10.  
  11.  
  12. /**
  13.  *
  14.  * @author cteam041
  15.  */
  16. public class grasshop {
  17. public static void main(String[] args) {
  18.  
  19. int R, C, Gr, Gc, Lr, Lc;
  20.  
  21. Scanner sc = new Scanner(System.in);
  22. while (sc.hasNextInt()) {
  23. List<Pos> q = new LinkedList<Pos>();
  24. R = sc.nextInt();
  25. C = sc.nextInt();
  26.  
  27. Gr = sc.nextInt()-1;
  28. Gc = sc.nextInt()-1;
  29.  
  30. Lr = sc.nextInt()-1;
  31. Lc = sc.nextInt()-1;
  32. Pos start = new Pos();
  33. start.r = Gr;
  34. start.c = Gc;
  35. start.iter = 1;
  36. q.add(start);
  37.  
  38. int[][] pole = new int[C][R];
  39. pole[Gc][Gr] = 1;
  40.  
  41. int iter = 1;
  42. boolean found = false;
  43. while (q.size() > 0) {
  44.  
  45. Pos k = q.get(0);
  46. q.remove(0);
  47.  
  48. iter = k.iter;
  49.  
  50. if (Lr == k.r && Lc == k.c) {
  51. found = true;
  52. System.out.println(k.iter-1);
  53. break;
  54. }
  55.  
  56. int i = k.c;
  57. int j = k.r;
  58.  
  59. int x = i + 2;
  60. int y = j + 1;
  61. if (x >= 0 && x < C && y >= 0 && y < R) {
  62. if (pole[x][y] == 0) {
  63. Pos n = new Pos();
  64. n.c = x; n.r = y; n.iter = iter+1;
  65. q.add(n);
  66. pole[x][y]=n.iter;
  67.  
  68. }
  69. }
  70.  
  71. x = i - 2;
  72. y = j + 1;
  73. if (x >= 0 && x < C && y >= 0 && y < R) {
  74. if (pole[x][y] == 0) {
  75. Pos n = new Pos();
  76. n.c = x; n.r = y; n.iter = iter+1;
  77. q.add(n);
  78. pole[x][y]=n.iter;
  79.  
  80. }
  81. }
  82.  
  83. x = i - 2;
  84. y = j - 1;
  85. if (x >= 0 && x < C && y >= 0 && y < R) {
  86. if (pole[x][y] == 0) {
  87. Pos n = new Pos();
  88. n.c = x; n.r = y; n.iter = iter+1;
  89. q.add(n);
  90. pole[x][y]=n.iter;
  91.  
  92. }
  93. }
  94.  
  95. x = i + 2;
  96. y = j - 1;
  97. if (x >= 0 && x < C && y >= 0 && y < R) {
  98. if (pole[x][y] == 0) {
  99. Pos n = new Pos();
  100. n.c = x; n.r = y; n.iter = iter+1;
  101. q.add(n);
  102. pole[x][y]=n.iter;
  103.  
  104. }
  105. }
  106.  
  107. //opacnefound?:"
  108. x = i + 1;
  109. y = j + 2;
  110. if (x >= 0 && x < C && y >= 0 && y < R) {
  111. if (pole[x][y] == 0) {
  112. Pos n = new Pos();
  113. n.c = x; n.r = y; n.iter = iter+1;
  114. q.add(n);
  115. pole[x][y]=n.iter;
  116.  
  117. }
  118. }
  119.  
  120. x = i - 1;
  121. y = j + 2;
  122. if (x >= 0 && x < C && y >= 0 && y < R) {
  123. if (pole[x][y] == 0) {
  124. Pos n = new Pos();
  125. n.c = x; n.r = y; n.iter = iter+1;
  126. q.add(n);
  127. pole[x][y]=n.iter;
  128.  
  129. }
  130. }
  131.  
  132. x = i - 1;
  133. y = j - 2;
  134. if (x >= 0 && x < C && y >= 0 && y < R) {
  135. if (pole[x][y] == 0) {
  136. Pos n = new Pos();
  137. n.c = x; n.r = y; n.iter = iter+1;
  138. q.add(n);
  139. pole[x][y]=n.iter;
  140.  
  141. }
  142. }
  143.  
  144. x = i + 1;
  145. y = j - 2;
  146. if (x >= 0 && x < C && y >= 0 && y < R) {
  147. if (pole[x][y] == 0) {
  148. Pos n = new Pos();
  149. n.c = x; n.r = y; n.iter = iter+1;
  150. q.add(n);
  151. pole[x][y]=n.iter;
  152.  
  153. }
  154. }
  155.  
  156. }
  157.  
  158. if (!found)
  159. System.out.println("impossible");
  160.  
  161. }
  162.  
  163. }
  164.  
  165. private static class Pos {
  166.  
  167. public int r, c, iter;
  168. }
  169. }
  170.  
  171.  

Diff to submission s950

grasshop.java

--- c4.s950.cteam041.grasshop.java.0.grasshop.java
+++ c4.s967.cteam041.grasshop.java.0.grasshop.java
@@ -36,5 +36,5 @@
             q.add(start);
 
-            int[][] pole = new int[R][C];
+            int[][] pole = new int[C][R];
             pole[Gc][Gr] = 1;