You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
983 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
1 year ago
int n; // 电桩数
int d; // 冲一次电能跑的距离
2 years ago
int res = INF;
int a[N];
/*
5 4
3 1 2 2 1
3
*/
void dfs(int u, int r, int c) {
if (u == n + 1) {
res = min(res, c);
return;
}
1 year ago
// 现在剩余的油量是r,加满的话油量是d
// 如果现在剩余的油量r不足以跑完下一个路a[u+1],那么必须在本站加油到d
1 year ago
if (d < a[u + 1]) return;
2 years ago
1 year ago
if (r >= a[u + 1]) { // 我有两种选择1、在本站不加油2、我就加满
dfs(u + 1, r - a[u + 1], c);
dfs(u + 1, d - a[u + 1], c + 1);
} else
dfs(u + 1, d - a[u + 1], c + 1);
2 years ago
}
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(0, 0, 0);
1 year ago
if (res == INF)
cout << -1 << endl;
else
cout << res << endl;
2 years ago
return 0;
}