如果在同一列,列号一样;同一行,行号一样; 如果在/左斜线上,行列之和一样; 如果在\右斜线上,行列之差一样 # include <iostream> using namespace std ; bool d [ 17 ] = { 0 } , b [ 9 ] = { 0 } , c [ 17 ] = { 0 } ; //b用来判断列有没有被占领 //c是c和d都是斜方向上的线 int sum = 0 , a [ 9 ] ; //表示皇后的位置 void search ( int i ) { int j ; for ( j = 1 ; j <= 8 ; j ++ ) if ( ( ! b [ j ] ) && ( ! c [ i + j ] ) && ( ! d [ i - j + 7 ] ) ) //如果列未占领&& { a [ i ] = j ; b [ j ] = 1 ; c [ i + j ] = 1 ; d [ i - j + 7 ] = 1 ; if ( i == 8 ) //摆放完了就输出呗 { int j ; sum ++ ; cout << "sum=" << sum << endl ; for ( int j = 1 ; j <= 8 ; j ++ ) cout << a [ j ] << " " ; cout << endl ; } else search (