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. Intellij IDEA 使用Spring-boot-devTools 没有生效 解决方法
  2. winphone系统a、input标签被点击时产生的半透明灰色背景
  3. Python邮件发送案例
  4. DOM(二)——XML DOM
  5. [设计模式]代理模式
  6. 第二篇:Spring Boot 热部署
  7. 类似 Observer Pattern 的 NSNotificationCenter (实例)
  8. 语言认知偏差_认知语言学可以教给开发人员什么
  9. 铁路从未授权任何第三方平台售票服务,各大旅游网站哪来的接口?
  10. 了解on的依赖的调查作业的最终目的,前期尽可能的要出更多的工数
  11. 如何用iframe代码显示调用网页的指定部分
  12. matlab 系数矩阵存储,用Matlab对矩阵进行LU分解法 值得收藏
  13. 有哪些电影一定要趁年轻看? - 风之影的回答 - 知乎
  14. 存储系统概述——SRAM静态随机存储器
  15. 解决socket:(10106)无法加载或初始化请求的服务提供程序
  16. Vue高仿网易云音乐APP (vue.js 移动端)
  17. 敏捷式Mybatis
  18. 国产CPU的6大品牌,3大路线对比
  19. 让数据站住脚-浅谈用户研究中的信度与效度
  20. 电路与模拟电子技术----正弦交流电路(上)

热门文章

  1. 16G内存手机的福音,微信小程序也能建共享相册啦
  2. maven java版本过低_maven项目编译运行时提示jdk版本过低问题解决方法
  3. Android UI开发——Material Design界面设计【详细】
  4. 一辆适合长途出行的电动跑车 奥迪RS e-tron GT正式上市
  5. 麻省理工学院计算机科学,麻省理工学院素以世界顶尖的工程学和计算机科学而享誉世界[1-2] ,位列2015-16年世...
  6. 中国微商概念及产业链分析
  7. 外卖店优先级(模拟)
  8. 善的讯息可以产生美丽的水结晶
  9. 第二十四天:2019年第九届MathorCup高校数学建模挑战赛A题
  10. 从iTunes获取App信息