0

I am working on the following problem:

Write a method that, given a chessboard with one knight, rocks on some of the squares, and a target position, indicates whether or not the knight can reach the target without landing on any rocks, and if so, the smallest number of moves needed by the knight to reach the target. The method should return the minimum number of moves needed to do so; otherwise, the method should return the value -1. (If the initial position has a rock on it, the method should return -1; likewise, if the target position has a rock on it, the method should return -1.)

You can see the code I've implemented so far below. My approach to rocks is to change the "coordinates" on the chessboard that have rocks as visited, so the knight can't revisit them, hence blocking his path(?).

My program compiles but doesn't return either minimum moves or -1. Any tips/different approaches to the problem are much appreciated. Thanks!!

PS: I'm ridiculously new to Java so apologies in advance for the messy code :)

import java.util.*;

public class Knight {

    public static int numMoves( int dim, int xstart, int ystart, int xtarget,
            int ytarget, int[] xrock, int[] yrock )
    {
        int result = -1;

        List<Integer> knightPos = new ArrayList<>(Arrays.asList(xstart, ystart));
        int [] targetPos = {xtarget, ytarget};
        int dis = 0;

        // x and y direction, where a knight can move
        int[] dx = { -2, -1, 1, 2, -2, -1, 1, 2 };
        int[] dy = { -1, -2, -2, -1, 1, 2, 2, 1 };

        // queue for storing states of knight in board
        Vector<cell> q = new Vector<>();

        // push starting position of knight with 0 distance
        q.add(new cell(knightPos.get(xstart), knightPos.get(ystart), dis));

        cell t;
        int x, y;
        boolean[][] visit = new boolean[dim + 1][dim + 1];

        // make all cell unvisited
        for (int i = 1; i <= dim; i++) {
            for (int j = 1; j <= dim; j++) {
                visit[i][j] = false;
            }
        }

        // visit starting state
        visit[knightPos.set(0,xstart)][knightPos.set(1,ystart)] = true;

        // visit rock squares
        for (int i = 0; i < xrock.length;) {
            for (int j = 0; j < yrock.length; ++i, ++j) {
                visit[knightPos.get(i)][knightPos.get(j)] = true;
            }
        }

        // loop until we have one element in queue
        while (!q.isEmpty()) {
            t = q.firstElement();
            q.remove(0);

            // if current cell is equal to target cell,
            // return its distance
            if (t.x == targetPos[0] && t.y == targetPos[1])
                return t.dis;

            // loop for all reachable states
            for (int i = 0; i < 8; i++) {
                x = t.x + dx[i];
                y = t.y + dy[i];

                // If reachable state is not yet visited and
                // inside board, push that state into queue
                if (isInside(x, y, dim) && !visit[x][y]) {
                    visit[x][y] = true;
                    q.add(new cell(x, y,dis + 1));
                }
            }
        }
        return result;
    }


    public static boolean isInside (int x, int y, int dim)
    {
        return x >= 0 && x <= dim && y >= 0 && y <= dim;
    }


    static class cell {
        int x, y;
        int dis;

        public cell(int x, int y, int dis) {
            this.x = x;
            this.y = y;
            this.dis = dis;
        }

    }

    public static void main( String[] args )
    {

    }

}
Wolf
  • 9,679
  • 7
  • 62
  • 108
ppan92
  • 1
  • 1
  • 1
    It says rocks, but I don't think it makes any difference. They are just nodes that the knight can't visit – ppan92 Jan 26 '21 at 17:54
  • Okay, clarified. We can take it as blocked fields, nice task! Maybe it could be easier to handle (mentally) if we had a class for a more compact representation of cell coordinates (x, y). – Wolf Jan 26 '21 at 18:00
  • Is there a programming language you are more familiar with? Then it might be a good idea to solve the problem there first and port the solution to Java afterwards. – Wolf Jan 26 '21 at 18:05
  • I started teaching myself Python and I've done some projects there. Much more "fluent" in Python. Good recommendation, thanks! – ppan92 Jan 26 '21 at 18:07
  • It may be also good to start with a smaller board, for example [5x5](https://stackoverflow.com/a/54592520/2932052); 8x8 will be unsolvable via brute force, see [Knight's tour - Wikipedia](https://en.wikipedia.org/wiki/Knight%27s_tour) – Wolf Jan 26 '21 at 18:42
  • 1
    @Wolf why is brute force relevant to BFS ? – c0der Feb 02 '21 at 05:58
  • 1
    **_"doesn't return either minimum moves or -1"_** Please add hard-coded test data and the expected result so we can reproduce it – c0der Feb 02 '21 at 06:00
  • @c0der My comment including brute force isn't replying to the BFS tag but to the problem in general. I have to admit that I haven't much experience with things like this, however, starting with a smaller version does not harm (normally). – Wolf Feb 02 '21 at 06:52

0 Answers0