The Sultan's Successors

Description

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

Accepted 0ms(评测来源:Virtual Judge)

编写编译环境:ubuntu(linux),emacs

题目大意:苏丹努比亚(似乎在哪里听过,一千零一夜?)年事已高,向划分疆域给继承者。选择继承者需Ta足够机智。于是,她用经典的八皇后问题,外加棋盘上多了系数,每取一个皇后累加皇后坐标上的数字,求最大数字。

思路:完全行行枚举,暴力判断。。。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int boa[8][8],qnn[8][2],ans;
bool col[8];
void Init()
{
ans=0;
memset(col,0,sizeof(col));
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
scanf("%d",&boa[i][j]);
}
bool jud(int x,int y)
{
for(int i=0;i<x;i++)
if((qnn[i][0]==qnn[i][1]&&x==y)||(x+y==qnn[i][0]+qnn[i][1])||(x-y==qnn[i][0]-qnn[i][1]))
return false;
return true;
}
void dfs(int cur,int sum)
{
if(cur==8){
ans=max(ans,sum);
return;
}
for(int j=0;j<8;j++)
if((!col[j])&&jud(cur,j)){
col[j]=1;
qnn[cur][0]=cur;
qnn[cur][1]=j;
dfs(cur+1,sum+boa[cur][j]);//cur means columns,every column has a queen;
col[j]=0;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
Init();
dfs(0,0);
printf("%5d\n",ans);
}
return 0;
}

uva167 The Sultan's Successors的更多相关文章

  1. uva167 - The Sultan's Successors

      题意:八皇后问题的扩展.8*8棋盘上每个格子都有一个整数,要求8个皇后所在格子的数字之后最大 解法一,回溯: 用vis数组记录 列,主对角(y-x), 副对角(y+x) 访问情况 #include ...

  2. 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 ...

  3. 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 ...

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

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

  5. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  6. UVA The Sultan&#39;s Successors

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

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. uva 167 - The Sultan&#39;s Successors(典型的八皇后问题)

    这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...

  9. UVA - 11604 General Sultan 题解

    题目大意: 有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串. 思路: 神奇的构图,暴力的求解. 可以发现,若有不同的拼法,则一 ...

随机推荐

  1. Android自定义ScrollView分段加载大文本数据到TextView

    以下内容为原创,转载时请注明链接地址:http://www.cnblogs.com/tiantianbyconan/p/3311658.html 这是我现在碰到的一个问题,如果需要在TextView中 ...

  2. 项目管理、测试管理、代码bug 管理

    1.友盟统计     阿里旗下的产品 http://www.umeng.com/ 2.bugly    腾讯旗下的产品 http://bugly.qq.com/ 3.禅道  项目管理工具  需要部署到 ...

  3. commandline (命令行)登录mysql

    mysql登录在命令行登录的时候是通过mysql的里面的程序的录的 其登录格式有两种:(在oracle上看到的是列出这两种,不要既有全参数名又有缩写参数名.) 1.全参数名登录释例 mysql --h ...

  4. MVVM 模式下iOS项目目录结构详细说明

    ➠更多技术干货请戳:听云博客 我们在做项目的时候,会经常用到各种设计模式,最常见的要数 MVC (模型,视图,控制器)了.但是,今天我们要说的是另一种设计模式——MVVM. 所以 MVVM 到底是什么 ...

  5. TCP/IP 协议难点之一—— IP分片

    1 IP协议简单介绍 就个人而言,网络中,抛开网络安全加密这些,就只单单讨论协议本身,比较难的有三个地方: IP分片与重组 TCP滑动窗口与TCP状态的改变 TCP定时器 其实协议本身根据<TC ...

  6. 浅谈JSON数据解析方法

    JSON数据解析 JSON是什么?? 如何把JSON数据解析出来 如何把一个字典转换为JSON JSON详细介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交 ...

  7. 深入理解JPEG图像格式Jphide隐写

    0x00 隐写原理 Jphide是基于最低有效位LSB的JPEG格式图像隐写算法,使用JPEG图像作为载体是因为相比其他图像格式更不容易发现隐藏信息,因为JPEG图像在DCT变换域上进行隐藏比空间域隐 ...

  8. 浅谈JS中的继承

    前言 JS 是没有继承的,不过可以曲线救国,利用构造函数.原型等方法实现继承的功能. var o=new Object(); 其实用构造函数实例化一个对象,就是继承,这里可以使用Object中的所有属 ...

  9. Vector和Stack(已过时,不建议使用)

    以下内容基于jdk1.7.0_79源码: 什么是Vector和Stack Vector:线程安全的动态数组 Stack:继承Vector,基于动态数组实现的一个线程安全的栈: Vector和Stack ...

  10. *MyBatis框架 在控制台打印sql语句

    在 log4j.properties  中将这段代码添加进去就好了#log4j.rootLogger=INFO, Console#Consolelog4j.appender.Console=org.a ...