Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

breadth first search - Knight's tour in Java with rocks

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

    }

}
question from:https://stackoverflow.com/questions/65906446/knights-tour-in-java-with-rocks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...