import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); for(int a=1;a queue=initPriorityQueue(); queue.add(startNode); int moves=-1; while(queue.isEmpty()==false){ Node curNode=queue.poll(); if(curNode.x==N-1 && curNode.y==N-1){ moves=curNode.dist/(a+b); break; } Collection neighbours=getNeighbours(curNode,board, a, b, N); for(Node neigh:neighbours){ int newDist=curNode.dist+a+b; if(newDist getNeighbours(Node curNode,Node[][] board, int a, int b,int N){ int[] x={curNode.x+a,curNode.x+a,curNode.x-a,curNode.x-a,curNode.x+b,curNode.x+b,curNode.x-b,curNode.x-b}; int[] y={curNode.y+b,curNode.y-b,curNode.y+b,curNode.y-b,curNode.y+a,curNode.y-a,curNode.y+a,curNode.y-a}; Set neighbours=new HashSet(); for(int i=0;i<8;i++){ if(x[i]>=0 && x[i]=0 && y[i] neighboursList=new ArrayList(neighbours); return neighboursList; } /* private static int[][] initBoard(int N){ int MAX=Integer.MAX_VALUE; int[][] board=new int[N][N]; for(int i=0;i initPriorityQueue(){ PriorityQueue queue=new PriorityQueue(new Comparator() { @Override public int compare(Node o1, Node o2) { int res=Integer.valueOf(o2.priority).compareTo(Integer.valueOf(o1.priority)); return res; } }); return queue; } private static class Node{ int x; int y; int dist; int priority; @Override public boolean equals(Object nodeObj){ Node node=(Node)nodeObj; boolean result=false; if(this.x==node.x && this.y==node.y) result=true; return result; } } }