-1
私はn-queensの問題に取り組んでおり、これまでの論理が正しいかどうかをテストしています。私のループは出力を停止し、2番目のクイーンピースを調整して無限ループに入り、競合がないようにします。Java、Stacks、LinkedListsの無限ループ
私は基本的に私のロジック、と無限ループを得るだろうとは思いませんでした: プッシュ(1,1)
競合
ためのチェック紛争の場合、トップ女王を調整し、それは調整できない場合は、明らかに競合の競合
チェックだろう-which(1、サイズ+ 1)無紛争やサイズ< 8、プッシュした場合、新しいトップ
を調整し、オフポップ など
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
なかったときに決定余分else文がありませんでしたそれはループの部分に戻って、NO CONFLICTを表示しますが、サイズは8未満です。これは、私がplayChess()をコメントアウトするときです。 – jackie
wh何かが紛争ではなかった。 – jackie