Advertisement
AhmedGhazaly

Untitled

Apr 6th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. class node{
  6.     int raw;
  7.     int colum;
  8.     int s;
  9.     public node(int raw,int colum,int s){
  10.         this.raw=raw;
  11.         this.colum=colum;
  12.         this .s=s;
  13.     }
  14.    
  15. }
  16. public class Main {
  17.    
  18.     public static void main(String[] args) throws IOException {
  19.         BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
  20.         StringTokenizer in=new StringTokenizer(r.readLine());
  21.     while(in.countTokens()>1){    
  22.         String f=in.nextToken();
  23.         String d=in.nextToken();
  24.        
  25.         int fraw=f.charAt(0)-96;
  26.         int fcolum=Integer.parseInt(f.charAt(1)+"");
  27.        
  28.         int dr=d.charAt(0)-96;
  29.         int dc=Integer.parseInt(d.charAt(1)+"");
  30.        
  31.        
  32.        // System.out.println(fraw+" "+fcolum);
  33.         //System.out.println(dr+ " "+dc);
  34.        
  35.         node c=new node(fraw,fcolum,0);
  36.        
  37.         Queue <node> q=new LinkedList<>();
  38.         q.add(c);
  39.         while(true){
  40.            
  41.             node cu=q.poll();
  42.             if(cu.raw<1 || cu.colum<1)continue;
  43.             if(cu.raw==dr && cu.colum==dc){
  44.                 System.out.printf("To get from %s to %s takes %d knight moves.\n",f,d,cu.s);
  45.                 break;
  46.             }
  47.            
  48.             int fr=cu.raw;
  49.             int fc=cu.colum;
  50.            
  51.             int steps=cu.s;
  52.             q.add(new node(fr+2,fc+1,steps+1)  );
  53.             q.add(new node(fr+1,fc+2,steps+1)  );
  54.             q.add(new node(fr+2,fc-1,steps+1)  );
  55.             q.add(new node(fr+1,fc-2,steps+1)  );
  56.             q.add(new node(fr-1,fc+2,steps+1)  );
  57.             q.add(new node(fr-1,fc-2,steps+1)  );
  58.             q.add(new node(fr-2,fc+1,steps+1)  );
  59.             q.add(new node(fr-2,fc-1,steps+1)  );
  60.            
  61.         }
  62.         in=new StringTokenizer(r.readLine());
  63.     }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement