没有什么是广搜解决不了的(
2024-06-28 21:39:43
发布于:广东
29阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <queue>
#include <bitset>
using namespace std;
struct node{
int val, step;
};
int n, m;
int a[] = {0, 1, 5, 11};
bitset <2000005> vis;
int bfs(){
queue <node> q;
q.push({0, 0});
while(!q.empty()){
node head = q.front();
q.pop();
if(head.val == n) return head.step;
for(int i = 1; i <= 3; i++){
if(!vis[head.val + a[i]]){
vis.set(head.val + a[i]);
q.push({head.val + a[i], head.step + 1});
}
}
}
}
int main(){
cin >> n;
cout << bfs();
return 0;
}
全部评论 3
但是数组要开大一点
2024-07-31 来自 江苏
0动态规划也可以
#include<bits/stdc++.h>
using namespace std;
int dp[14000000];int n;
int x,y,z;
int main()
{
cin>>n;for(int i=1;i<=n;i++){ dp[i]=i; if(1<=i and i<5) dp[i] = i; if(5<=i and i<11) dp[i] = min(i,dp[i-5]+1); if(i>=11) dp[i] = min(dp[i-5]+1,dp[i-11]+1); } cout<<dp[n]; return 0;
}
2024-07-31 来自 江苏
0正解就是动态规划,但也可以用广搜
2024-07-31 来自 湖南
0
呵呵哈哈🤓
2024-06-28 来自 广东
0
有帮助,赞一个