八皇后问题c语言版(xcode下通过)

 1 int arr[8][8] = {0}; //arr[row][col];
 2 
 3 
 4 //表示第几个棋子
 5 int check(int row,int col){
 6   
 7     //1,同一列不能有皇后
 8     for(int i = 0; i < 8; i++){
 9         if(arr[i][col] == 1){
10             return 0;
11         }
12     }
13     
14     //2,左斜上方,不能有皇后。
15     for(int i = row, j = col; i >= 0 && j >= 0; i--,j--){
16         if(arr[i][j] == 1){
17             return 0;
18         }
19     }
20     
21     //3,右上方,不能有皇后
22     for(int i = row, j = col; i >= 0 && j < 8; i--,j++){
23         if(arr[i][j] == 1){
24             return 0;
25         }
26     }
27     
28     return 1;
29 }
30 
31 void printfArr(){
32     for(int list = 0; list < 8; list++){
33         for(int line = 0; line < 8; line++){
34             if(arr[list][line] == 1){
35                 printf("(%d,%d)",list,line);
36             }
37         }
38     }
39     printf("
");
40 }
41 
42 int count = 0;
43 void eightQueue(int row){
44     
45     if(row > 7){
46         printfArr();
47         count++;
48         return ;
49     }
50     
51     for(int col = 0; col < 8; col++){
52         if(check(row,col) == 1){
53             arr[row][col] = 1;
54             eightQueue(row + 1);
55             arr[row][col] = 0;
56         }
57     }
58 }
原文地址:https://www.cnblogs.com/etata/p/12365129.html