java eight_queue

public class Test {

public void calmaze(int[][] pmaze, int nlvl){

for(int i = 0; i < pmaze.length; i++){

if(justplay(pmaze, nlvl, i)){

pmaze[nlvl][i] = 1;

}else{

continue;

}

if(nlvl == 7){

display(pmaze);

}else{

calmaze(pmaze, nlvl + 1);

}

for(int j = 0; j < pmaze.length; j++){

pmaze[nlvl][j] = 0;

}

}

}

public boolean justplay(int[][] pmaze, int row, int col){

boolean flag = false;

if(row == 0)

return true;

int tmprow = row, tmpcol = col;

while(tmprow > 0 && tmpcol > 0){

if(pmaze[tmprow - 1][tmpcol - 1] == 1){

return flag;

}

tmprow--;

tmpcol--;

}

tmprow = row;

tmpcol = col;

while(tmprow > 0 && tmpcol<pmaze.length-1){

if(pmaze[tmprow - 1][tmpcol + 1] == 1){

return flag;

}

tmprow--;

tmpcol++;

}

tmprow = row;

while(tmprow > 0){

if(pmaze[tmprow - 1][col] == 1){

return flag;

}

tmprow--;

}

return true;

}

public void display(int[][] pmaze){

int i = 0, j = 0;

System.out.println("the "+seq++ +"th times");

for(i = 0; i < pmaze.length; i++){

for(j = 0; j < pmaze[i].length; j++){

if(pmaze[i][j] == 0){

System.out.print("*");

}else{

System.out.print("#");

}

}

System.out.println();

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章