八皇后 答案

我使用的穷举法 java写的

 class MyQueen{
 public static void main(String[] args){
  int count=0;
  for(int row1=1; row1<=8; row1++){
   for(int row2=1; row2<=8; row2++){
    for(int row3=1; row3<=8; row3++){
     for(int row4=1; row4<=8; row4++){
         for(int row5=1; row5<=8; row5++){
       for(int row6=1; row6<=8; row6++){
        for(int row7=1; row7<=8; row7++){
         for(int row8=1; row8<=8; row8++){
          if(row2!=row1&&row2!=row1-1&&row2!=row1+1&&row3!=row1&&row3!=row1-2&&row3!=row1+2&&row4!=row1&&row4!=row1-3&&row4!=row1+3&&row5!=row1&&row5!=row1-4&&row5!=row1+4&&row6!=row1&&row6!=row1-5&&row6!=row1+5&&row7!=row1&&row7!=row1-6&&row7!=row1+6&&row8!=row1&&row8!=row1-7&&row8!=row1+7)
           if(row3!=row2&&row3!=row2-1&&row3!=row2+1&&row4!=row2&&row4!=row2-2&&row4!=row2+2&&row5!=row2&&row5!=row2-3&&row5!=row2+3&&row6!=row2&&row6!=row2-4&&row6!=row2+4&&row7!=row2&&row7!=row2-5&&row7!=row2+5&&row8!=row2&&row8!=row2-6&&row8!=row2+6)
            if(row4!=row3&&row4!=row3-1&&row4!=row3+1&&row5!=row3&&row5!=row3-2&&row5!=row3+2&&row6!=row3&&row6!=row3-3&&row6!=row3+3&&row7!=row3&&row7!=row3-4&&row7!=row3+4&&row8!=row3&&row8!=row3-5&&row8!=row3+5)
             if(row5!=row4&&row5!=row4-1&&row5!=row4+1&&row6!=row4&&row6!=row4-2&&row6!=row4+2&&row7!=row4&&row7!=row4-3&&row7!=row4+5&&row8!=row4&&row8!=row4-4&&row8!=row4+4)
              if(row6!=row5&&row6!=row5-1&&row6!=row5+1&&row7!=row5&&row7!=row5-2&&row7!=row5+2&&row8!=row5&&row8!=row5-3&&row8!=row5+3)
               if(row7!=row6&&row7!=row6-1&&row7!=row6+1&&row8!=row6&&row8!=row6-2&&row8!=row6+2)
                if(row8!=row7&&row8!=row7-1&&row8!=row7+1){
                 count++;
                }
         }
        }
       }
      }
     }
    } 
   }
  }
 System.out.println("totle:"+ count);
 }


}

答案是:98种
 

你可能感兴趣的:(八皇后)