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

来源:互联网 发布:淘宝pr 值多少 编辑:程序博客网 时间:2024/06/07 23:44

 

题意:给定一个8*8的棋盘,每个上都有一个固定的数,放置8个皇后,

使他们不在同行,同列,主斜线和副斜线上,然后求出最大的权值。

思路:

一行只能放一个皇后,所以我们一行一行找过去。一共只要找8行。

开3个数组。一个代表列,一个代表主斜线。一个代表副斜线。

如果一个点放了皇后。就把列,主副斜线进行标记。然后到下一行

找下一个皇后。直到找不到进行回溯。。每次找到最后一行。都把

权值和和max进行比较。如果比较大。就存进max。

 

注意:主斜线上每个点的行列值相差都相等。副斜线上

每个点的行列值相加都相等。所以

主斜线为 行坐标 - 列坐标

副斜线为 行坐标 + 列坐标

 


 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 1617 18 19 20 21 22 23 2425 26 27 28 29 30 31 3233 34 35 36 37 38 39 4041 42 43 44 45 46 47 4848 50 51 52 53 54 55 5657 58 59 60 61 62 63 64

Sample output

  260
 #include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int a[10][10],b[10];int Max;int tmp;void dfs(int cur)  {  int i,j;      if(cur==8)      {         tmp=0;        for(i=0;i<8;i++)           tmp+=a[i][b[i]];         if(tmp>Max)             Max=tmp;        return;      }      for(i=0;i<8;i++)      {          int flag=1;         for(j=0;j<cur;j++)            if(i==b[j]||i+cur==j+b[j]||cur-i==j-b[j])         {             flag=0;             break;         }        if(flag)        {            b[cur]=i;            dfs(cur+1);        }      }  }int main(){  int i,j,k,t; scanf("%d",&t); while(t--) {    for(i=0;i<8;i++)      for(j=0;j<8;j++)        scanf("%d",&a[i][j]);    Max=0;    dfs(0);   printf("%5d\n",Max); }    return 0;} 

原创粉丝点击