167

就是八皇后问题的小变种,加了个权值,最后算一下权值即可

//============================================================================
// Name        : 167.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int w[10][10], C[10], tag, Max, N, vis[3][20];

void dfs(int cur){
	if(cur == 8){
		int temp = 0;
		for(int i = 0;i < 8;i++){
//			printf("%d ", C[i]);
			temp += w[i][C[i]];
		}
		if(Max < temp) Max = temp;
		return;
	}
	else{
		for(int i = 0;i < 8;i++){
			if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+8]){
				C[cur] = i;
				vis[0][i] = vis[1][cur+i] = vis[2][cur-i+8] = 1;
				dfs(cur+1);
				vis[0][i] = vis[1][cur+i] = vis[2][cur-i+8] = 0;
			}
		}
	}
}



int main() {
	freopen("a.txt", "r", stdin);
	scanf("%d", &N);
	while(N--){
		memset(C, 0, sizeof(C));
		memset(w, 0, sizeof(w));
		memset(vis, 0, sizeof(vis));
		for(int i = 0;i < 8;i++){
			for(int j = 0;j < 8;j++){
				scanf("%d", &w[i][j]);
			}
		}
		Max = 0;
		dfs(0);
		printf("%5d\n", Max);
	}
	return 0;
}

posted @ 2011-05-26 10:01  KOKO's  阅读(103)  评论(0编辑  收藏  举报