链接: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. 如何在Github上精准地找到想要的开源项目?
  2. 一次幸运的集群操作与修复经历
  3. php 加权计算公式,PHP计算加权平均数的实例分享
  4. mysql实现心跳表_第29问:MySQL 的复制心跳说它不想跳了
  5. Linux 服务器配置信息查询方法,国产化申威服务器配置信息查看演示
  6. ES Next Arrow function Promise Iterator Generator yield Async Await
  7. Mozilla发布最大公共语音数据集Common Voice
  8. 如何识别交换机的性能优劣?
  9. python 编码 解码 读写文件
  10. Direct 3D学习笔记(三)——光照与材质
  11. 有安全研究者混入了 PHP 8.0 开发组!
  12. 第四届CCF计算机职业资格认证考试题解(C++)
  13. 手机摄影:黄埔军校旧址(下)
  14. JMeter插件之PerfMon监控服务器性能
  15. win10 计算机管理器没有ime,Win10电脑右下角提示“已禁用IME”的6种解决方法
  16. Python爬虫 | 爬取高质量小姐姐照片
  17. orcal数据库11g安装时出现物理内存不足的情况解决办法
  18. 1.3 nest.js使用supertest单元测试及e2e测试
  19. Hive恢复误删数据表
  20. neat算法做监督学习(Python)

热门文章

  1. 企业数据治理之主数据管理
  2. expected an indented block报错的原因
  3. 阿里云免费个人Docker镜像仓库搭建
  4. apache iotdb_清华数为工业互联网时序数据库Apache IoTDB亮相2019工业互联网峰会
  5. 如何刷鸿蒙OS,普通安卓手机如何刷鸿蒙系统?
  6. pwnable-shellshock
  7. html代码 imgn,html代码大全
  8. Kodu程序的菜单---Kodu少儿编程第七天
  9. SpringCloud 09 - Gateway 网关
  10. 如何访问集群中指定的服务器,【Nacos源码之配置管理 六】集群模式下服务器之间是如何互相感知的...