Source code for submission s825

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. fronta.add(new Bod(StartX,StartY,0));
  75. bf1();
  76. if (val==-1){
  77. System.out.println("impossible");
  78. }
  79. else {
  80. System.out.println(val);
  81. }
  82. }
  83. }
  84.  
  85. public static void bf1(){
  86. Bod temp;
  87. while(true){
  88. if (konec == true || fronta.isEmpty())return;
  89. temp=fronta.remove();
  90. bf(temp.x, temp.y, temp.vzdal);
  91. }
  92. }
  93.  
  94.  
  95. public static void bf(int x, int y,int vzdal){
  96. if (x == CilX && y == CilY) {
  97. konec = true;
  98. val = vzdal;
  99. return;
  100. }
  101. stav[x][y]=1;
  102. //System.out.println(x + " " + y);
  103. if (stav[x+1][y+2]==0){
  104. fronta.add(new Bod(x+1,y+2,vzdal+1));
  105. }
  106. if (stav[x+2][y+1]==0){
  107. fronta.add(new Bod(x+2, y+1,vzdal+1));
  108. }
  109. if (stav[x+2][y-1]==0){
  110. fronta.add(new Bod(x+2, y-1,vzdal+1));
  111. }
  112. if (stav[x+1][y-2]==0){
  113. fronta.add(new Bod(x+1, y-2,vzdal+1));
  114. }
  115. if (stav[x-1][y+2]==0){
  116. fronta.add(new Bod(x-1, y+2,vzdal+1));
  117. }
  118. if (stav[x-1][y-2]==0){
  119. fronta.add(new Bod(x-1, y-2,vzdal+1));
  120. }
  121. if (stav[x-2][y+1]==0){
  122. fronta.add(new Bod(x-2, y+1,vzdal+1));
  123. }
  124. if (stav[x-2][y-1]==0){
  125. fronta.add(new Bod(x-2, y-1,vzdal+1));
  126. }
  127.  
  128. }
  129.  
  130. }
  131.  

Diff to submission s812

Grashopper.java

--- c4.s812.cteam051.grasshop.java.0.Grashopper.java
+++ c4.s825.cteam051.grasshop.java.0.Grashopper.java
@@ -39,5 +39,5 @@
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
-        stav = new int [102][102];
+        stav = new int [104][104];
         fronta = new ArrayDeque<Bod>();
         //vzdalenost = new int [102][102];