Untitled
unknown
plain_text
a year ago
1.9 kB
2
Indexable
Never
package lv4; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; class pos { int x, y; public pos(int x, int y) { this.x = x; this.y = y; } } public class knight { static int[] spinX = { -1, -2, -2, -1, 1, 2, 2, 1 }; static int[] spinY = { 2, 1, -1, -2, -2, -1, 1, 2 }; static int cr, cc, nr, nc, r, c; static pos[] Data = new pos[5000000]; static int front, rear; public knight() { this.front = this.rear = -1; } void reset() { front = rear = -1; } public void enQueue(pos value) { Data[++rear] = value; } pos deQueue() { return Data[++front]; } boolean isEmpty() { if (this.front == this.rear) { return true; } return false; } public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream("Text")); Scanner scanner = new Scanner(System.in); int tc = scanner.nextInt(); knight queue = new knight(); for (int Case = 1; Case <= tc; Case++) { queue.reset(); int n = scanner.nextInt(); int m = scanner.nextInt(); int r = scanner.nextInt(); int c = scanner.nextInt(); int s = scanner.nextInt(); int k = scanner.nextInt(); int[][] arr = new int[n + 1][m + 1]; boolean[][] check = new boolean[n + 1][m + 1]; pos locPos = new pos(r, c); queue.enQueue(locPos); check[r][c] = true; while (!queue.isEmpty()) { locPos = queue.deQueue(); cr = locPos.x; cc = locPos.y; if (cr == s && cc == k) { System.out.println(arr[cr][cc]); break; } for (int i = 0; i < 8; i++) { nr = cr + spinX[i]; nc = cc + spinY[i]; if (nr >= 1 && nr <= n && nc >= 1 && nc <= m && check[nr][nc] == false) { arr[nr][nc] = arr[cr][cc] + 1; locPos = new pos(nr, nc); queue.enQueue(locPos); check[nr][nc] = true; } } } } } }