-1

私は n-queens 問題に取り組んでおり、これまでのことをテストして、私の論理が正しいかどうかを確認しています。競合がないように2番目のクイーンピースを調整した後、私のループ停止は出力を停止し、無限ループに入ります。

私のロジックで無限ループが発生するとは思いませんでした。これは基本的に次のとおりです。 Push (1,1)

競合をチェックする

競合する場合はトップ クイーンを調整し、調整できない場合はポップオフし、新しいトップを調整します

競合がなく、サイズが 8 未満の場合は、(size+1, 1) をプッシュします。これは明らかに競合です。

競合などのチェック

    public static boolean conflictCheck() {
    QueenNode temp = head;
    //walk through stack and check for conflicts

    while(temp!=null) {
        //if there is no next node, there is no conflict with it
        if (temp.getNext() == null){
            System.out.println("No next node");
            if (queens.size() < 8 ) {
                System.out.println("No problems");
                return false;
            }
        }
        else if (temp.getRow() ==temp.getNext().getRow() || temp.getColumn() == temp.getNext().getColumn() ||
                diagonal(temp, temp.getNext())){
            System.out.println("There's a conflict");
            return true;
        }
    }
    return false;
}

public static void playChess() {
    System.out.println("Playing chess");
    if (conflictCheck()) {
        if (head.getColumn() == 8) {
            queens.pop();
        }
        if (!queens.isEmpty()) {
            System.out.println("Adjusting head");
            head.setColumn(head.getColumn()+1);
            System.out.println("Head is now " + head.getRow() + ", " + head.getColumn());
            playChess();

        }
    }
    else if (!conflictCheck() && queens.size() < 8) {
        System.out.println("Stack isn't full yet");
        queens.push(queens.size()+1,1);
        playChess();
        }
    else {
        success= true;
        System.out.println("Success");
        queens.viewPieces();
        return;
    }
}

public static void main(String[] args) {
    queens.push(1, 1);
    queens.viewPieces();
    success = false;
    playChess();
}

}

私の出力は次のとおりです。

The stack
1, 1
End of stack
Playing chess
No next node
No problems
No next node
No problems
Stack isn't full yet
Playing chess
There's a conflict
Adjusting head
Head is now 2, 2
Playing chess
problem
There's a conflict
Adjusting head
Head is now 2, 3
Playing chess
4

1 に答える 1

1

何かが競合ではなかったときに決定する追加のelseステートメントがありませんでした

于 2012-06-24T21:51:54.323 に答える