Source code for submission s1200

Go to diff to previous submission

Main.java

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6. public static final int[][] polePozic={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc=new Scanner(System.in);
  10. while(sc.hasNext()){
  11.  
  12. int pocetRadek=sc.nextInt();
  13. int pocetSloupcu=sc.nextInt();
  14.  
  15. int poziceSkokanaX=sc.nextInt();
  16. int poziceSkokanaY=sc.nextInt();
  17. int poziceMnamkyX=sc.nextInt();
  18. int poziceMnamkyY=sc.nextInt();
  19.  
  20. boolean poleJizSkakano[][]=new boolean[pocetRadek+1][pocetSloupcu+1];
  21. poleJizSkakano[poziceSkokanaY][poziceSkokanaX]=true;
  22. // System.out.println(poleJizSkakano[5][5]);
  23. ArrayList<Integer> fronta = new ArrayList<Integer>();
  24. fronta.add(poziceSkokanaX);
  25. fronta.add(poziceSkokanaY);
  26. fronta.add(0);
  27.  
  28. boolean nalez=false;
  29. while(!fronta.isEmpty() && !nalez){
  30. Integer aktX=fronta.get(0); fronta.remove(0);
  31. Integer aktY=fronta.get(0); fronta.remove(0);
  32. Integer aktStep=fronta.get(0); fronta.remove(0);
  33. // System.out.println("aktx je "+aktX+"akty je "+aktY+" aktStep je "+aktStep);
  34.  
  35. if((aktX ==poziceMnamkyX) && (aktY==poziceMnamkyY)){
  36. System.out.println(aktStep);
  37. nalez=true;
  38. }else{
  39. aktStep++;
  40. for(int i=0;i<polePozic.length;i++){
  41. int testX=aktX+polePozic[i][0];
  42. int testY=aktY+polePozic[i][1];
  43.  
  44. if(((testX>=1) && (testX<=pocetSloupcu))&&((testY>=1)&&(testY<=pocetRadek))){
  45. if(!poleJizSkakano[testY][testX]){
  46. fronta.add(testX);
  47. fronta.add(testY);
  48. fronta.add(aktStep);
  49. }
  50. }
  51. }
  52.  
  53. }
  54. }
  55. // System.out.println("dostalo se k ifu");
  56. if(fronta.isEmpty()){
  57. System.out.println("impossible");
  58. }
  59. }
  60. }
  61. }
  62.  
  63.  
  64. // private class Place{
  65. // public int x;
  66. // public int y;
  67. // public int step;
  68. //
  69. // public Place(int x, int y, int step){
  70. // this.x=x;
  71. // this.y=y;
  72. // this.step=step;
  73. // }
  74. // }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  

Diff to submission s1122

Main.java

--- c4.s1122.cteam117.grasshop.java.0.Main.java
+++ c4.s1200.cteam117.grasshop.java.0.Main.java
@@ -19,29 +19,32 @@
 
                         boolean poleJizSkakano[][]=new boolean[pocetRadek+1][pocetSloupcu+1];
-                        Place pocatek=new Place(poziceSkokanaX, poziceSkokanaY,0);
                         poleJizSkakano[poziceSkokanaY][poziceSkokanaX]=true;
                         //                      System.out.println(poleJizSkakano[5][5]);
-                        ArrayList<Place> fronta = new ArrayList<Place>();
-                        fronta.add(pocatek);
+                        ArrayList<Integer> fronta = new ArrayList<Integer>();
+                        fronta.add(poziceSkokanaX);
+                        fronta.add(poziceSkokanaY);
+                        fronta.add(0);
 
                         boolean nalez=false;
                         while(!fronta.isEmpty() && !nalez){
-                                Place aktZpracovane=fronta.get(0);
-                                fronta.remove(0);
+                                Integer aktX=fronta.get(0); fronta.remove(0);
+                                Integer aktY=fronta.get(0); fronta.remove(0);
+                                Integer aktStep=fronta.get(0); fronta.remove(0);
+//                              System.out.println("aktx je "+aktX+"akty je "+aktY+" aktStep je "+aktStep);
 
-                                int aktX=aktZpracovane.x;
-                                int aktY=aktZpracovane.y;
                                 if((aktX ==poziceMnamkyX) && (aktY==poziceMnamkyY)){
-                                        System.out.println(aktZpracovane.step);
+                                        System.out.println(aktStep);
                                         nalez=true;
                                 }else{
-                                        aktZpracovane.step++;
+                                        aktStep++;
                                         for(int i=0;i<polePozic.length;i++){
-                                                int testX=aktZpracovane.x+polePozic[i][0];
-                                                int testY=aktZpracovane.y+polePozic[i][1];
+                                                int testX=aktX+polePozic[i][0];
+                                                int testY=aktY+polePozic[i][1];
 
                                                 if(((testX>=1) && (testX<=pocetSloupcu))&&((testY>=1)&&(testY<=pocetRadek))){
                                                         if(!poleJizSkakano[testY][testX]){
-                                                                fronta.add(new Place(testX, testY,aktZpracovane.step));
+                                                                fronta.add(testX);
+                                                                fronta.add(testY);
+                                                                fronta.add(aktStep);                    
                                                         }
                                                 }       
@@ -50,5 +53,5 @@
                                 }
                         }
-//                      System.out.println("dostalo se k ifu");
+//                                              System.out.println("dostalo se k ifu");
                         if(fronta.isEmpty()){
                                 System.out.println("impossible");
@@ -59,30 +61,21 @@
 }
 
-class Place{
-        public int x;
-        public int y;
-        public int step;
 
-        public Place(int x, int y, int step){
-                this.x=x;
-                this.y=y;
-                this.step=step;
-        }
-}
-
-//class Fronta{
-//      public Place zacatek;
-//      public Place konec;
-//      
-//      public Fronta(){
-//              zacatek=null;
-//              konec=null;
-//              zacatek=konec;
+//      private class Place{
+//              public int x;
+//              public int y;
+//              public int step;
+//
+//              public Place(int x, int y, int step){
+//                      this.x=x;
+//                      this.y=y;
+//                      this.step=step;
+//              }
 //      }
-//      
-//      public pridejPrvek()
-//      
-//      
-//}
+
+
+
+
+