# UVa 167 - The Sultan's Successors（dfs）

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

## 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);
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120