UVA167【The Sultan‘s Successors】(递归与回溯、8皇后问题)

链接:UVA167【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. There will never be more than 20 boards.

输出描述:
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

题意:
在8*8的棋盘上每个格子有自己的价值
在棋盘上放8个互相攻击不到的皇后
求8个位置价值之和的最大值

数据范围:
询问次数不超过20,每格价值不超过100

思路:
8皇后问题的应用
用一个二维数组来存储每种情况皇后的摆放位置
P[i][j]表示方式 i 中第 j 行皇后的列位置
用递归来完成p数组

代码:

#include <cstdio> 
using namespace std; 
int P[1000][9]; //方式 i 中第 j 行皇后的列位置为 P[i][j] 
int tmp[8]; //当前方式中第 i 行皇后的列位置为 tmp[i] 
int n = 0; //方式数初始化
bool col[8] = {0}, left[15] = {0}, right[15] = {0}; //所有列和左右对角线未被选中

void fun(int r)
{
	if(r == 8)
	{
		for(int i = 0;i < 8;i++)
			P[n][i] = tmp[i];
		n++;
		return;
	}
	for(int c = 0;c < 8;c++)
	{
		int ld = c+r;
		int rd = (c-r)+7;
		if(!col[c]&&!left[ld]&&!right[rd])
		{
			col[c] = left[ld] = right[rd] = 1;
			tmp[r] = c;
			fun(r+1);
			col[c] = left[ld] = right[rd] = 0;
		}
	}
}

int main()
{
	fun(0);
	
	int Case;
	scanf("%d",&Case);
	int board[8][8]; 
	while(Case--)
	{
		int ans = 0;
		for(int i = 0;i < 8;i++)
			for(int j = 0;j < 8;j++)
				scanf("%d",&board[i][j]);		
		for(int i = 0;i < n;i++)
		{	
			int sum = 0;
			for(int j = 0;j < 8;j++)
				sum += board[j][P[i][j]];
			if(ans < sum) ans = sum; 
		}
		printf("%5d\n",ans);			
	}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔说给风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值