데스 나이트(16948)
Last updated
Last updated
6
5 1 0 5-17
0 3 4 32import java.util.*;
import java.io.*;
public class Main{
static int n;
static int[][] arr;
static boolean[][] visited;
static int[] dx = {-2, -2, 0, 0, 2, 2};
static int[] dy = {-1, 1, -2, 2, 1, -1};
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
arr = new int[n][n];
visited = new boolean[n][n];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int sx = Integer.parseInt(st.nextToken());
int sy = Integer.parseInt(st.nextToken());
int ex = Integer.parseInt(st.nextToken());
int ey = Integer.parseInt(st.nextToken());
fn(sx, sy, ex, ey);
}
public static void fn(int sx, int sy, int ex, int ey){
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{sx, sy});
visited[sx][sy] = true;
while(!q.isEmpty()){
int[] xy = q.poll();
if(xy[0]==ex && xy[1]==ey){
System.out.println(arr[xy[0]][xy[1]]);
return;
}
for(int i=0; i<6; i++){
int nx = xy[0] + dx[i];
int ny = xy[1] + dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=n || visited[nx][ny])
continue;
visited[nx][ny] = true;
q.add(new int[] {nx, ny});
arr[nx][ny] = arr[xy[0]][xy[1]] + 1;
}
}
System.out.println(-1);
}
}