import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static class Cell{ int n; int x; int y; int cost; Cell(int n, int x, int y){ this.n = n; this.x = x; this.y = y; cost = Integer.MAX_VALUE; } boolean isValid(){ if (x>=0 && x=0 && y map = new HashMap(); Queue q = new LinkedList(); for (int i=0; i<(n) ;i++){ for (int j=0; j<(n) ;j++){ Cell cell = new Cell(n, j, i); map.put(cell.getIndex(), cell); } } Cell c = map.get(0); c.cost = 0; q.add(c); int[] dx; int[] dy; int moves = 0; if (x==y){ dx = new int[]{x, x, -x, -x}; dy = new int[]{y, -y, y, -y}; moves = 4; }else { dx = new int[]{x, x, -x, -x, y, -y, y, -y}; dy = new int[]{y, -y, y, -y, x, x, -x, -x}; moves = 8; } while(!q.isEmpty()){ Cell prevCell = q.remove(); for (int i=0; i