import [Link].
*;
public class BoardGamePathFinder {
static class Cell {
int x, y, steps;
Cell(int x, int y, int steps) {
this.x = x;
this.y = y;
[Link] = steps;
public static int minMoves(int[][] grid, int[] source, int[] dest, int[]
moveRule) {
int M = [Link];
int N = grid[0].length;
boolean[][] visited = new boolean[M][N];
Queue<Cell> queue = new LinkedList<>();
// 4 directions based on move rule:
int[][] directions = new int[4][2];
directions[0] = new int[]{moveRule[0], moveRule[1]}; //
Forward
directions[1] = new int[]{moveRule[1], -moveRule[0]}; // Right
(90° CW)
directions[2] = new int[]{-moveRule[1], moveRule[0]}; // Left
(90° CCW)
directions[3] = new int[]{-moveRule[0], -moveRule[1]}; //
Backward (180°)
[Link](new Cell(source[0], source[1], 0));
visited[source[0]][source[1]] = true;
while (![Link]()) {
Cell curr = [Link]();
if (curr.x == dest[0] && curr.y == dest[1]) {
return [Link];
for (int[] dir : directions) {
int newX = curr.x + dir[0];
int newY = curr.y + dir[1];
if (newX >= 0 && newY >= 0 && newX < M && newY < N &&
grid[newX][newY] == 0 && !visited[newX][newY]) {
visited[newX][newY] = true;
[Link](new Cell(newX, newY, [Link] + 1));
return -1; // If destination is unreachable
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int M = [Link]();
int N = [Link]();
int[][] grid = new int[M][N];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
grid[i][j] = [Link]();
int[] source = new int[]{[Link](), [Link]()};
int[] dest = new int[]{[Link](), [Link]()};
int[] moveRule = new int[]{[Link](), [Link]()};
int result = minMoves(grid, source, dest, moveRule);
[Link](result);