//很久以前做的了,当时WA,一直没找到错误...然后今天看了下。。。发现就是每次开始新一轮年的时候没哟初始化.......

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 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<cstdio>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8, sum=0, msum=0;
int a[10][10] = { 0 }, b[10][10];
int t = 1;
void print()
{printf("No. %d\n", t++);for (int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){printf("%d ", a[j][i]);}printf("\n");}
}
bool judge(int x, int y)
{for(int i=0; i<x; i++){//竖直方向||左上方 || 右上方if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))return false;}return true;
}
/*
void dfs(int num)
{if (num >= 8){print();}for (int i = 0; i < 8; i++){if (a[num][i]!=1&&judge(num, i)){a[num][i] = 1;dfs(num + 1);a[num][i] = 0;}}
}
*/int dfs(int num)
{if (num >= 8){//      print();if(msum < sum)msum = sum;}for (int i = 0; i < 8; i++){if (a[num][i]!=1&&judge(num, i)){a[num][i] = 1;sum += b[num][i];if(dfs(num + 1)){  return 1;}a[num][i] = 0;sum -= b[num][i];}}return 0;
}
int main()
{int n;cin >> n;for(int k = 0; k < n; k++) {for (int i = 0; i < 8; i++)for (int j = 0; j < 8; j++)cin >> b[i][j];dfs(0);cout << fixed << setw(5) << msum << endl;}return 0;
}

The Sultan's Successors UVA - 167相关推荐

  1. uva 167 The Sultan's Successors

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

  2. Uva 167 The Sultan's Successors(dfs)

    题目链接:Uva 167 思路分析:八皇后问题,采用回溯法解决问题. 代码如下: #include <iostream> #include <string.h> using n ...

  3. UVA The Sultan's Successors

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

  4. UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索

    UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...

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

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

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

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

  7. The Sultan's Successors (八皇后)

    The Sultan's Successors(八皇后问题) The Sultan of Nubia has no children, so she has decided that the coun ...

  8. UVA 167(The Sultan's Successors)八皇后回溯

    题意 输入8x8的棋盘 在棋盘上放置8个皇后 输出8个皇后位置对应数字之和的最大值 回溯,一年前做的题目,竟然又碰到了,之前没学过dfs,对着题解思考了很长时间... 粗心的我,写错了一个变量名,查了 ...

  9. UVA 167 - The Sultan‘s Successors

    题目大意:八皇后问题,每个格子有分数,皇后放置在格子上则得到分数,问怎么放置八皇后总分数最高. 解题思路:回溯,小紫里讲的挺清楚的,只要在达成条件时,将八个位置的分数加起来最后得到最高分输出就可以了. ...

最新文章

  1. Pyinstaller 打包exe 报错 “failed to execute script XXX“的一种解决方案
  2. dsst依赖opencv
  3. 使用VScode开发C语言程序,环境安装配置,保姆级教程
  4. Git操作之配置别名
  5. python怎么做乘法表_python怎么写乘法表
  6. element走马灯自动_Element Carousel 走马灯的具体实现
  7. ios html gif 显示,iOS中gif图的显示方法示例
  8. luogu1024 一元三次方程求解
  9. Android Studio如何导出可供Unity使用的aar插件详解
  10. Super Jumping! Jumping! Jumping! HDU - 1087
  11. NAVICAT MYSQL 建表字段 默认值、EMPTY STRING、空白、NULL 的区别
  12. 符号实体(转义字符)
  13. git安装和GitHub使用
  14. grpc+protobuf 的C++ service 实例解析
  15. WIN32:参考文章
  16. PUN搭建大厅与网络同步
  17. 电脑重装系统苹果笔记本开不了机的解决措施有哪些
  18. 安装torch-geometric
  19. HI,Bing,近期数字安全领域有什么热点事件?
  20. python 拷贝文件创建目录失败_解决python os.mkdir创建目录失败的问题

热门文章

  1. Oracle 考试题 原题
  2. 家庭居室装修需做哪些项目
  3. 莫队算法(普通莫队、带修莫队、树上莫队、不删除莫队)学习笔记【理解+套路/核心代码+例题及题解】
  4. MAC系统下安装homebrew(2020.04)
  5. 一台电脑怎么安装32位和64位的jdk,怎么配置环境变量
  6. Unity Json 编写及读取
  7. Vue.js快速入门之五:Mockjs的使用和语法详解
  8. vi编辑器怎么不保存退出?
  9. 一些实用的镜像网站推荐
  10. 生活记录:突然“醒来”