#include <stdio.h>
#define ABS(x) ( (x) > 0 ? (x) : -(x) )
#define INF (1 << 31 - 1)
#define MAX_STONES (20 + 1)
#define MAX_WEIGHT (100000)
int numOfStones;
int weights[MAX_STONES];
int totalWeigh;
int minDifferOfWeight = INF;
void DFS(int depth, int currentWeigh){
if (depth == numOfStones){
if (ABS(totalWeigh - currentWeigh - currentWeigh) < minDifferOfWeight)
minDifferOfWeight = ABS(totalWeigh - currentWeigh - currentWeigh);
return;
}
DFS(depth + 1, currentWeigh + weights[depth + 1]);
DFS(depth + 1, currentWeigh);
}
int main()
{
scanf("%d", &numOfStones);
int indexOfStone;
for (indexOfStone = 1; indexOfStone <= numOfStones; indexOfStone++){
scanf("%d", &weights[indexOfStone]);
totalWeigh += weights[indexOfStone];
}
DFS(0, 0);
printf("%d", minDifferOfWeight);
return 0;
}
URAL 1005 Stone Pile (DFS)
最新推荐文章于 2022-04-19 21:19:11 发布