0% found this document useful (0 votes)
7 views3 pages

Import Java

The provided Java code implements a pathfinding algorithm for a board game, allowing a player to find the minimum number of moves from a source cell to a destination cell on a grid. The movement is determined by a specified move rule, which defines the possible directions. The program reads input for the grid dimensions, the grid itself, the source and destination coordinates, and the move rule, then outputs the minimum moves required or -1 if the destination is unreachable.

Uploaded by

skygennetsis14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Import Java

The provided Java code implements a pathfinding algorithm for a board game, allowing a player to find the minimum number of moves from a source cell to a destination cell on a grid. The movement is determined by a specified move rule, which defines the possible directions. The program reads input for the grid dimensions, the grid itself, the source and destination coordinates, and the move rule, then outputs the minimum moves required or -1 if the destination is unreachable.

Uploaded by

skygennetsis14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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);

You might also like