import [Link].
*;
public class GridGame {
private static final int[][] DIRECTIONS = {
{1, 2},
{2, -1},
{-1, 1},
{-1, -2}
};
public static int minMoves(int[][] grid, int[] source, int[] destination, int[]
moveRule) {
int M = [Link], N = grid[0].length;
boolean[][] visited = new boolean[M][N];
Queue<int[]> queue = new LinkedList<>();
[Link](new int[]{source[0], source[1], 0});
visited[source[0]][source[1]] = true;
while (![Link]()) {
int[] current = [Link]();
int x = current[0], y = current[1], steps = current[2];
if (x == destination[0] && y == destination[1]) {
return steps;
}
for (int[] direction : DIRECTIONS) {
int newX = x + moveRule[0] * direction[0];
int newY = y + moveRule[1] * direction[1];
if (isValid(newX, newY, M, N, grid, visited)) {
visited[newX][newY] = true;
[Link](new int[]{newX, newY, steps + 1});
}
}
}
return -1;
}
private static boolean isValid(int x, int y, int M, int N, int[][] grid,
boolean[][] visited) {
return x >= 0 && x < M && y >= 0 && y < N && grid[x][y] == 0 && !visited[x]
[y];
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter grid dimensions M and N:");
int M = [Link]();
int N = [Link]();
[Link]("Enter grid (0 for open cells, 1 for blocked cells):");
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[] destination = new int[]{[Link](), [Link]()};
int[] moveRule = new int[]{[Link](), [Link]()};
int result = minMoves(grid, source, destination, moveRule);
[Link](result);
}
}