Uva--167(暴力,八皇后)

2014-07-16 12:56:22

题意&思路:变种的八皇后,只不过要记录路径和。自己敲了个DFS,当然佳哥写的也是一种思路:通过标记每列皇后的行数。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 int k,tmax,g[10][10],used[10][10];
 8 
 9 void Dfs(int col,int sum){
10     if(col > 8){
11         tmax = max(tmax,sum);
12         return;
13     }
14     for(int i = 1; i <= 8; ++i){ //row
15         int flag = 1;
16         for(int j = 1; j < col; ++j){
17             if(used[i][j] || (i-col+j>=1 && used[i-col+j][j]) || (i+col-j<=8 && used[i+col-j][j])){
18                     flag = 0;
19                     break;
20             }
21         }
22         if(flag){
23             used[i][col] = 1;
24             Dfs(col + 1,sum + g[i][col]);
25             used[i][col] = 0;
26         }
27     } 
28 }
29 
30 int main(){
31     scanf("%d",&k);
32     while(k--){
33         for(int i = 1; i <= 8; ++i){
34             for(int j = 1; j <= 8; ++j){
35                 scanf("%d",&g[i][j]);
36             }
37         }
38         memset(used,0,sizeof(used));
39         tmax = 0;
40         Dfs(1,0);
41         printf("%5d\n",tmax);
42     }
43     return 0;
44 }

转载于:https://www.cnblogs.com/naturepengchen/articles/3848427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值