Files
python/TangDou/LuoGuBook/P2036_DFS.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
791 B
C++

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 15;
int a[N], b[N], n;
int ans = INF;
/**
* @param step 第几步
* @param x 目前的酸度值
* @param y 目前的苦度值
*/
void dfs(int step, int x, int y) {
if (step == n) {
//清水不行~
if (x == 0 && y == 0) return;
//更新ans
ans = min(abs(x - y), ans);
return;
}
//选择
dfs(step + 1, (x == 0 ? 1 : x) * a[step + 1], y + b[step + 1]);
//放弃
dfs(step + 1, x, y);
}
int main() {
//n种配料
cin >> n;
//分别读入酸度和苦度
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
dfs(0, 0, 0);
//输出
printf("%d\n", ans);
return 0;
}