Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class node{
- int raw;
- int colum;
- int s;
- public node(int raw,int colum,int s){
- this.raw=raw;
- this.colum=colum;
- this .s=s;
- }
- }
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer in=new StringTokenizer(r.readLine());
- while(in.countTokens()>1){
- String f=in.nextToken();
- String d=in.nextToken();
- int fraw=f.charAt(0)-96;
- int fcolum=Integer.parseInt(f.charAt(1)+"");
- int dr=d.charAt(0)-96;
- int dc=Integer.parseInt(d.charAt(1)+"");
- // System.out.println(fraw+" "+fcolum);
- //System.out.println(dr+ " "+dc);
- node c=new node(fraw,fcolum,0);
- Queue <node> q=new LinkedList<>();
- q.add(c);
- while(true){
- node cu=q.poll();
- if(cu.raw<1 || cu.colum<1)continue;
- if(cu.raw==dr && cu.colum==dc){
- System.out.printf("To get from %s to %s takes %d knight moves.\n",f,d,cu.s);
- break;
- }
- int fr=cu.raw;
- int fc=cu.colum;
- int steps=cu.s;
- q.add(new node(fr+2,fc+1,steps+1) );
- q.add(new node(fr+1,fc+2,steps+1) );
- q.add(new node(fr+2,fc-1,steps+1) );
- q.add(new node(fr+1,fc-2,steps+1) );
- q.add(new node(fr-1,fc+2,steps+1) );
- q.add(new node(fr-1,fc-2,steps+1) );
- q.add(new node(fr-2,fc+1,steps+1) );
- q.add(new node(fr-2,fc-1,steps+1) );
- }
- in=new StringTokenizer(r.readLine());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement