0% found this document useful (0 votes)
12 views2 pages

Working

This Java program finds a path through a board of squares from a starting point to the end. It uses recursion and backtracking to try all possible paths. It stores the path taken in a 2D array and returns true if a complete path is found or false if no solution exists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Working

This Java program finds a path through a board of squares from a starting point to the end. It uses recursion and backtracking to try all possible paths. It stores the path taken in a 2D array and returns true if a complete path is found or false if no solution exists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;

public class Main {

private static int[] xa = new int[]{-1, -1, 1, 1, -2, -2, 2, 2};


private static int[] ya = new int[]{-2, 2, -2, 2, -1, 1, -1, 1};

public static void findPath(int n, int x, int y) {


int[][] board = new int[n][n];
boolean[][] visited = new boolean[n][n];
board[x][y] = 1;
visited[x][y] = true;
if (move(x, y, board, visited, 2)) {
print(board);
} else {
System.out.println("No solution found");
}
}

public static boolean move(int x, int y, int[][] board, boolean[][] visited,


int count) {
if (count > board.length * board.length) {
return true;
}
List<Point> nextMoves = getNextMoves(x, y, board, visited);
if (nextMoves.isEmpty()) {
return false;
}
nextMoves.sort(Comparator.comparingInt(p -> getNextMoves(p.x, p.y, board,
visited).size()));
for (Point p : nextMoves) {
board[p.x][p.y] = count;
visited[p.x][p.y] = true;
if (move(p.x, p.y, board, visited, count + 1)) {
return true;
}
board[p.x][p.y] = 0;
visited[p.x][p.y] = false;
}
return false;
}

public static List<Point> getNextMoves(int x, int y, int[][] board, boolean[][]


visited) {
List<Point> nextMoves = new ArrayList<>();
for (int i = 0; i < 8; i++) {
int nextX = x + xa[i];
int nextY = y + ya[i];
if (isValidMove(nextX, nextY, board) && !visited[nextX][nextY]) {
nextMoves.add(new Point(nextX, nextY));
}
}
return nextMoves;
}

public static void print(int[][] board) {


for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] < 10) {
System.out.print("0" + board[i][j] + " ");
} else {
System.out.print(board[i][j] + " ");
}
}
System.out.println();
}
}

public static boolean isValidMove(int x, int y, int[][] board) {


return x >= 0 && y >= 0 && x < board.length && y < board.length;
}

public static void main(String[] args) {


int boardSize = 10; // Change the board size here
int startX = 2; // Change the starting X coordinate here
int startY = 2; // Change the starting Y coordinate here
findPath(boardSize, startX, startY);
}

static class Point {


int x;
int y;

Point(int x, int y) {
this.x = x;
this.y = y;
}
}
}

You might also like