UVA 167 (13.07.26)

 The Sultan's Successors 

The Sultan of Nubia has no children, so she has decided that thecountry will be split into up tok separate parts on her death andeach part will be inherited by whoever performs best at some test. Itis possible for any individual to inherit more than one or indeed allof the portions. To ensure that only highly intelligent peopleeventually become her successors, the Sultan has devised an ingenioustest. In a large hall filled with the splash of fountains and thedelicate scent of incense have been placedk chessboards. Eachchessboard has numbers in the range 1 to 99 written on each square andis supplied with 8 jewelled chess queens. The task facing eachpotential successor is to place the 8 queens on the chess board insuch a way that no queen threatens another one, and so that thenumbers on the squares thus selected sum to a number at least as highas one already chosen by the Sultan. (For those unfamiliar with therules of chess, this implies that each row and column of the boardcontains exactly one queen, and each diagonal contains no more thanone.)

Write a program that will read in the number and details of thechessboards and determine the highest scores possible for each boardunder these conditions. (You know that the Sultan is both a good chessplayer and a good mathematician and you suspect that her score is thebest attainable.)

Input

Input will consist of k (the number of boards), on a line by itself,followed byk sets of 64 numbers, each set consisting of eight linesof eight numbers. Each number will be a positive integer less than100. There will never be more than 20 boards.

Output

Output will consist of k numbers consisting of your k scores, eachscore on a line by itself and right justified in a field 5 characterswide.

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

题意:
其实就是八皇后问题的
但是每个点都是不一样的值 要求的是, 把八皇后放好, 而每个皇后位置的值加起来是最大的 做法: map[i][j]数组存好图上的值 将八皇后问题的代码稍改即可 判定一次可行的方案, 都要拿该方案的总值与之前的最大值比, 判定是否更新最大值~ 注意: 输出是"%5d", 要留空格的~ 
#include<stdio.h>

int C[8];
int map[8][8];
int Max;

void search(int cur, int sum) {
	if(cur == 8) {
		if(sum > Max)
			Max = sum;
	}
	for(int i = 0; i < 8; i++) {
		int mark = 1;
		C[cur] = i;
		for(int j = 0; j < cur; j++) {
			if(C[cur] == C[j] || cur-C[cur] == j-C[j] || cur+C[cur] == j+C[j]) {
				mark = 0;
				break;
			}
		}
		if(mark)
			search(cur+1, sum+map[cur][i]);
	}
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		for(int i = 0; i < 8; i++)
			for(int j = 0; j < 8; j++)
				scanf("%d", &map[i][j]);
		Max = 0;
		search(0, 0);
		printf("%5d\n", Max);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值