递归Java编程,Knight's Tour让我疯狂

时间:2017-03-09 15:46:09

标签: java recursion

我一直在做一个学校项目,但无法弄清楚问题。骑士跳回到最后一步发生死角时的问题。

我已经为4x4测试添加了输出,你可以清楚地看到当骑士从12号看到死路时,骑士跳回到第11号。然后从转弯继续11号和#34;解决了旅行"。

如果模式没有解决问题,我也不确定如何继续。因为那时我需要以某种方式记录该模式,以便我不会再次使用相同的模式。对不起我的坏Engligh并提前感谢。

table.loc[table.student.duplicated(), "student"] = ""
table

这里是4x4的输出:

        package knightsTour;

    import java.util.Scanner;
    import java.util.ArrayList;

    public class KnightsTour
    {
        private static int turns = 0;
        private static ArrayList<String> moves = new ArrayList<String>();

        private static int squares;
        private static int table[][];

        private static boolean takeTour(int x, int y) {
            // Checks if all squares is used. If true, algorithm will stop
            if (checkIfFinished())
                return true;
            table[x][y] = ++turns;
            // 2 Left, 1 Down
            if (x > 1 && y < squares -1 && table[x-2][y+1] == 0)
            {
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Left, 1 Down");
                if (takeTour(x-2, y+1))
                {
                    return true;
                }
            }
            // 2 Left, 1 Up
            if (x > 1 && y > 0 && table[x-2][y-1] == 0)
            {
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Left, 1 Up");
                if (takeTour(x-2, y-1))
                {
                    return true;
                }
            }
            // 2 Up, 1 Left
            if (y > 1 && x > 0 && table[x-1][y-2] == 0)
            {
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Up, 1 Left");
                if (takeTour(x-1, y-2))
                {
                    return true;
                }
            }
            // 2 Up, 1 Right
            if (y > 1 && x < squares -1 && table[x+1][y-2] == 0)
            {
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Up, 1 Right");
                if (takeTour(x+1, y-2))
                {
                    return true;
                }
            }
            // 2 Right, 1 Up
            if (x < squares -2 && y > 0 && table[x+2][y-1] == 0)
            {
                System.out.println("x:" + x + ", y:" + y + " (2r,1u)moving to x:" + (x+2) + ", y:" + (y-1));
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Right, 1 Up");
                if (takeTour(x+2, y-1))
                {
                    return true;
                }
            }
            // 2 Right, 1 Down
            if (x < squares -2 && y < squares -1 && table[x+2][y+1] == 0)
            {
                System.out.println("x:" + x + ", y:" + y + " (2r,1d)moving to x:" + (x+2) + ", y:" + (y+1));
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Right, 1 Down");
                if (takeTour(x+2, y+1))
                {
                    return true;
                }
            }
            // 2 Down, 1 Right
            if (y < squares -2 && x < squares-1 && table[x+1][y+2] == 0)
            {
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Down, 1 Right");
                if (takeTour(x+1, y+2))
                {
                    return true;
                }
            }
            // 2 Down, 1 Left
            if (y < squares -2 && x > 0 && table[x-1][y+2] == 0)
            {
                moves.add("X: " + x + ", Y: " + y + ". Moving 2 Down, 1 Left");
                if (takeTour(x-1, y+2))
                {
                    return true;
                }
            }
            return false;
        }

        // Checks if all squares is used
        private static boolean checkIfFinished()
        {
            for (int i = 0; i < squares; i++)
            {
                for (int j = 0; j < squares; j++)
                {
                    if (table[i][j] == 0)
                        return false;
                }
            }
            return true;
        }

        // Made this to save code from 3 duplicates
        private static void invalidNumber()
        {
            System.out.println("Invalid number! Killing proccess");
            System.exit(0);
        }

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);
            System.out.print("Number of squares: ");
            squares = Integer.parseInt(sc.nextLine());
            if (squares < 1 )
                invalidNumber();

            System.out.println("Note: Start values is from 0 -> n-1"
                            + "\n0,0 is at top left side");
            System.out.print("X start value: ");
            int x = Integer.parseInt(sc.nextLine());
            if (x < 0 || x > squares -1)
                invalidNumber();

            System.out.print("Y start value: ");
            int y = Integer.parseInt(sc.nextLine());
            if (y < 0 || y > squares -1)
                invalidNumber();
            sc.close();

            table = new int[squares][squares];

            boolean tourComplete = takeTour(x, y);

            for (String s : moves)
            {
                System.out.println(s);
            }
            if (!tourComplete)
                System.out.println("Did not find any way to complete Knights Tour!");

            // Print the table with the move-numbers
            for (int i = 0; i < squares; i++)
            {
                for (int j = 0; j < squares; j++)
                {
                    System.out.printf("%4d", table[j][i]);
                }
                System.out.println();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您最好的选择是在递归调用的方法中添加List<Point> visited。我还会将您的int xint y参数更改为Point。通过这种方式,您可以直接调用visited.contains(point)来确定您是否已经对Point进行了测试。在这种情况下,您不会使用该特定Point进行递归调用,只需转到下一个。

答案 1 :(得分:0)

我发现了问题!我必须在takeTour(int x,int y)中的每个第8个if(takeTour(x + -a,y + -b))语句中放置else块,然后返回false。现在我只是想知道我将如何跟踪模式以便我可以返回一步并尝试新的模式