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 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
时间复杂度:$O(k * 92)$
题解:dfs
代码:
#include <bits/stdc++.h>
using namespace std; int k, sum;
int mp[10][10];
int out[10]; void dfs(int cnt, int all) {
if(cnt == 9) {
if(all > sum)
sum = all;
return ;
}
for(int i = 1; i <= 8; i ++) {
bool flag = true;
out[cnt] = i;
for(int j = 1; j < cnt; j ++) {
if(out[cnt] == out[j] || cnt - out[cnt] == j - out[j] || cnt + out[cnt] == j + out[j]) {
flag = false;
break ;
}
}
if(flag)
dfs(cnt + 1, all + mp[cnt][i]);
}
} int main() {
scanf("%d", &k);
while(k --) {
sum = 0;
for(int i = 1; i <= 8; i ++) {
for(int j = 1; j <= 8; j ++)
scanf("%d", &mp[i][j]);
}
dfs(1, 0);
printf("%5d\n", sum);
}
return 0;
}
UVA 167 R-The Sultan's Successors的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- ACM-ICPC Dhaka Regional 2012 题解
B: Uva: 12582 - Wedding of Sultan 给定一个字符串(仅由大写字母构成)一个字母表示一个地点,经过这个点或离开这个点都输出这个地点的字母) 问: 每一个地点经过的次数(维 ...
随机推荐
- 网站用户行为分析——在Ubuntu下安装MySQL及其常用操作
安装MySQL 使用以下命令即可进行mysql安装,注意安装前先更新一下软件源以获得最新版本: sudo apt-get update #更新软件源 sudo apt-get install mysq ...
- 【NXP开发板应用—智能插排】3.驱动GPIO点亮外接LED
[前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...
- Python(9-18天总结)
day9:函数:def (形参): 函数体 函数名(实参)形参:在函数声明位置的变量 1. 位置参数 2. 默认值参数 3. 混合 位置, 默认值 4. 动态传参, *args 动态接收位置参数, * ...
- c. 求阶乘和的方法(N的值不能太大)初学者
#include <stdio.h> int main() { int n,i; int a=1; //a设置为一个数的阶乘 int b; // b 设置为阶乘的和 for(i ...
- Active MQ C++实现通讯记录
Active MQ C++实现通讯 背景知识: ActiveMQ是一个易于使用的消息中间件. 消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息中间件(MOM:Message O ...
- sublime_text3常用操作与快捷键
1.编辑多列 按鼠标滚轮进行多列选中 键盘ctrl+alt+↓进行多行选中操作 2.快捷键 ctrl+H:替换(F为经典的搜索) ctrl+G:跳到指定行 ctrl+D:选词,连续按选中下面匹配的词, ...
- 【Keras案例学习】 CNN做手写字符分类(mnist_cnn )
from __future__ import print_function import numpy as np np.random.seed(1337) from keras.datasets im ...
- jmeter开发自己的sampler插件
1. 新建maven工程 2.pom文件引入jmeter的核心包 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...
- JavaWeb——JavaWeb开发入门
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- generator-ivweb 基于react-redux的多页脚手架
背景 每个公司甚至每个项目组,在开发新项目的时候都会有一些自己特色的东西,比如公共组件,ajax请求拦截处理,内部积累的一些业务逻辑等等,如果没有自己的脚手架,那么拷贝代码成为常态,每个项目的结构,甚 ...