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

11  2  3  4  5  6  7  89 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
注意:这个题主要要考虑到列,对角线的问题,对角线分为主对角线和斜对角线,left为主对角线,right为斜对角线, 分别求法不同但是要注意区分,找规律可得到,设一个坐标为(r , c),那么下一个斜对角线(left)为(r + d,c  - d)可得到 r + c = r1 + c1.。同理右对角线也满足(r , c) (r + d , c + d)满足c' - r' = c - r + 7( + 7 是为了保证c - r为正整数)

AC代码

#include <cstdio>
int chess[8][8],max;
bool col[8],left[15],right[15];
void dfs(int row,int sum)
{if(row == 8){max = max < sum ? sum:max;return;}for(int i = 0;i < 8;i++){if(!col[i] && !left[i - row + 7] && !right[i + row]){col[i] = left[i - row + 7] = right[i + row ] = true;//列进行标记dfs(row + 1,sum + chess[row][i]);col[i] = left[i - row + 7] = right[i + row ] = false;//退回之后要取消标记}}
}
int main()
{int k;scanf("%d",&k);while(k--){for(int i = 0;i < 8;i++)for(int j = 0;j < 8;j++)scanf("%d",&chess[i][j]);max = -1;dfs(0,0);printf("%5d\n",max);//注意题中的格式}return 0;
}

The Sultan's Successors (八皇后)相关推荐

  1. The Sultan's Successors (八皇后)DFS

    The Sultan's Successors 原题链接https://vjudge.net/contest/345248#problem/F 八皇后问题,既在8*8的棋盘中放置8个皇后,每行,每列, ...

  2. 38行代码AC——UVA-167The Sultan‘s Successors(八皇后问题,附视频讲解)

    最近备考蓝桥,学习到递归模块,从最基本的八皇后及其变种开始刷起(如果可以穿越,我一定要抓到发明递归的那个人,然后把他干掉,造福后世的算法er,). 题目大意 一个人,没孩子,要在死前分割财产,然后出了 ...

  3. UVA167【The Sultan‘s Successors】(递归与回溯、8皇后问题)

    链接:UVA167[The Sultan's Successors] 题目描述: The Sultan of Nubia has no children, so she has decided tha ...

  4. UVA The Sultan's Successors

    题目如下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecount ...

  5. uva 167 The Sultan's Successors

    题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108& ...

  6. 167 The Sultan's Successors

    原题: The Sultan of Nubia has no children, so she has decided that the country will be split into up t ...

  7. 苏丹的继承者(八皇后问题)之python解

            八皇后问题 早在1848年就已提出,可谓是历史悠久,经久不衰,曾一度难倒了高斯之流的顶级数学大师,但是,在计算机发明之后,这道曾经的世界难题被以无数种方式轻松解决,这也不由得令我们广大 ...

  8. Python:爬山法/随机重启爬山法/允许侧移的爬山法解决八皇后问题

    文章目录 1 八皇后问题 2 程序代码 2.1 程序1 2.2 程序2 2.3 程序3 2.3.1 爬山法 2.3.2 随机重启爬山法 2.3.3 允许皇后侧移的爬山法 3 评价 1 八皇后问题 有一 ...

  9. C语言局部搜索算法(爬山法,模拟退火法,遗传算法)求解八皇后问题

    C语言局部算法求解八皇后问题 写在前面 八皇后问题及局部搜索算法 爬山法(hill-climbing searching) 算法介绍 代码实现 退火法(simulated annealing) 算法介 ...

最新文章

  1. 怎么进u8系统服务器网址,服务器地址变更后如何进入u8
  2. python操作微信小程序云端数据库_微信小程序云开发之数据库操作
  3. servlet和action的区别
  4. Android系统移植与驱动开发概述
  5. mvc模式缺点 php,mvc模式有哪些优缺点
  6. java poi 打开 保存_Java-Apache POI-在DB中读取和存储RTF内容
  7. 利用奇异值产生脆弱水印应用于检测、定位、恢复文章总结
  8. 用集合return多个值_十五个常用的 Laravel 集合(Collection)
  9. 绑定事件和解绑事件的方法
  10. TeamViewer 远程连接一直显示正在初始化显示参数的解决方案
  11. python for ArcGIS 绘制重庆市板块地图
  12. 2022年热传递与热物理国际研讨会(HTT 2022)
  13. Oracle导入dmp文件步骤
  14. 基于ffmpeg的音频转换器-rtp承载的amr转换为pcm音频流
  15. 蚁群算法C语言最短路径规划,蚁群算法规划路径
  16. ant-vue中的a-icon使用方法
  17. windows-phone-power-tools [wp 安装部署xap]
  18. 【ct107d】开发版说明书
  19. 数据索引---Solr DIH
  20. 云服务器是不是虚拟主机,云服务器不是虚拟主机吗

热门文章

  1. 《哈利·波特:霍格沃茨之谜》推出二月支线任务“天界舞会”,带来新的选择、挑战与服装
  2. corpus iweb_「As is depicted」和「As depicted」哪个对?
  3. k-最近邻聚类k-Nearest Neighbor
  4. 最近ChatGPT封号太严重了,这里是解封攻略步骤(建议收藏)
  5. [蓝桥杯][2019年第十届真题]-----外卖店优先级
  6. itunes store服务中断_终于明白为什么那么多人吐槽apple的态度了.临时工一样的中国区itunes store客服......
  7. 榆熙电商:拼多多商家一年能进行几次申诉?
  8. java 求两个list 集合的交集,重复的元素
  9. 5款小巧有趣的微信小程序,个个让你心花怒放!
  10. 模拟QQ自动隐藏窗体