Source code for submission s777

Go to diff to previous submission

Grasshopper.java

  1.  
  2. import java.io.BufferedReader;
  3.  
  4. /*
  5.  * To change this template, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.util.LinkedList;
  11. import java.util.StringTokenizer;
  12.  
  13.  
  14.  
  15. /**
  16.  *
  17.  * @author cteam022
  18.  */
  19. public class Grasshopper {
  20.  
  21.  
  22. /**
  23.   * @param args the command line arguments
  24.   */
  25. public static void main(String[] args) throws IOException {
  26. Grasshopper g = new Grasshopper();
  27. while(true){g.uloha();}
  28. }
  29.  
  30. void uloha() throws IOException{
  31. if(!input.ready()){System.exit(0);}
  32. st = new StringTokenizer(input.readLine());
  33. int a = Integer.valueOf(st.nextToken());
  34. int b = Integer.valueOf(st.nextToken());
  35. int x1 = Integer.valueOf(st.nextToken())-1;
  36. int y1 = Integer.valueOf(st.nextToken())-1;
  37. int x2 = Integer.valueOf(st.nextToken())-1;
  38. int y2 = Integer.valueOf(st.nextToken())-1;
  39.  
  40. int x = x1, y = y1;
  41.  
  42. int i;
  43.  
  44. for(i=0; Math.abs(x - x2) > 2 && Math.abs(y - y2)>2; i++){
  45. int dx = x2 - x;
  46. int dy = y2 - y;
  47.  
  48. if(Math.abs(dx) >= Math.abs(dy)){
  49. x += Math.signum(dx)*2;
  50. y += Math.signum(dy);
  51. }else{
  52. x += Math.signum(dx);
  53. y += Math.signum(dy)*2;
  54. }
  55. }
  56.  
  57. LinkedList<Integer[]> fifo = new LinkedList<Integer[]>();
  58. fifo.addLast(new Integer[]{x, y, 0});
  59. boolean[][] visited = new boolean[b][a];
  60. for (int k = 0; k < b; k++)
  61. for (int l = 0; l < a; l++)
  62. visited[k][l] = false;
  63.  
  64.  
  65. int p = rekurze(6, x2, y2, a, b, visited, fifo);
  66.  
  67. if(p == -1){
  68. System.out.println("impossible");
  69. }else{
  70. System.out.println(i+p);
  71. }
  72.  
  73. }
  74.  
  75. static int rekurze(int pocet, int x2, int y2, int a, int b, boolean[][] visited, LinkedList<Integer[]> fifo){
  76. while (!fifo.isEmpty()) {
  77.  
  78. Integer[] coords = fifo.pop();
  79. int x = coords[0]; int y = coords[1]; int dep = coords[2];
  80.  
  81. if (visited[y][x]) continue;
  82. visited[y][x] = true;
  83.  
  84. if (x2 == x && y2 == y) return dep;
  85.  
  86.  
  87.  
  88. for(int druhx = -1; druhx < 2; druhx+=2){
  89. for(int druhy = -1; druhy < 2; druhy+=2){
  90. if(x+druhx*2 < a && x+druhx*2>=0 && y+druhy < b && y+druhy >=0){
  91. fifo.addLast(new Integer[]{x+druhx*2, y+druhy, dep+1});
  92. }
  93. if(x+druhx < a && x+druhx>=0 && y+druhy*2 < b && y+druhy*2 >=0){
  94. fifo.addLast(new Integer[]{x+druhx, y+druhy*2, dep+1});
  95. }
  96. }
  97.  
  98. }
  99. }
  100.  
  101. return -1;
  102. }
  103.  
  104. }
  105.  

Diff to submission s742

Grasshopper.java

--- c4.s742.cteam022.grasshop.java.0.Grasshopper.java
+++ c4.s777.cteam022.grasshop.java.0.Grasshopper.java
@@ -35,8 +35,8 @@
         int a = Integer.valueOf(st.nextToken());
         int b = Integer.valueOf(st.nextToken());
-        int x1 = Integer.valueOf(st.nextToken());
-        int y1 = Integer.valueOf(st.nextToken());
-        int x2 = Integer.valueOf(st.nextToken());
-        int y2 = Integer.valueOf(st.nextToken());
+        int x1 = Integer.valueOf(st.nextToken())-1;
+        int y1 = Integer.valueOf(st.nextToken())-1;
+        int x2 = Integer.valueOf(st.nextToken())-1;
+        int y2 = Integer.valueOf(st.nextToken())-1;
         
         int x = x1, y = y1;