Source code for submission s918

Go to diff to previous submission

Grashopper.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. import java.util.ArrayDeque;
  7. import java.util.Queue;
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author cteam051
  13.  */
  14.  
  15.  
  16.  
  17. public class Grashopper {
  18.  
  19. public static class Bod{
  20. private int x,y,vzdal;
  21.  
  22. public Bod(int x, int y, int vzdal) {
  23. this.x = x;
  24. this.y = y;
  25. this.vzdal = vzdal;
  26. }
  27.  
  28. }
  29.  
  30.  
  31.  
  32. public static int [][]stav;
  33. //public static int [][]vzdalenost;
  34. public static boolean konec;
  35. public static int val;
  36. public static int X, Y, StartX, StartY, CilX, CilY;
  37. public static Queue<Bod> fronta;
  38.  
  39. public static void main(String[] args) {
  40. Scanner sc = new Scanner(System.in);
  41. stav = new int [104][104];
  42. fronta = new ArrayDeque<Bod>();
  43. //vzdalenost = new int [102][102];
  44.  
  45. while (sc.hasNextInt()){
  46. fronta.clear();
  47. konec = false;
  48. X = sc.nextInt()+2;
  49. Y = sc.nextInt()+2;
  50. StartX = sc.nextInt()+1;
  51. StartY = sc.nextInt()+1;
  52. CilX = sc.nextInt()+1;
  53. CilY = sc.nextInt()+1;
  54. val = -1;
  55. for (int i=2; i<X; i++){
  56. for (int j=2; j<Y; j++){
  57. stav[i][j]=0;
  58. }
  59. }
  60. int TempX=X+2;
  61. int TempY=Y+2;
  62. for (int i=0; i<2; i++){
  63. for (int j=0; j<TempY; j++){
  64. stav[i][j]=1;
  65. stav[i+X][j]=1;
  66. }
  67. }
  68. for (int i=0; i<TempX; i++){
  69. for (int j=0; j<2; j++){
  70. stav[i][j]=1;
  71. stav[i][j+Y]=1;
  72. }
  73. }
  74. stav[StartX][StartY]=1;
  75. fronta.add(new Bod(StartX,StartY,0));
  76. bf1();
  77. if (val==-1){
  78. System.out.println("impossible");
  79. }
  80. else {
  81. System.out.println(val);
  82. }
  83. }
  84. }
  85.  
  86. public static void bf1(){
  87. Bod temp;
  88. while(true){
  89. if (konec == true || fronta.isEmpty())return;
  90. temp=fronta.remove();
  91. bf(temp.x, temp.y, temp.vzdal);
  92. }
  93. }
  94.  
  95.  
  96. public static void bf(int x, int y,int vzdal){
  97. if (x == CilX && y == CilY) {
  98. konec = true;
  99. val = vzdal;
  100. return;
  101. }
  102. //System.out.println(x + " " + y);
  103. if (stav[x+1][y+2]==0){
  104. stav[x+1][y+2]=1;
  105. fronta.add(new Bod(x+1,y+2,vzdal+1));
  106. }
  107. if (stav[x+2][y+1]==0){
  108. stav[x+2][y+1]=1;
  109. fronta.add(new Bod(x+2, y+1,vzdal+1));
  110. }
  111. if (stav[x+2][y-1]==0){
  112. stav[x+2][y-1]=1;
  113. fronta.add(new Bod(x+2, y-1,vzdal+1));
  114. }
  115. if (stav[x+1][y-2]==0){
  116. stav[x+1][y-2]=1;
  117. fronta.add(new Bod(x+1, y-2,vzdal+1));
  118. }
  119. if (stav[x-1][y+2]==0){
  120. stav[x-1][y+2]=1;
  121. fronta.add(new Bod(x-1, y+2,vzdal+1));
  122. }
  123. if (stav[x-1][y-2]==0){
  124. stav[x-1][y-2]=1;
  125. fronta.add(new Bod(x-1, y-2,vzdal+1));
  126. }
  127. if (stav[x-2][y+1]==0){
  128. stav[x-2][y+1]=1;
  129. fronta.add(new Bod(x-2, y+1,vzdal+1));
  130. }
  131. if (stav[x-2][y-1]==0){
  132. stav[x-2][y-1]=1;
  133. fronta.add(new Bod(x-2, y-1,vzdal+1));
  134. }
  135.  
  136. }
  137.  
  138. }
  139.  

Diff to submission s825

Grashopper.java

--- c4.s825.cteam051.grasshop.java.0.Grashopper.java
+++ c4.s918.cteam051.grasshop.java.0.Grashopper.java
@@ -72,4 +72,5 @@
                 }
             }
+            stav[StartX][StartY]=1;
             fronta.add(new Bod(StartX,StartY,0));
             bf1();
@@ -99,28 +100,35 @@
             return;
         }
-        stav[x][y]=1;
         //System.out.println(x + " " + y);
         if (stav[x+1][y+2]==0){
+            stav[x+1][y+2]=1;
             fronta.add(new Bod(x+1,y+2,vzdal+1));
         }
         if (stav[x+2][y+1]==0){
+            stav[x+2][y+1]=1;
             fronta.add(new Bod(x+2, y+1,vzdal+1));
         }
         if (stav[x+2][y-1]==0){
+            stav[x+2][y-1]=1;
             fronta.add(new Bod(x+2, y-1,vzdal+1));
         }
         if (stav[x+1][y-2]==0){
+            stav[x+1][y-2]=1;
             fronta.add(new Bod(x+1, y-2,vzdal+1));
         }
         if (stav[x-1][y+2]==0){
+            stav[x-1][y+2]=1;
             fronta.add(new Bod(x-1, y+2,vzdal+1));
         }
         if (stav[x-1][y-2]==0){
+            stav[x-1][y-2]=1;
             fronta.add(new Bod(x-1, y-2,vzdal+1));
         }
         if (stav[x-2][y+1]==0){
+            stav[x-2][y+1]=1;
             fronta.add(new Bod(x-2, y+1,vzdal+1));
         }
         if (stav[x-2][y-1]==0){
+            stav[x-2][y-1]=1;
             fronta.add(new Bod(x-2, y-1,vzdal+1));
         }