問題背景:八皇後問題是一個以國際象棋為背景的問題:如何能夠在 8×8 的國際象棋棋盤上放置八個皇後,使得任何一個皇後都無法直接吃掉其他的皇後。為了達到這個目的, 任兩個皇後都不能處於同一條橫行、縱行或斜線上。
以下的代碼給出的解法應該是最容易理解的一種方法,本問題還可以用回溯法和遞歸解決,理論上效率會比該方法好很多。 1 #include<stdio.h>
2
3 int chess[8][8];
4 int count = 0;
5
6 void display(){
7 int row,col;
8 for(row = 0; row < 8; row++){
9 for(col = 0; col < 8; col++){
10 if(chess[row][col] == 1){
11 printf("%c ",45);
12 }
13 else if(chess[row][col] == 2){
14 putchar(1);
15 printf("%c",32);
16
17 }
18
19 }
20 printf("\n");
21 }
22 getch();
23 }
24 void setLimitsArea(int row,int col){
25 int i,j;
26 for(j = 0; j < 8; j++)
27 if(col != j)
28 chess[row][j] = 1;
29 for(i = 0; i < 8; i++)
30 if(row != i)
31 chess[i][col] = 1;
32 for(i = 1; row - i >= 0 && col - i >= 0; i++)
33 chess[row - i][col - i] = 1;
34 for(i = row + 1,j = col + 1; i < 8 && j < 8; i++,j++)
35 chess[i][j] = 1;
36 for(i = 1; row - i >= 0 && col + i < 8; i++)
37 chess[row - i][col + i] = 1;
38 for(i = row + 1,j = col - 1; i < 8 && j >=0; i++,j--)
39 chess[i][j] = 1;
40 }
41 void eightQueensProblem(){
42 int row1,row2,row3,row4,row5,row6,row7,row8;
43 int i, j;
44 for(row1 = 0;row1 < 8;row1++){
45 for(row2 = 0;row2 < 8;row2++){
46 for(row3 = 0;row3 < 8;row3++){
47 for(row4 = 0;row4 < 8;row4++){
48 for(row5 = 0;row5 < 8;row5++){
49 for(row6 = 0;row6 < 8;row6++){
50 for(row7 = 0;row7 < 8;row7++){
51 for(row8 = 0;row8 < 8;row8++){
52 for(i = 0;i < 8;i++)
53 for(j = 0;j < 8;j++)
54 chess[i][j] = 0;
55 chess[0][row1] = 2;
56 setLimitsArea(0,row1);
57 if(chess[1][row2] == 0){
58 chess[1][row2] = 2;
59 setLimitsArea(1,row2);
60 if(chess[2][row3] == 0){
61 chess[2][row3] = 2;
62 setLimitsArea(2,row3);
63 if(chess[3][row4] == 0){
64 chess[3][row4] = 2;
65 setLimitsArea(3,row4);
66 if(chess[4][row5] == 0){
67 chess[4][row5] = 2;
68 setLimitsArea(4,row5);
69 if(chess[5][row6] == 0){
70 chess[5][row6] = 2;
71 setLimitsArea(5,row6);
72 if(chess[6][row7] == 0){
73 chess[6][row7] = 2;
74 setLimitsArea(6,row7);
75 if(chess[7][row8] == 0){
76 chess[7][row8] = 2;
77 count = count + 1;
78 printf(" NO %d Method \n",count);
79 display();
80 }
81 }
82 }
83 }
84 }
85 }
86 }
87 }
88 }
89 }
90 }
91 }
92 }
93 }
94 }
95 }
96
97 int main(){
98 int row = 0,col = 0;
99 for(row = 0; row < 8; row++)
100 for(col = 0; col < 8; col++)
101 chess[row][col] = 0;
102 eightQueensProblem();
103 return 0;
104 }
代碼思路簡介:setLimitArea的作用是當在chess[row][col]放置了皇後之後(該元素的值設置為2)。將該元素所在的行,所在的列,主對角線,和斜對角線上的元素都設置為1(原來整個數組元 素的值都為0),表示以後的皇後不可以放在這裡。
eightQueensProblem函數對八個皇後的每一種放置方法(每行放置一個,每行從位置0到7嘗試)進行判斷。將符合的輸出。
dispaly僅僅是打印函數,但是將表示放置了皇後的2在顯示的時候顯示為一個笑臉符號,將1表示為了一條線。