-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a2e2a4
commit fb74091
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int N = Integer.parseInt(br.readLine()); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int r1 = Integer.parseInt(st.nextToken()); | ||
int c1 = Integer.parseInt(st.nextToken()); | ||
int r2 = Integer.parseInt(st.nextToken()); | ||
int c2 = Integer.parseInt(st.nextToken()); | ||
|
||
int[][] visited = new int[N+1][N+1]; | ||
int[] dh = {-2, -2, 0, 0, 2, 2}; | ||
int[] dw = {-1, 1, -2, 2, -1, 1}; | ||
Queue<int[]> q = new LinkedList<>(); | ||
q.offer(new int[]{r1, c1}); | ||
while(!q.isEmpty()) { | ||
int h = q.peek()[0]; | ||
int w = q.peek()[1]; | ||
q.poll(); | ||
if(h == r2 && w == c2) { | ||
System.out.println(visited[h][w]); | ||
return; | ||
} | ||
|
||
for(int i=0; i<6; i++) { | ||
int nextH = h + dh[i]; | ||
int nextW = w + dw[i]; | ||
if(nextH >= 0 && nextH < N && nextW >= 0 && nextW < N) { | ||
if(visited[nextH][nextW] == 0) { | ||
visited[nextH][nextW] = visited[h][w] + 1; | ||
q.offer(new int[]{nextH, nextW}); | ||
} | ||
} | ||
} | ||
} | ||
System.out.println("-1"); | ||
} | ||
} |