链接:UVA167【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 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 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.

输入样例:

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

输出样例:

260

题意:
在8*8的棋盘上每个格子有自己的价值
在棋盘上放8个互相攻击不到的皇后
求8个位置价值之和的最大值

数据范围:
询问次数不超过20,每格价值不超过100

思路:
8皇后问题的应用
用一个二维数组来存储每种情况皇后的摆放位置
P[i][j]表示方式 i 中第 j 行皇后的列位置
用递归来完成p数组

代码:

#include <cstdio>
using namespace std;
int P[1000][9]; //方式 i 中第 j 行皇后的列位置为 P[i][j]
int tmp[8]; //当前方式中第 i 行皇后的列位置为 tmp[i]
int n = 0; //方式数初始化
bool col[8] = {0}, left[15] = {0}, right[15] = {0}; //所有列和左右对角线未被选中void fun(int r)
{if(r == 8){for(int i = 0;i < 8;i++)P[n][i] = tmp[i];n++;return;}for(int c = 0;c < 8;c++){int ld = c+r;int rd = (c-r)+7;if(!col[c]&&!left[ld]&&!right[rd]){col[c] = left[ld] = right[rd] = 1;tmp[r] = c;fun(r+1);col[c] = left[ld] = right[rd] = 0;}}
}int main()
{fun(0);int Case;scanf("%d",&Case);int board[8][8]; while(Case--){int ans = 0;for(int i = 0;i < 8;i++)for(int j = 0;j < 8;j++)scanf("%d",&board[i][j]);        for(int i = 0;i < n;i++){ int sum = 0;for(int j = 0;j < 8;j++)sum += board[j][P[i][j]];if(ans < sum) ans = sum; }printf("%5d\n",ans);          }return 0;
}

UVA167【The Sultan‘s Successors】(递归与回溯、8皇后问题)相关推荐

  1. LeetCode算法题9:递归和回溯-N皇后问题

    文章目录 N 皇后 初始算法 : 修改后的算法 优化后的算法: 总结 N 皇后 题目链接:https://leetcode-cn.com/problems/n-queens/ 题目描述:n 皇后问题 ...

  2. LeetCode算法题14:递归和回溯2

    文章目录 前言 一.全排列II 仿照全排列(n 叉树) 剪枝(去掉重复的结果) 二.组合总和 一.初始解法(n 叉树): 1,采用 Set 去重 2,在递归搜索的时候去重(推荐解法) 初始解法的遍历过 ...

  3. 递归--基于回溯和递归的八皇后问题解法

    八皇后问题是在8*8的棋盘上放置8枚皇后,使得棋盘中每个纵向.横向.左上至右下斜向.右上至左下斜向均只有一枚皇后.八皇后的一个可行解如图所示:                             ...

  4. Pythonic:递归、回溯等5种方法生成不重复数字整数

    问题描述:从0到9这10个数字任选3个不重复的数字,能构成哪些三位数? So easy!看到这样的问题,很多人会写出类似(注意,只是类似,我为了使得本文几个函数具有相同的调用形式,给demo1和dem ...

  5. 20210325:力扣递归,回溯类型题目合集

    力扣递归,回溯类型题目合集 题目 思路与算法 代码实现 写在最后 题目 子集 2. 90. 子集 II 3. 40. 组合总和 II 4. 22. 括号生成 思路与算法 子集:注释的很详细,递归生成子 ...

  6. 【算法】递归|迷宫回溯问题|八皇后问题

    [算法]递归|迷宫回溯问题|八皇后问题   迷宫回溯问题,要用动态的眼光来看待这个递归算法. package com.serein.recursion;/*** @author baichuan* @ ...

  7. 数独算法-递归与回溯

    1.概述 数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复. 1)终盘 ...

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

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

  9. UVA The Sultan's Successors

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

最新文章

  1. VC++ .NET 2003学习
  2. Windows PE第九章 线程局部存储
  3. eclipse控制台自动换行不分割单词_这 7 个实用的文档技巧,不掌握就太可惜了...
  4. System.ArgumentException: 输入数组长度大于此表中的列数。
  5. CG-CTF-Web-SQL注入1
  6. WPF轮播图实现方式
  7. 前端学习(2568):使用高级特性provide和inject
  8. labview my_fpga开发套件下载_LabVIEW面向对象的ActorFramework(1)
  9. 三十分钟包会——正则表达式
  10. nginx 日志格式设置 和 负载均衡下 获取真实ip
  11. centos 减少tty数量的方法
  12. python 复制列表内容_python 复制列表的六种方法
  13. 高等数学(第七版)同济大学 习题4-3 个人解答
  14. Unix网络编程开篇
  15. 小白学习一eNSP华为模拟器(5)利用三层交换机实现VLAN间路由
  16. 微信小程序 轮播图展示,图片全屏显示
  17. 高通平台文档下载【学习笔记】
  18. JS实现四舍五入保留两位小数
  19. java输出到空心三角形_java经典算法_019打印三角形(空心,实心) | 学步园
  20. top介绍RES与VIRT区别

热门文章

  1. Scratch软件编程等级考试四级——20191221
  2. 蓝牙耳机单次续航排名,续航最久的蓝牙耳机推荐
  3. 什么是等级保护?等保二级和三级的区别?
  4. 数学建模案例--基于微分方程的酒后驾车问题浅析
  5. 我们为什么选择计算机专业?为什么学习编程?
  6. linux无法识别m2固态,主板识别不出m.2固态硬盘怎么办|笔记本电脑无法识别m.2固态硬盘解决方法-系统城...
  7. 从平头哥讲起,谈谈全域旅游,说说为什么要做全栈工程师
  8. GPS坐标系转换(标准坐标系WGS84转GCJ-02火星坐标系)
  9. 抽象工厂和工厂方法模式
  10. DE2-115 SDRAM地址问题