2018_1_23_uva167_回溯打表_
2018-01-23 14:59
288 查看
hdu1642
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.
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
Sample Output
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.
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<iostream> #include<string> #include<vector> #include<algorithm> #include<cstring> using namespace std; int P[1000][9]; int tmp[8]; int n=0; bool col[8]={0},lleft[15]={0},rright[15]={0}; void func(int r){ if(r==8){ for(int i=0;i<8;i++) P [i]=tmp[i]; n++; return; } for(int c=0;c<8;c++){ int ld=(c-r)+7; int rd=c+r; if(!col[c]&&!lleft[ld]&&!rright[rd]){ col[c]=1,lleft[ld]=1,rright[rd]=1; tmp[r]=c; func(r+1); col[c]=0,lleft[ld]=0,rright[rd]=0; } } } int main(){ func(0); int Case; int board[8][8]; scanf("%d",&Case); while(Case--){ for(int i=0;i<8;i++) for(int j=0;j<8;j++) scanf("%d",&board[i][j]); int ans=0; for(int i=0;i<n;i++){ int sum=0; for(int j=0;j<8;j++) sum+=board[j][P[i][j]]; if(sum>ans)swap(ans,sum); } printf("%5d\n",ans); } return 0; }
相关文章推荐
- UVa 167 - The Sultan's Successors 递归回溯
- [回溯]23 out of 5 UVA 10344
- UVA 10344 - 23 out of 5 全排列,3进制表示状态,回溯
- 2018_1_23_poj_2258_回溯
- uva 167 The Sultan's Successors(回溯)
- UVa 10344 23 out of 5 (全排列枚举&回溯)
- UVA 23 Out of 5(DFS+回溯)
- UVA - 167 The Sultan's Successors(回溯 八皇后问题改编)
- UVa 10344 - 23 out of 5 全排列递归回溯
- uva-167 - The Sultan's Successors-八皇后-回溯
- UVa 10344 - 23 out of 5 递归回溯
- UVA - 10344 23 out of 5(回溯+全排列)
- 23 Out of 5 uva+回溯
- UVA 10344 - 23 Out of 5(回溯)
- UVa 208 Firetruck【回溯】
- UVA 10820 Send a Table (欧拉函数打表)
- [回溯]Stamps UVA165
- uva167 - The Sultan's Successors
- UVa 10006 Carmichael Numbers 【快速幂+素数打表(埃氏筛法)】
- UVA 10706 Number Sequence (找规律 + 打表 + 查找)