Source code for submission s783

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 = 0;
  43.  
  44. /*
  45.   for(i=0; Math.abs(x - x2) > 2 && Math.abs(y - y2)>2; i++){
  46.   int dx = x2 - x;
  47.   int dy = y2 - y;
  48.  
  49.   if(Math.abs(dx) >= Math.abs(dy)){
  50.   x += Math.signum(dx)*2;
  51.   y += Math.signum(dy);
  52.   }else{
  53.   x += Math.signum(dx);
  54.   y += Math.signum(dy)*2;
  55.   }
  56.   }*/
  57.  
  58. LinkedList<Integer[]> fifo = new LinkedList<Integer[]>();
  59. fifo.addLast(new Integer[]{x, y, 0});
  60. boolean[][] visited = new boolean[b][a];
  61. for (int k = 0; k < b; k++)
  62. for (int l = 0; l < a; l++)
  63. visited[k][l] = false;
  64.  
  65.  
  66. int p = rekurze(6, x2, y2, a, b, visited, fifo);
  67.  
  68. if(p == -1){
  69. System.out.println("impossible");
  70. }else{
  71. System.out.println(i+p);
  72. }
  73.  
  74. }
  75.  
  76. static int rekurze(int pocet, int x2, int y2, int a, int b, boolean[][] visited, LinkedList<Integer[]> fifo){
  77. while (!fifo.isEmpty()) {
  78.  
  79. Integer[] coords = fifo.pop();
  80. int x = coords[0]; int y = coords[1]; int dep = coords[2];
  81.  
  82. if (visited[y][x]) continue;
  83. visited[y][x] = true;
  84.  
  85. if (x2 == x && y2 == y) return dep;
  86.  
  87.  
  88.  
  89. for(int druhx = -1; druhx < 2; druhx+=2){
  90. for(int druhy = -1; druhy < 2; druhy+=2){
  91. if(x+druhx*2 < a && x+druhx*2>=0 && y+druhy < b && y+druhy >=0){
  92. fifo.addLast(new Integer[]{x+druhx*2, y+druhy, dep+1});
  93. }
  94. if(x+druhx < a && x+druhx>=0 && y+druhy*2 < b && y+druhy*2 >=0){
  95. fifo.addLast(new Integer[]{x+druhx, y+druhy*2, dep+1});
  96. }
  97. }
  98.  
  99. }
  100. }
  101.  
  102. return -1;
  103. }
  104.  
  105. }
  106.  

Diff to submission s777

Grasshopper.java

--- c4.s777.cteam022.grasshop.java.0.Grasshopper.java
+++ c4.s783.cteam022.grasshop.java.0.Grasshopper.java
@@ -42,6 +42,7 @@
         int x = x1, y = y1;
         
-        int i;
+        int i = 0;
         
+        /*
         for(i=0; Math.abs(x - x2) > 2 && Math.abs(y - y2)>2; i++){
             int dx = x2 - x;
@@ -55,5 +56,5 @@
                 y += Math.signum(dy)*2;
             }
-        }
+        }*/
         
         LinkedList<Integer[]> fifo = new LinkedList<Integer[]>();