Source code for submission s680

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.StringTokenizer;
  11.  
  12.  
  13.  
  14. /**
  15.  *
  16.  * @author cteam022
  17.  */
  18. public class Grasshopper {
  19.  
  20.  
  21. /**
  22.   * @param args the command line arguments
  23.   */
  24. public static void main(String[] args) throws IOException {
  25. Grasshopper g = new Grasshopper();
  26. while(true){g.uloha();}
  27. }
  28.  
  29. void uloha() throws IOException{
  30. if(!input.ready()){System.exit(0);}
  31. st = new StringTokenizer(input.readLine());
  32. int a = Integer.valueOf(st.nextToken());
  33. int b = Integer.valueOf(st.nextToken());
  34. int x1 = Integer.valueOf(st.nextToken());
  35. int y1 = Integer.valueOf(st.nextToken());
  36. int x2 = Integer.valueOf(st.nextToken());
  37. int y2 = Integer.valueOf(st.nextToken());
  38.  
  39. int x = x1, y = y1;
  40.  
  41. int i;
  42.  
  43. for(i=0; Math.abs(x - x2) > 2 && Math.abs(y - y2)>2; i++){
  44. int dx = x2 - x;
  45. int dy = y2 - y;
  46.  
  47. if(Math.abs(dx) >= Math.abs(dy)){
  48. x += Math.signum(dx)*2;
  49. y += Math.signum(dy);
  50. }else{
  51. x += Math.signum(dx);
  52. y += Math.signum(dy)*2;
  53. }
  54. }
  55.  
  56. int p = rekurze(6, x2, y2, x, y, a, b);
  57.  
  58. if(p == 100){
  59. System.out.println("impossible");
  60. }else{
  61. System.out.println(i+p);
  62. }
  63.  
  64. }
  65.  
  66. static int rekurze(int pocet, int x2, int y2, int x,int y,int a, int b){
  67. if (pocet < 0) return 100;
  68. if (x2 == x && y2 == y) return 0;
  69. int min = 100;
  70. for(int druhx = -1; druhx < 2; druhx+=2){
  71. for(int druhy = -1; druhy < 2; druhy+=2){
  72. if(x+druhx*2 < a && x+druhx*2>=0 && y+druhy < b && y+druhy >=0){
  73. int r1 = rekurze(pocet - 1, x2, y2, x+druhx*2, y+druhy, a, b);
  74. min = Math.min(r1, min);
  75. }
  76. if(x+druhx < a && x+druhx>=0 && y+druhy*2 < b && y+druhy*2 >=0){
  77. int r2 = rekurze(pocet - 1,x2, y2, x+druhx, y+druhy*2, a, b);
  78. min = Math.min(r2, min);
  79. }
  80. }
  81.  
  82. }
  83. return min==100 ? 100 : min + 1;
  84. }
  85.  
  86. }
  87.