Source code for submission s1122

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. Place pocatek=new Place(poziceSkokanaX, poziceSkokanaY,0);
  22. poleJizSkakano[poziceSkokanaY][poziceSkokanaX]=true;
  23. // System.out.println(poleJizSkakano[5][5]);
  24. ArrayList<Place> fronta = new ArrayList<Place>();
  25. fronta.add(pocatek);
  26.  
  27. boolean nalez=false;
  28. while(!fronta.isEmpty() && !nalez){
  29. Place aktZpracovane=fronta.get(0);
  30. fronta.remove(0);
  31.  
  32. int aktX=aktZpracovane.x;
  33. int aktY=aktZpracovane.y;
  34. if((aktX ==poziceMnamkyX) && (aktY==poziceMnamkyY)){
  35. System.out.println(aktZpracovane.step);
  36. nalez=true;
  37. }else{
  38. aktZpracovane.step++;
  39. for(int i=0;i<polePozic.length;i++){
  40. int testX=aktZpracovane.x+polePozic[i][0];
  41. int testY=aktZpracovane.y+polePozic[i][1];
  42.  
  43. if(((testX>=1) && (testX<=pocetSloupcu))&&((testY>=1)&&(testY<=pocetRadek))){
  44. if(!poleJizSkakano[testY][testX]){
  45. fronta.add(new Place(testX, testY,aktZpracovane.step));
  46. }
  47. }
  48. }
  49.  
  50. }
  51. }
  52. // System.out.println("dostalo se k ifu");
  53. if(fronta.isEmpty()){
  54. System.out.println("impossible");
  55. }
  56. }
  57. }
  58.  
  59. }
  60.  
  61. class Place{
  62. public int x;
  63. public int y;
  64. public int step;
  65.  
  66. public Place(int x, int y, int step){
  67. this.x=x;
  68. this.y=y;
  69. this.step=step;
  70. }
  71. }
  72.  
  73. //class Fronta{
  74. // public Place zacatek;
  75. // public Place konec;
  76. //
  77. // public Fronta(){
  78. // zacatek=null;
  79. // konec=null;
  80. // zacatek=konec;
  81. // }
  82. //
  83. // public pridejPrvek()
  84. //
  85. //
  86. //}
  87.  
  88.  
  89.  
  90.  
  91.