2754:八皇后,考点:递归回溯

原题:八皇后输出所有解

          八皇后输出指定串

描述

由于八皇后是递归经典问题,略去描述,不同题目背景相似,只是输出不同。

输出所有解

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 char board[9][9];
 5 bool righttop[16] = {}, lefttop[16] = {}, row[9] = {};
 6 int solutionNumber = 0;
 7 void queen(int col) 
 8 {
 9     if (col == 9) {
10         cout << "No. " << ++solutionNumber << endl;
11         for (int i = 1; i <= 8; i++) {
12             for (int j = 1; j <= 8; j++)
13                 cout << board[i][j] << ' ';
14             cout << endl;
15         }
16         return;
17     }
18     for (int i = 1; i <= 8; i++) {
19         if (row[i] || righttop[i + col - 1] || lefttop[8 + i - col])
20             continue;
21         board[i][col] = '1';
22         row[i] = true;
23         righttop[i + col - 1] = true;
24         lefttop[8 + i - col] = true;
25         queen(col + 1);
26         board[i][col] = '0';
27         row[i] = false;
28         righttop[i + col - 1] = false;
29         lefttop[8 + i - col] = false;
30     }
31 }
32 
33 int main()
34 {
35     memset(board, '0', sizeof(board));
36     queen(1);
37 }

输出指定串

这份代码没有AC,但我把所有串都输出与一份AC代码进行了比较,没有任何不同,但我提交后就是WA,我也找不到bug了╭(╯^╰)╮

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 char board[9][9];
 6 bool righttop[16] = {}, lefttop[16] = {}, row[9] = {};
 7 int solutionNumber = 0;
 8 char outputs[92][9];
 9 void queen(int col) 
10 {
11     if (col == 9) {
12         for (int i = 1; i <= 8; i++) {
13             for (int j = 1; j <= 8; j++)
14                 if (board[i][j] == '1')
15                     outputs[solutionNumber][i-1] = j + '0';
16         }
17         outputs[solutionNumber++][8] = '\0';
18         return;
19     }
20     for (int i = 1; i <= 8; i++) {
21         if (row[i] || righttop[i + col - 1] || lefttop[8 + i - col])
22             continue;
23         board[i][col] = '1';
24         row[i] = true;
25         righttop[i + col - 1] = true;
26         lefttop[8 + i - col] = true;
27         queen(col + 1);
28         board[i][col] = '0';
29         row[i] = false;
30         righttop[i + col - 1] = false;
31         lefttop[8 + i - col] = false;
32     }
33 }
34 void bubblesort()
35 {
36     for(int i=0;i<92-1;i++)
37         for (int j = 0; j < 92 - 1 - i; j++)
38         {
39             if (strcmp(outputs[j], outputs[j + 1]) == 1) {
40                 char temp[9];
41                 strcpy(temp, outputs[j]);
42                 strcpy(outputs[j], outputs[j + 1]);
43                 strcpy(outputs[j + 1], temp);
44             }
45         }
46 }
47 int main()
48 {
49     memset(board, '0', sizeof(board));
50     memset(outputs, 0, sizeof(outputs));
51     queen(1);
52     bubblesort();
53     int n;
54     cin >> n;
55     int *input = new int[n];
56     for (int i = 0; i < n; i++)
57         cin >> input[i];
58     for(int i=0;i<n;i++)
59     {
60         for (int j = 0; j < 8; j++)
61             cout << outputs[input[i] - 1][j] - '0';
62         cout << endl;
63     }
64     return 0;
65 }

 

posted @ 2021-07-03 20:07  永远是个小孩子  阅读(62)  评论(0编辑  收藏  举报