解题思路:
记录一个最大值,如果当前搜索过程中的最大值 + 剩下层数 * maxv <= res
则不可能更新最大值 , 直接return
故只需要加一句剪枝代码:
if (s + (n - x + 1) * maxv <= res)
return;
参考代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 15;
int g[N][N];
int n;
bool st[N];//第i列的人是否选择
int maxv;
int res;
void dfs(int x,int s)//第x行,总和为s
{
if (x > n)
{
res = max(res,s);
return;
}
if (s + (n - x + 1) * maxv <= res)
return;
for (int i = 1 ; i <= n ; i ++)
{
if (st[i]) continue;
st[i] = 1;
dfs(x + 1,s + g[x][i]);
st[i] = 0;
}
}
int main()
{
cin >> n;
for (int i = 1 ; i <= n ; i ++)
for (int j = 1 ; j <= n ; j ++)
{
cin >> g[i][j];
maxv = max(g[i][j],maxv);
}
dfs(1,0);
cout << res << endl;
return 0;
}
0.0分
1 人评分
拆分位数 (C语言代码)浏览:1326 |
A+B for Input-Output Practice (II) (C语言代码)浏览:989 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:904 |
简单的a+b (C语言代码)浏览:596 |
母牛的故事 (C语言代码)浏览:1427 |
1124题解浏览:591 |
蓝桥杯历届试题-翻硬币 (C++代码)浏览:872 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:463 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:610 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:503 |