The Great Mixing
这道题非常有意思
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
int a[2234];
int dp[5005];
int n, k;
int vis[5005];
vector<int>v;
int bfs() {
int i, sum = 0;
queue<int>q;
for (i = 0; i < v.size(); i++) {
int m = v[i] + 1000;
if(vis[m]==0){
q.push(m);
vis[m]=1;
dp[m] = 1;
}
if(m==1000) return 1;
}
int temp;
while (!q.empty()) {
temp = q.front();
q.pop();
for (i = 0; i < v.size(); i++) {
sum = temp + v[i];
if (dp[sum]>dp[temp] + 1 && sum <= 2000 && sum >= 0){
dp[sum] = dp[temp] + 1;
if (sum == 1000)return 1;
if (vis[sum] == 0) {
q.push(sum);
vis[sum] = 1;
}
}
}
}
return -1;
}
int main()
{
memset(a, 0, sizeof(a));
memset(dp, inf, sizeof(dp));
cin >> n >> k;
int i, t;
for (i = 1; i <= k; i++) {
scanf("%d",&t);
if(a[t-n+1000]==0){
v.push_back(t-n);
}
a[t-n+1000] = 1;
}
bfs();
if (dp[1000] != inf)
cout << dp[1000] << endl;
else cout << -1 << endl;
return 0;
}