UVA 167 The Sultan's Successors (八皇后问题、回溯)

Describe:
The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate parts on her death and each part will be inherited by whoever performs best at some test. It is possible for any individual to inherit more than one or indeed all of the portions. To ensure that only highly intelligent people eventually become her successors, the Sultan has devised an ingenious test. In a large hall filled with the splash of fountains and the delicate scent of incense have been placed k chessboards. Each chessboard has numbers in the range 1 to 99 written on each square and is supplied with 8 jewelled chess queens. The task facing each potential successor is to place the 8 queens on the chess board in such a way that no queen threatens another one, and so that the numbers on the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For those unfamiliar with the rules of chess, this implies that each row and column of the board contains exactly one queen, and each diagonal contains no more than one.) Write a program that will read in the number and details of the chessboards and determine the highest scores possible for each board under these conditions. (You know that the Sultan is both a good chess player and a good mathematician and you suspect that her score is the best attainable.)
Input:
Input will consist of k (the number of boards), on a line by itself, followed by k sets of 64 numbers, each set consisting of eight lines of eight numbers. Each number will be a positive integer less than 100. There will never be more than 20 boards.
Output:
Output will consist of k numbers consisting of your k scores, each score on a line by itself and right justified in a field 5 characters wide.
Sample Input:
2
1  2  3  4  5  6  7  8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
48 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
99 92 53 74 69 76 87 98
9 12 11 12 19 14 15 16
17 14 19 20 29 22 23 24
25 26 57 28 29 30 31 32
33 34 36 76 39 58 39 40
1 42 43 44 85 46 47 48
58 60 71 82 53 34 55 56
57 58 39 90 61 32 23 44
Sample Output:
  260
  429

 

传送门:UVA 167

本题主体为8皇后问题,还不太懂的可以点击这里:Click Here

题目大意:(我太懒了,从lucky猫那里直接复制的)

 

努比亞的蘇丹沒有子女,所以他要從一些有資格的繼承者中挑選一個出來繼承王位。他希望這個繼承者是夠聰明的,所以他決定用一個遊戲來測試這些人。

 

他準備了一個西洋棋盤,上面的每個格子中均有一個1到99的數字。他又準備了8個皇后棋子。每位參加遊戲的人必須將8個皇后放置到棋盤中,且各皇后彼此不可互相攻擊。可以想像,這樣有不只一種的放置方式。而蘇丹要挑選的繼承者就是那位可以放置8個皇后,並且放置皇后的8個位置中的數的和為最大的那一個人。

 

你的任務就是讀入棋盤上的數,幫蘇丹算出可以放置8個皇后的最大的和是多少。

解题思路:

我们知道八皇后有92种解,所以我们先离线计算出所有的解并存起来,对于每一组数,我们算出所有解的值,最后取其最大的即可,思路比较简单,直接上代码。

AC代码:

 

 1 #include 
 2 #include 
 3 using namespace std;
 4 int k;     //题目输入的k
 5 int cnt = 0; // 计数器,最后应该是92
 6 int ans[100][9]; // 用来存储8皇后的所有解
 7 int a[9];   // 回溯时用到的数组
 8 int Left[15]; // 这三个不用解释了
 9 int Right[15];
10 int col[9];
11 // 8皇后,不解释了
12 // 自己想的,可能写的和其他人又出入
13 void eight(int line)
14 {
15     if(line == 9)
16     {
17         for(int i = 1; i <= 8; i++)
18             ans[cnt][i] = a[i];
19         cnt++;
20         return;
21     }
22     if(line == 1)
23     {
24         for(int i = 1; i <= 8; i++)
25         {
26             a[1] = i;
27             col[i] = 1;
28             Left[8-i] = 1;
29             Right[i-1] = 1;
30             eight(line+1);
31             Left[8-i] = 0;
32             Right[i-1] = 0;
33             col[i] = 0;
34         }
35     } else {
36         for(int i = 1; i <= 8; i++)
37         {
38             if(col[i] == 0 && Left[line-i+7] == 0 && Right[line+i-2] == 0)
39             {
40                 a[line] = i;
41                 col[i] = 1;
42                 Left[line-i+7] = 1;
43                 Right[line+i-2] = 1;
44                 eight(line+1);
45                 col[i] = 0;
46                 Left[line-i+7] = 0;
47                 Right[line+i-2] = 0;
48             }
49         }
50     }
51 }
52 int main()
53 {
54     eight(1); // 注意别忘了要调用函数
55     int num[9][9]; // 每一次的棋盘数值
56     cin >> k;
57     while(k--)
58     {
59         int maxn = 0;  // 一个是最大值,最终答案
60         int answer = 0; // 一个是中间变量
61         for(int i = 1; i <= 8; i++) // 输入,不说了
62         {
63             for(int j = 1; j <= 8; j++)
64             {
65                 cin >> num[i][j];
66             }
67         }
68         for(int i = 0; i < cnt; i++) // 遍历92种中的每一个
69         {
70             answer = 0;  // 一定要清零,一定要清零,一定要清零
71             for(int j = 1; j <= 8; j++) // 因为用的一维数组,不需要两重循环,一重即可
72             {
73                 answer += num[j][ans[i][j]]; // ans[i][j]即为列号
74             }
75             maxn = max(maxn,answer);   // 取其最大
76         }
77         printf("%5d\n",maxn);  // 输出,注意输出格式
78     }
79     return 0;
80 }
View Code

 

小结:

没啥可结的了

 

你可能感兴趣的:(UVA 167 The Sultan's Successors (八皇后问题、回溯))