八皇后有关问题及控制台动态演示

www.MyException.Cn  网友分享于：2013-10-27  浏览：0次

``` 1 bool Is_meet(int row,int col) {
2     for (int i = 0;i < row;i++) {
3         if (col == queen[i])
4             return 0;
5         if ((row + col) == (i + queen[i]))
6             return 0;
7         if ((row - col) == (i - queen[i]))
8             return 0;
9     }
10     queen[row] = col;
11     return 1;
12 }```

``` 1 void findQueenPosition(int row) {
2     for (int j_col = 0;j_col < 8;j_col++) {    //j_col变量用于遍历每一列
3         if (Is_meet(row, j_col)) {
4             if (row == 7) {　　　　　　　　　　　　//如果已经是最后一行了就可以退出循环
5                 break;
6             }
7             else {
8                 findQueenPosition(row + 1);　　//进行递归
9             }
10         }
11     }
12     queen[row] = -1;
13     return;
14 }```

``` 1 void display() {
2     system("cls");
3     cout << "八皇后问题动态演示:" << endl;
4     for (int i = 0;i < 8;i++) {
5         if (queen[i] == -1) {
6             for (int j = 0;j < 8;j++)
7                 cout << ". ";
8             cout << endl;
9         }
10         else {
11             for (int j = 0;j < queen[i];j++) {
12                 cout << ". ";
13             }
14             cout << "# ";
15             for (int k = queen[i] + 1;k < 8;k++) {
16                 cout << ". ";
17             }
18             cout << endl;
19         }
20     }
21     Sleep(100);//停留时间可以自行更改
22 }```

``` 1 void findQueenPosition(int row) {
2     for (int j_col = 0;j_col < 8;j_col++) {
3         if (Is_meet(row, j_col)) {
4             if (row == 7) {
5                 display();
6                 cout << "Case " << ++_count << "." << endl;
7                 Sleep(3000);//停留时间可以自行更改
8                 break;
9             }
10             else {
11                 display();
12                 findQueenPosition(row + 1);
13             }
14         }
15     }
16     queen[row] = -1;
17     return;
18 }```

``` 1 #include <iostream>
2 #include <windows.h>
3 using namespace std;
4
5 int queen[8]={-1,-1,-1,-1,-1,-1,-1,-1};
6 int _count = 0;
7
8 bool Is_meet(int row,int col) {
9     for (int i = 0;i < row;i++) {
10         if (col == queen[i])
11             return 0;
12         if ((row + col) == (i + queen[i]))
13             return 0;
14         if ((row - col) == (i - queen[i]))
15             return 0;
16     }
17     queen[row] = col;
18     return 1;
19 }
20
21 void display() {
22     system("cls");
23     cout << "八皇后问题动态演示:" << endl;
24     for (int i = 0;i < 8;i++) {
25         if (queen[i] == -1) {
26             for (int j = 0;j < 8;j++)
27                 cout << ". ";
28             cout << endl;
29         }
30         else {
31             for (int j = 0;j < queen[i];j++) {
32                 cout << ". ";
33             }
34             cout << "# ";
35             for (int k = queen[i] + 1;k < 8;k++) {
36                 cout << ". ";
37             }
38             cout << endl;
39         }
40     }
41     Sleep(100);//停留时间可以自行更改
42 }
43
44 void findQueenPosition(int row) {
45     for (int j_col = 0;j_col < 8;j_col++) {
46         if (Is_meet(row, j_col)) {
47             if (row == 7) {
48                 display();
49                 cout << "Case " << ++_count << "." << endl;
50                 Sleep(3000);//停留时间可以自行更改
51                 break;
52             }
53             else {
54                 display();
55                 findQueenPosition(row + 1);
56             }
57         }
58     }
59     queen[row] = -1;
60     return;
61 }
62
63 int main() {
64     findQueenPosition(0);
65     cout << "The total case of eight queen problem is " << _count << endl;
66     return 0;
67 }```