如何在此递归代码中摆脱无限循环?

时间:2019-08-08 09:03:58

标签: java recursion return recursive-backtracking

我正在使用Backjumping算法实现N-Queen问题求解器,并且在递归调用中遇到了无限循环错误。

我主要是在返回函数时遇到了麻烦。我认为在设计递归调用时出错。

package Backjumping;

import org.python.google.common.primitives.Ints;

import java.util.*;

public class Backjumping {

    int size;
    List<Integer> columns;
    int numberofplaces;
    int numberofbacktracks;
    HashMap<Integer, List<Integer>> conflict;
    boolean noBreak = true;

    Backjumping(int size) {
        this.size = size;
        columns = new ArrayList();
        conflict = new HashMap<>(size);
        for (int i = 0; i < size; i++) {
            conflict.put(i, new ArrayList<>());
        }
    }


    List place(int startRow) {
        if (columns.size() == size) {
            System.out.println("Solution Found! The board size was :" + size);
            System.out.println(numberofplaces + " total nodes assigned were made.");
            System.out.println(numberofbacktracks + " total backtracks were executed.");
            return this.columns;
        } else {
            for (int row = 0; row < size; row++) {
                if (isSafe(columns.size(), row)) {
                    if (indexExists(columns, columns.size()))
                        columns.set(columns.size(), row);
                    else
                        columns.add(columns.size(), row);
                    numberofplaces += 1;
                    return place(startRow);
                }

            }
            if (noBreak) {
                List<Integer> max_check = conflict.get(columns.size());
                int lastRow = Collections.min(max_check);
                numberofbacktracks += 1;
                conflict.replace(columns.size(), new ArrayList<>());
                int previous_variable = columns.remove(lastRow);
                return place(previous_variable);

            }
        }
        return this.columns;
    }

    private boolean isSafe(int cols, int rows) {

        for (int threatrow : columns) {
            int threatcol = columns.indexOf(threatrow);
            if (rows == threatrow || cols == columns.indexOf(threatrow)) {
                (conflict.get(cols)).add(threatcol);
                return false;
            } else if ((threatrow + threatcol) == (rows + cols) || (threatrow - threatcol) == (rows - cols)) {
                (conflict.get(cols)).add(threatcol);
                return false;
            }
        }
        return true;
    }

    public boolean indexExists(final List list, final int index) {
        return index >= 0 && index < list.size();
    }

    public static void main(String[] args) {
        System.out.println("Enter the size of board");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Backjumping bj = new Backjumping(n);
        double start = System.currentTimeMillis();
        List cols = bj.place(0);
        double end = System.currentTimeMillis();
        System.out.println("Time to solve in second       = " + (end - start) * 0.001 + " s");
        System.out.print("Ths solution is : ");
        cols.forEach(i -> System.out.print(((int) i + 1) + ", "));
        System.out.println("\n\nPlotting CSP result on N_Queens board");
        System.out.println("......................................\n");
        bj.getBoardPic(n, cols);
    }

    public void getBoardPic(int size, List columns) {
        int[] cols = Ints.toArray(columns);

        int[][] matrix = new int[size][size];

        for (int a = 0; a < size; a++) {
            int j = cols[a];
            matrix[a][j] = 1;
        }

        for (int a = 0; a < size; a++) {
            for (int b = 0; b < size; b++) {
                if (matrix[b][a] == 1)
                    System.out.print(" Q ");
                else
                    System.out.print(" - ");
            }
            System.out.println();
        }
    }
}


主要错误是,当我在row=0中分配for (int row = 0; row < size; row++)时,n = 6的输入大小错误,而其他值正确。

当我在row=startrow中分配for (int row = startrow; row < size; row++)时,n = 6的输入大小正确,其他值是错误的。

0 个答案:

没有答案