首页 > 搜索 > DFS搜索 > hdu 1642 The Sultan’s Successors-DFS-[解题报告]
2013
12-21

hdu 1642 The Sultan’s Successors-DFS-[解题报告]

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

输出:

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.

样例输入:

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

样例输出:

  260

 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 矩阵中。输入每个格子的权值。然后要根据八皇后那个摆放方式。求出摆放皇后位置权值相加最大的情况。。输出

思路:八皇后问题。一个格子放了皇后之后,它的行列斜线,将不能再放皇后,我们可以这样考虑:

一行只能放一个皇后,所以我们一行一行找过去。一共只要找8行。。开3个数组。一个代表列,一个代表主斜线。

一个代表副斜线。。如果一个点放了皇后。就把列,主副斜线进行标记。然后到下一行找下一个皇后。直到找不到进行回溯。。每次找到最后一行。都把权值和和max进行比较。如果比较大。就存进max。

这里有个很巧妙的地方。在标记主和副斜线的时候。注意:主斜线上每个点的行列值相差都相等。副斜线上每个点的行列值相加都相等。因此保存的时候就很方便了。主斜线为 行坐标 – 列坐标 +8 副斜线为行坐标 + 列坐标

具体看代码

vis[0]是列。vis[1]是副斜线。vis[2]是主斜线。

#include <stdio.h>
#include <string.h>
#define N 8
int t;
int map[10][10];
int vis[3][20];
int max;

void dfs(int cur, int sum)
{
    if (cur == N + 1)
    {
	if (max < sum)
	    max = sum;
	return;
    }
    for (int i = 1; i <= N; i ++)
    {
	if (!vis[0][i] && !vis[1][cur + i] && !vis[2][cur - i + N])
	{
	    vis[0][i] = vis[1][cur + i] = vis[2][cur - i + N] = 1;
	    dfs(cur + 1, sum + map[cur][i]);
	    vis[0][i] = vis[1][cur + i] = vis[2][cur - i + N] = 0;
	}
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t --)
    {
	max = 0;
	memset(vis, 0, sizeof(vis));
	memset(map, -1, sizeof(map));
	for (int i = 1; i <= N; i ++)
	    for (int j = 1; j <= N; j ++)
	    {
		scanf("%d", &map[i][j]);
	    }
	dfs(1, 0);
	printf("%5d\n", max);

    }
    return 0;
}

解题转自:http://blog.csdn.net/accelerator_/article/details/9565925


  1. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  2. 可以参考算法导论中的时间戳。就是结束访问时间,最后结束的顶点肯定是入度为0的顶点,因为DFS要回溯

  3. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  4. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.