Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an algorithm to find all possible routes in a board game?

I'm trying to write a javascript function that finds all possible routes of length N on a board (a 15x15 grid) where the player cannot move diagonally. I was able to come up with a pretty simple recursive solution but I suspect it is extremely unoptimized.

Here is the code:

search(n, u, v) {

    if (n == 0 || isWall(u, v))
        return;
        
    board[v][u] = 2;
    
    search(n - 1, u, v - 1);
    search(n - 1, u + 1, v);
    search(n - 1, u, v + 1);
    search(n - 1, u - 1, v);
    
    return;
}

board is a 2d array that contains the board's data. Free spaces, walls and reachable spaces are represented by 0s, 1s and 2s respectively.

Here's an example of what is looks like given N=6

SampleBoard

EDIT: As mentionned below, I'm trying to find all reachable cells in N or less moves.

like image 803
kaffeetasse Avatar asked Dec 19 '25 10:12

kaffeetasse


2 Answers

Like others wrote, you should use a breadth-first traversal instead of a depth-first traversal.

Secondly, you should not revisit a cell that already has been marked with value 2, so your condition to continue should be that the current cell has value 0.

I would suggest implementing the traversal using two arrays:

function search(board, n, u, v) {
    let count = 0;
    let frontier = [[u, v]];
    
    while (n-- > 0 && frontier.length) {
        let newFrontier = [];
        for (let [u, v] of frontier) {
            if (board[v]?.[u] === 0) {
                board[v][u] = 2;
                count++;
                newFrontier.push([u, v - 1], [u + 1, v], [u, v + 1], [u - 1, v]);
            }
        }
        frontier = newFrontier;
    }
    
    return count;
}

let board = [
    "11111111111111",
    "10000000000001",
    "10000000000001",
    "10011100111001",
    "10010000001001",
    "10010000001001",
    "10000000000001",
    "10000000000001",
    "10000000000001",
    "10010000001001",
    "10010000001001",
    "10011100111001",
    "10000000000001",
    "10000000000001",
    "11111111111111"
].map(row => Array.from(row, Number));

let res = search(board, 6, 2, 2);

console.log("number of cells reached: ", res);

console.log(board.map(row => row.join(" ")).join("\n"));
like image 107
trincot Avatar answered Dec 20 '25 22:12

trincot


You should use Breadth-First-Search to accomplish your problem. You can read up about BFS here. Below is my unran code in Java. I recommend not using that because I coded it in the StackOverflow editor, but the basic idea is there.

public class BFS {
    static final int MAX_N = 100;
    public static void main(String[] args) {
         int[][] board = new int[MAX_N][MAX_N];
         Queue<Point> q = new LinkedList<>();
         List<int[]> reachable = new ArrayList<>();
         boolean[][] vist = new boolean[MAX_N][MAX_N];
         q.add(new Point(0,0,0));
         vist[0][0] = true;
         while(!q.isEmpty()) {
             Point curr = q.poll();

             if(vist[curr.x][curr.y]) continue;
             if(curr.move > N) continue;
 
             reachable.add(new int[]{curr.x, curr.y});

             // dx and dy array not shown
             for(int i = 0; i < 4; i++) {
                 int nx = curr.x + dx[i];
                 int ny = curr.y + dy[i];

                 if(nx < 0 || nx >= MAX_N || ny < 0 || ny >= MAX_N) continue;
                 if(board[nx][ny] == 1) continue;

                 vist[nx][ny] = true;
                 q.add(new Point(nx, ny, curr.move+1));
                 
             }
         }

         // You now have your reachable points.
    }
}

class Point {
    public int x, y, move;
    public Point(int x, int y, int move) {
        this.x = x;
        this.y = y;
        this.move = move;
    }
}
like image 32
Qi Wang Avatar answered Dec 21 '25 00:12

Qi Wang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!