The Sultan's Successors UVA - 167
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 <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int c[],vis[][],tot,n=,sum_max;
int mapn[][];
void search(int cur)
{
if(cur==n)//递归边界,只要走到了这里,所有皇后必然不冲突
{
if(sum_max<tot)
sum_max = tot;
}
else for(int i=;i<n;i++)
{
if(!vis[][i]&&!vis[][cur+i]&&!vis[][cur-i+n])//利用二维数组直接判断
{//0为竖行,1为副对角线,2为主对角线
c[cur] = i;//保存下每行皇后的位置
tot += mapn[cur][i];
vis[][i] = vis[][cur+i] = vis[][cur-i+n] = ;
search(cur+);
vis[][i] = vis[][cur+i] = vis[][cur-i+n] = ;//记得改回来
tot -= mapn[cur][i];
}
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
sum_max = ,tot = ;
memset(vis,,sizeof(vis));
for(int i=;i<;i++)
for(int j=;j<;j++)
{
cin >> mapn[i][j];
}
search();
printf("%5d\n",sum_max);
}
return ;
}
The Sultan's Successors UVA - 167的更多相关文章
- Uva 167 The Sultan's Successors(dfs)
题目链接:Uva 167 思路分析:八皇后问题,采用回溯法解决问题. 代码如下: #include <iostream> #include <string.h> using n ...
- uva167 The Sultan's Successors
The Sultan's Successors Description The Sultan of Nubia has no children, so she has decided that the ...
- UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...
- UVA 167 R-The Sultan's Successors
https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...
- uva 167 - The Sultan's Successors(典型的八皇后问题)
这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...
- uva167 - The Sultan's Successors
题意:八皇后问题的扩展.8*8棋盘上每个格子都有一个整数,要求8个皇后所在格子的数字之后最大 解法一,回溯: 用vis数组记录 列,主对角(y-x), 副对角(y+x) 访问情况 #include ...
- UVA The Sultan's Successors
题目例如以下: The Sultan's Successors The Sultan of Nubia has no children, so she has decided that thecou ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 备战NOIP每周写题记录(一)···不间断更新
※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...
随机推荐
- Android 虹软人脸识别SDK-人脸对比
准备 : 登录官方网站,获取SDK,进行个人验证后新建项目,获取APP_ID,和SDK_KEY: https://ai.arcsoft.com.cn/ucenter/resource/build/in ...
- 万万没想到,JVM内存结构的面试题可以问的这么难?
在我的博客中,之前有很多文章介绍过JVM内存结构,相信很多看多我文章的朋友对这部分知识都有一定的了解了. 那么,请大家尝试着回答一下以下问题: 1.JVM管理的内存结构是怎样的? 2.不同的虚拟机在实 ...
- iview中page组件的跳转功能BUG解决方案
xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!! 在 ...
- 解读 PHP 的 P++提案
解读 PHP 的 P++提案 周末看到一篇文章说 PHP 创始人提议将 PHP 拉出新分支,创建 P++ 语言.随后阅读了一下 Zeev Suraski 发起的这个邮件列表,大致了解了一下,这里做个解 ...
- 2019最新最全Java开发面试常见问题答案总结
2019最新最全Java开发面试常见问题答案总结 马上准备9月份出去面试Java开发,自己学习丢西瓜捡芝麻,学了的都忘了,所以有机会自己做个学习笔记,摘录自各个博文以及总结. 1.JAVA面向对象的特 ...
- 鲜为人知的maven标签解说
目录 localRepository interactiveMode offline pluginGroups proxies servers mirrors profiles 使用场景 出现位置 激 ...
- Redis简单梳理及集群配置
**REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵 ...
- python调用支付宝支付接口详细示例—附带Django demo代码
项目演示: 一.输入金额 二.跳转到支付宝付款 三.支付成功 四.跳转回自己网站 在使用支付宝接口的前期准备: 1.支付宝公钥 2.应用公钥 3.应用私钥 4.APPID 5.Django 1.11. ...
- Jenkins将ASP.NETCore部署到Azure
首先需要获得Azure上App-service 的porfile. 登录portal 选到app,点击Get publish pofile 将得到一个 ****.PublishSettings,注意这 ...
- spring-boot-plus V1.2.1 发布 文件上传下载和静态资源访问
[V1.2.1-RELEASE] 2019.08.21 ⭐️ New Features 文件上传保存到服务器指定目录 文件下载 访问上传的图片等资源 启用项目静态资源访问,可访问static/temp ...