当前位置:编程学习 > JAVA >>

N后问题的优先队列分支限界算法实现(出不来结果啊)

求大神帮助,改个代码~~
  import java.util.LinkedList;


public class Queen3 {
static LinkedListBorder queue = new LinkedListBorder();

public static void main(String args[]){
for(int i=0;iBorder.SIZE;i++){
Border b = new Border();
b.border[0][i] = true;
queue.addLast(b);
}

do{
Border tempB = queue.peek();
place(tempB);
}
while(!queue.isEmpty());
}

static void place(Border fb){
for(int i=0;iBorder.SIZE;i++){
if(!fb.isUnderAttack(fb.pRow+1,i)){
Border b = new Border(fb,fb.pRow+1);
b.border[b.pRow][i] = true;
if(b.pRow=Border.SIZE-1){
print(b);
}
else Queen3.queue.addLast(b);
}
}
Queen3.queue.pollFirst();
}

public static void print(Border b){
for(int i=0;iBorder.SIZE;i++){
for(int j=0;jBorder.SIZE;j++){
if(b.border[i][j])
System.out.print(1 );
else System.out.print(0 );
}
System.out.println();
}
System.out.println();
}
}


class Border{
static final int SIZE = 8;
boolean[][] border  ;
int pRow;       准备放置皇后的行
Border(){
border = new boolean[SIZE][SIZE];
pRow = 0;
}
Border(Border b,int row){    
border = new boolean[SIZE][SIZE];
pRow = row;
for(int i=0;ipRow;i++)
for(int j=0;jSIZE;j++)
border[i][j] = b.border[i][j];
}

boolean isUnderAttack(int row,int col){
int i,j;
j=row-1;
while(j=0){
if(border[j][col]) return true;
j--;
}
i=row-1;j=col-1;
while(i=0&&j=0){
if(border[i][j]) return true;
i--;
j--;
}
i=row-1;j=col+1;
while(i=0&&jSIZE){
if(border[i][j]) return true;
i--;
j++;
}


return false;
}

boolean placeFine(){
return (pRow=SIZE);
}

}
算法 N后问题
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,