تبليغاتX
وبلاگ امید - هشت وزير
با کلیک روی ستاره یک امتیاز به این مطب بده
قرار دا دن صفحه شطرنج به گونه اي که هيچ وزيري ديگري را تهديد نکند
     

public class Queen{
public static void main(String args[]){
int
      chessboard[][] = new int[8][8];
int
      i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
chessboard[i][j]=0;
putQueenInRow(chessboard,0);
for(i=0;i<8;i++)
for(j=0;j<8;
      j++)
if (chessboard[i][j] ==
      1)
System.out.println("["+(i+1)+"]["+(j+1)+"]");
}
public static
      boolean putQueenInRow(int board[][],int row){
for(int
      i=0;i<8;i++)
{
if(isFreeCell(board,row,i))
{
board[row][i]=1;
if
      (row==7 || putQueenInRow(board,row+1)) return
      true;
board[row][i]=0;
}
}
return false;
}
public static
      boolean isFreeCell(int board[][],int row,int col)
{
int
      i,j;
i=row-1;
j=col-1;
while(i >= 0 && j >=
      0)
if(board[i--][j--]==1) return
      false;
i=row-1;
j=col+1;
while(i >= 0 && j <
      8)
if(board[i--][j++]==1) return false;
i=row-1;
while(i >=
      0)
if(board[i--][col]==1) return false;
return
      true;
}
}

نوشته شده توسط امید الحاقی در پنجشنبه بیست و دوم آذر 1386 ساعت 23:8 | لینک ثابت |
 
Powered By Blogfa - Designing & Supporting Tools By WebGozar