CF6D 题解
一 关于题解
思路来源:
本篇题解与“胡金梁”同学题解思路相似,但是“胡金梁”同学的题解在
测试数据:
使原题解
10 2 1 12 13 15 14 14 11 12 14 11 15
二 解题思路
方法一: \text{IDA*(TLE)}
迭代加深:不断加深搜索深度。 由于
方法二: \text{DFS}
详解写在代码批注里。
三 参考代码
#include <cstdio>
#include <cstring>
#define re register
using namespace std;
int n, a, b, sum, cnt;
int num[15], use[15], at[50];
bool IDA_star(int step)
{
int tot = 0, count = 0;
for (int i = 1; i <= n; ++i)
{
if (use[i] > 0) tot += use[i];
if (use[i] >= 0) count++;
}
if (count == 0) return true;
if ((tot / (a + b + b)) >= step) return false;
for (int i = 2; i < n; ++i)
{
//printf("%d ", i);
at[++cnt] = i;
use[i] -= a;
use[i - 1] -= b;
use[i + 1] -= b;
if (IDA_star(step - 1) == true) return true;
use[i] += a;
use[i - 1] += b;
use[i + 1] += b;
--cnt;
}
return false;
}
int main()
{
scanf("%d %d %d", &n, &a, &b);
for (re int i = 1; i <= n; ++i)
{
scanf("%d", &num[i]);
use[i] = num[i];
sum += num[i];
}
for (re int i = int(sum / (a + b + b) + 1); ; ++i)
{
cnt = 0;
for (re int j = 1; j <= n; ++j)
use[j] = num[j];
memset(at, 0, sizeof(at));
//printf("%dth:\n", i);
if (IDA_star(i) == true)
{
printf("%d\n", i);
for (re int j = 1; j <= i; ++j)
printf("%d ", at[j]);
return 0;
}
}
}