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);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值