呃呃呃呃呃懒得用dp
2024-06-28 11:58:02
发布于:广东
8阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct node{
int val, step;
};
int n, m;
int a[100005];
bool vis[100005];
int bfs(){
queue <node> q;
q.push({0, 0});
while(!q.empty()){
node head = q.front();
q.pop();
if(head.val == m) return head.step;
for(int i = 1; i <= n; i++){
if(!vis[head.val + a[i]]){
vis[head.val + a[i]] = 1;
q.push({head.val + a[i], head.step + 1});
}
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}cout << bfs();
return 0;
}
这里空空如也
有帮助,赞一个