UVA The Sultan's Successors (八皇后问题)

 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

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


       题意:给出一个8*8的矩阵,每个位置都对应着一个1到99的数,在满足的八皇后规则的前提下找到最后的总和最大的那一个,输出最大的值maxx
       八皇后的规则就是在一个8*8的棋盘中放八个棋子,并且这些棋子满足两两不在同一行,同一列,同一对角线上。


代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int map[9][9];
int v[9];
int maxx;
int q[9];

int findx(int cnt,int ii)
{
    for(int i=0;i<cnt;i++)
    {
        if(q[i] == ii|| cnt-i==q[i]-ii || cnt-i== ii-q[i])
        {
            return 0;
        }
    }
    return 1;
}

void DFS(int cnt,int sum)
{
    if(cnt == 8)
    {
        if(maxx < sum)
        {
            maxx = sum;
        }
        return ;
    }
    for(int i=0;i<8;i++)
    {
        if(v[i] == 0 && findx(cnt,i) == 1)
        {
            v[i] = 1;
            q[cnt] = i;
            DFS(cnt+1,sum+map[cnt][i]);
            v[i] = 0;
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        maxx = 0;
        memset(v,0,sizeof(v));
        DFS(0,0);
        printf("%5d\n",maxx);
    }
    return 0;
}

UVA The Sultan's Successors (八皇后问题)

时间: 2024-12-21 02:56:44

UVA The Sultan's Successors (八皇后问题)的相关文章

UVA The Sultan&#39;s Successors

题目如下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecountry will be split into up to k separate parts on her death andeach part will be inherited by whoever performs best at some test. Itis possible for any

The Sultan&#39;s Successors UVA 167(八皇后问题)

说说: 其实这道题本质上就是一个八皇后问题.唯一的区别就是每个棋盘的格子都对应一个数字.最后要求输出,对应的解占据的格子的和的最大值.这只要在最后求出解的时候统计一下就可以了.下面就简单的说说八皇后问题,其实解法也不难.因为要求每行每列都要有棋子.因此只要确定每一行对应的棋子的列数就可以了.而对于每个棋子的所放的位置,同列上和对角线上不能有其他棋子,这个只要设一个访问数组保存一下就可以了.(注意要记得回溯).至于对角线的表示方法,例如所在位置为(x,y),那么一条对角线可以用x+y表示,另一条对

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

这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include <stdlib.h> int vis[100][100];//刚開始wrong的原因就是这里数组开小了,开了[8][8],以为可以.后来看到[cur-i+8]意识到可能数组开小了.改大之后AC. int a[8][8]; int C[10]; int max_,tot; void search_(int

Uva 167 The Sultan&#39;s Successors

题目链接:Uva 167 思路:     八皇后问题,采用回溯法解决问题. 代码: #include <iostream> #include <string.h> using namespace std; const int MAX_N = 10; int A[MAX_N]; int M[MAX_N][MAX_N]; int num, Max = 0; int is_safe( int row, int col ) { for ( int i = 0; i < row; ++

The Sultan&#39;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 n

Don&#39;t Get Rooked UVA 639(八皇后问题变形)

说说: 这道题目类似于八皇后问题.有n*n的正方形棋盘,且n<=4.例如在n=4时,有下图所示的棋盘,其中每两个棋子不能放在同一行或者同一列,除非有围墙(黑色的格子)将它们隔开.求给定的棋盘,能放下的最多的棋子数目. 分析: 在八皇后问题中,我们对整个棋盘分成八行考虑的,每行插入一个棋子.所以对于这道题目解决方案也类似,同样是一行一行插入.但是与八皇后问题不同的是,本题中棋盘一行可能插入多个棋子,也可能没有棋子.所以在递归函数中,不仅要给出所要处理的行的信息,也要给出所要处理的列的信息,其实就是

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

python解决八皇后问题

经典回溯算法:八皇后问题 算法要求: 在国际象棋棋盘上(8*8)放置八个皇后,使得任意两个皇后之间不能在同一行,同一列,也不能位于同于对角线上. 国际象棋的棋盘如下图所示: 问共有多少种不同的方法,并且指出各种不同的放法. # -*- coding:utf-8 -*- __author__ = "tyomcat" print("******八皇后问题的解决方法******") def next_col(current, n=8): length = len(curr

用遗传算法解八皇后问题

此算法收敛速度还可以,基本在1万代之内就能找到解 主程序 clear; clc; %% %八皇后问题,8X8的棋盘上,放置8个皇后,使之两两都不能攻击 %初始的状态,随机在棋盘上放置8个皇后,每列放一个 n = 8; %8皇后 %% %用遗传算法计算 %先随机获得几个个体,形成一个种群 %这个种群有10个个体 No_of_people = 10; people = randi(n,[No_of_people,n]); %计算每个初始种群的h值 people_h = ones(No_of_peop