[回溯]The Sultan's Successors UVA167
来源:互联网 发布:麦粒网络 编辑:程序博客网 时间:2024/05/29 15:21
The Sultan's Successors
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
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1617 18 19 20 21 22 23 2425 26 27 28 29 30 31 3233 34 35 36 37 38 39 4041 42 43 44 45 46 47 4848 50 51 52 53 54 55 5657 58 59 60 61 62 63 64
Sample output
260
一道典型的变异八皇后问题,每个节点带权值了。
#include<iostream>#include<cstring>#include<iomanip>using namespace std;int arry[10][10],vis[3][20];int maxnum;void dfs(int pos,int num){ int i,j; if(pos==8) { if(num>maxnum) { maxnum=num; } return; } else { for(i=0;i<8;i++) { if(!vis[0][i]&&!vis[1][i+pos]&&!vis[2][pos-i+8]) { vis[0][i]=vis[1][i+pos]=vis[2][pos-i+8]=1; dfs(pos+1,num+arry[pos][i]); vis[0][i]=vis[1][i+pos]=vis[2][pos-i+8]=0; } } }}int main(){ int n; cin>>n; while(n--) { int i,j; for(i=0;i<8;i++) { for(j=0;j<8;j++) { cin>>arry[i][j]; } } memset(vis,0,sizeof(vis)); maxnum=0; dfs(0,0); cout<<setw(5)<<maxnum<<endl; } return 0;}
- [回溯]The Sultan's Successors UVA167
- uva167 The Sultan's Successors
- uva167 The Sultan's Successors
- uva167 The Sultan's Successors
- uva167 -The Sultan's Successors(类八皇后)
- 167 - The Sultan's Successors 回溯
- The Sultan's Successors
- uva-167 - The Sultan's Successors-八皇后-回溯
- 167 - The Sultan's Successors 八皇后(回溯法)
- uva 167 The Sultan's Successors(回溯)
- UVa 167 - The Sultan's Successors 递归回溯
- 167 - The Sultan's Successors
- vua167 - The Sultan's Successors
- UVaOJ167 - The Sultan's Successors
- UVA The Sultan's Successors
- 167The Sultan's Successors
- UVa 167 - The Sultan's Successors
- Uva 167 The Sultan's Successors
- java中PreparedStatement和Statement的区别
- leetcode: Longest Palindromic Substring
- Java中的Random类
- c++中vector与list的区别
- 转载-Oracle 常用的dump命令
- [回溯]The Sultan's Successors UVA167
- 配置了centos6.4的yum的更新源
- 生产者消费者模型(二)-引入ArrayBlockingQueue
- 开始进行安卓开发
- 线性回归之梯度下降法(附代码)
- 菜鸟学PHP之Smarty入门(组图)
- Centos和Fedora如何安装最新版的Firefox
- C#中判断远端服务器可达
- oracle基础知识总结 part 2 : 其他语句,事务和常用数据对象