UVa 167 - The Sultan's Successors(dfs)

原创 2018年04月17日 12:53:09

The Sultan of Nubia has no children, so she has decided that the country will be split into up to kseparate parts on her death and each part will be inherited by whoever performs best at some test. Itis possible for any individual to inherit more than one or indeed all of the portions. To ensure thatonly highly intelligent people eventually become her successors, the Sultan has devised an ingenioustest. In a large hall filled with the splash of fountains and the delicate scent of incense have beenplaced k chessboards. Each chessboard has numbers in the range 1 to 99 written on each square and issupplied with 8 jewelled chess queens. The task facing each potential successor is to place the 8 queenson the chess board in such a way that no queen threatens another one, and so that the numbers onthe squares thus selected sum to a number at least as high as one already chosen by the Sultan. (Forthose unfamiliar with the rules of chess, this implies that each row and column of the board containsexactly 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 thehighest scores possible for each board under these conditions. (You know that the Sultan is both agood 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<bits/stdc++.h>
using namespace std;
int a[10][10];
int v1[100];
int v2[100];
int v3[100];
int ans,sum;
void dfs(int s,int sum)
{
    if(s==8)
    {
        ans=max(ans,sum);
        return;
    }
    for(int j=0;j<8;j++)
    {
        if(!v1[j]&&!v2[s+j]&&!v3[s-j+8])
        {
            v1[j]=1;
            v2[s+j]=1;
            v3[s-j+8]=1;
            dfs(s+1,sum+a[s][j]);
            v1[j]=0;
            v2[s+j]=0;
            v3[s-j+8]=0;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        sum=0,ans=0;
        memset(v1,0,sizeof(v1));
        memset(v2,0,sizeof(v2));
        memset(v3,0,sizeof(v3));
        for(int i=0;i<8;i++)
        for(int j=0;j<8;j++)
        scanf("%d",&a[i][j]);
        dfs(0,0);
        printf("%5d\n",ans);
    }
}


uva 167 The Sultan's Successors(回溯)

The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that the cou...
  • u011328934
  • u011328934
  • 2013-07-31 09:52:34
  • 937

UVA The Sultan's Successors

题目如下: The Sultan's Successors  The Sultan of Nubia has no children, so she...
  • u013840081
  • u013840081
  • 2014-07-16 16:29:27
  • 1057

The Sultan's Successors

经典八皇后问题变形,求出存在权值的情况下的最大值; // File Name: 1033.cpp // Author: bo_jwolf // Created Time: 2014年02月03日 星...
  • liujie619406439
  • liujie619406439
  • 2014-02-03 20:02:32
  • 1034

167 The Sultan's Successors

uva 167 The Sultan's Successors n皇后问题
  • tengfei461807914
  • tengfei461807914
  • 2017-01-02 01:59:54
  • 213

uva 167 - The Sultan's Successors

点击打开链接 题目意思:给定8 x 8的一个方格,对于该方格来说每一行上面只能取一个数,然后同行同列同对角线上都是不满足,求出所能得到的最大值 解题思路:8皇后的变形问题,变为求解...
  • cgl1079743846
  • cgl1079743846
  • 2012-07-16 01:00:47
  • 1191

UVA 167,The Sultan's Successors

题目:八皇后问题的基础上输入每个格子上的数值。问哪一组数的和最小 #include #include #include #include #include ...
  • surpasslll
  • surpasslll
  • 2017-03-22 17:59:37
  • 108

UVa 167 - The Sultan's Successors

题目链接:UVa 167 - The Sultan's Successors 回溯。 八皇后变形,每个棋格子多了权值,要求找到最大权值。 #include #include #include...
  • fobdddf
  • fobdddf
  • 2014-03-08 11:22:27
  • 387

uva 167 - The Sultan's Successors

The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that the...
  • Frankiller
  • Frankiller
  • 2012-07-30 20:21:12
  • 1112

uva 167 The Sultan's Successors

这是一题八皇后的题,挺经典的。 给你一个k,表示有k组测试,接下来有8x8列,找出 最大皇后位置和最大...
  • xf_zhen
  • xf_zhen
  • 2016-04-09 00:28:13
  • 165

UVA - 167 The Sultan's Successors

和八皇后问题一样,只不过在位置上放置了数字而已,有
  • L123012013048
  • L123012013048
  • 2014-10-21 23:13:51
  • 598
收藏助手
不良信息举报
您举报文章:UVa 167 - The Sultan's Successors(dfs)
举报原因:
原因补充:

(最多只允许输入30个字)