Source code for submission s855

Go to diff to previous submission

Grasshop.java

  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Grasshop {
  5.  
  6. public static void main(String[] args) {
  7. int i,z,j,k,i1,i2,j1,j2;
  8. boolean skok = false;
  9. int tah[][] = new int[9][2];
  10. tah[8][0]=1; tah[8][1]=2;
  11. tah[1][0]=2; tah[1][1]=1;
  12. tah[2][0]=2; tah[2][1]=-1;
  13. tah[3][0]=1; tah[3][1]=-2;
  14. tah[4][0]=-1; tah[4][1]=-2;
  15. tah[5][0]=-2; tah[5][1]=-1;
  16. tah[6][0]=-2; tah[6][1]=1;
  17. tah[7][0]=-1; tah[7][1]=2;
  18.  
  19. Scanner sc = new Scanner(System.in);
  20. while(sc.hasNext()){
  21. int r = sc.nextInt();
  22. int c = sc.nextInt();
  23. int gr = sc.nextInt();
  24. int gc = sc.nextInt();
  25. int lr = sc.nextInt();
  26. int lc = sc.nextInt();
  27.  
  28. int sach[][]= new int[c+3][r+3];
  29.  
  30. for(i=1;i<=c;i++){
  31. for(j=1;j<=r;j++){
  32. sach[i][j]=-1;
  33. }
  34. }
  35.  
  36. sach[gc][gr]=0;
  37. int krok=1;
  38.  
  39.  
  40.  
  41. if((gr!=lr)&&(gc!=lc))
  42. while(sach[lc][lr]==-1){
  43. skok = false;
  44. for(i1=1;i1<=c;i1++){
  45. for(i2=1;i2<=r;i2++){
  46. if(sach[i1][i2]==krok-1){
  47.  
  48. for(k=1;k<=8;k++){
  49. j1=i1+tah[k][0];
  50. j2=i2+tah[k][1];
  51.  
  52.  
  53. if((j2>0)&&(j2<r+1)&&(j1>0)&&(j1<c+1)){
  54. if(sach[j1][j2]==-1){
  55. sach[j1][j2]=krok;
  56. skok = true;
  57. }
  58. }
  59.  
  60. }
  61. }
  62. }
  63. }
  64. krok++;
  65. if(!skok){break;}
  66.  
  67.  
  68. }
  69. if((gc==lc)&&(gr==lr)){
  70. System.out.println("0");
  71. } else {
  72. if(skok==true){System.out.println(sach[lc][lr]);}
  73. else {
  74. System.out.println("impossible");
  75. }
  76. }
  77. }
  78.  
  79.  
  80. }
  81.  
  82. }
  83.  

Diff to submission s796

Grasshop.java

--- c4.s796.cteam060.grasshop.java.0.Grasshop.java
+++ c4.s855.cteam060.grasshop.java.0.Grasshop.java
@@ -26,5 +26,5 @@
         int lc = sc.nextInt();
       
-        int sach[][]= new int[c+1][r+1];
+        int sach[][]= new int[c+3][r+3];
         
         for(i=1;i<=c;i++){
@@ -36,4 +36,8 @@
         sach[gc][gr]=0;
         int krok=1;
+        
+       
+        
+        if((gr!=lr)&&(gc!=lc))
         while(sach[lc][lr]==-1){
           skok = false;  
@@ -60,9 +64,15 @@
           krok++;
           if(!skok){break;}
+          
+          
         }
-        if(skok==true){System.out.println(sach[lr][lc]);}
-        else {
-          System.out.println("impossible");    
-        }
+        if((gc==lc)&&(gr==lr)){ 
+                  System.out.println("0");
+                } else {
+          if(skok==true){System.out.println(sach[lc][lr]);}
+          else {
+            System.out.println("impossible");    
+          }
+            }
       }