UVA 167 The Sultan's Successors
2015-02-17 15:22
561 查看
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 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 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.
260
八皇后问题的变种,怎样放得到的分数最高。
学会怎样简化代码也是很重要的。
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 than20 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
1 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
Sample output
260八皇后问题的变种,怎样放得到的分数最高。
#include<cstdio> #include<cstring> #include<iostream> #include<string> #include<algorithm> #include<math.h> using namespace std; const int maxn = 8; int t, sum; int f[maxn], a[maxn][maxn]; bool check() { for (int i = 0; i < maxn; i++) for (int j = i + 1; j < maxn; j++) if (j - i == f[j] - f[i] || j - i == f[i] - f[j]) return false; return true; } int main() { cin >> t; while (t--) { sum = 0; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) cin >> a[i][j]; f[i] = i; } do{ if (check()) { int tot = 0; for (int i = 0; i < maxn; i++) tot += a[i][f[i]]; sum = max(sum, tot); } } while (next_permutation(f, f + maxn)); printf("%5d\n", sum); } return 0; }
学会怎样简化代码也是很重要的。
相关文章推荐
- uva 167 - The Sultan's Successors(典型的八皇后问题)
- Uva-167-The Sultan's Successors
- UVa 167 - The Sultan's Successors 递归回溯
- UVa 167 - The Sultan's Successors, 八皇后问题
- uva 167 - The Sultan's Successors
- UVa 167 The Sultan's Successors (八皇后问题)
- UVa 167 - The Sultan's Successors
- uva-167 - The Sultan's Successors-八皇后-回溯
- uva 167 - The Sultan's Successors
- uva167 -The Sultan's Successors(类八皇后)
- UVA_167_The Sultan's Successors
- uva 167 - The Sultan's Successors(典型的八皇后问题)
- uva 167 The Sultan's Successors
- UVa 167 - The Sultan's Successors
- UVA - 167 The Sultan's Successors(回溯 八皇后问题改编)
- uva 167 The Sultan's Successors 搜索
- 【权值八皇后】uva 167——The Sultan's Successors
- UVA The Sultan's Successors
- UVA The Sultan's Successors
- 167 - The Sultan's Successors(八皇后问题(回溯))