acgo题库
  • 首页
  • 题库
  • 题单
  • 竞赛
  • 讨论
  • 排行
  • 团队
  • 备赛专区

    竞赛

    • CSP-J/S
    • 蓝桥杯

    考级

    • GESP
    • CPA
    • 电子学会考级
登录
注册
题目详情题解(0)讨论(0)提交记录(0)
  • 【正经题解】最少问题

    userId_undefined

    AC君

    管理员
    倔强青铜
    12阅读
    0回复
    0点赞
  • .

    #include<bits/stdc++.h> using namespace std; const int N=100005; int n,m; int fx[3]={1,-1,2}; bool vis[N]={}; struct data{ int x,cnt; }; int bfs(){ queue<data> q; q.push({n,0}); vis[n]=true; while (q.size()){ data t=q.front(); q.pop(); int x=t.x,step=t.cnt; if (x==m) return step; for (int i=0;i<3;i++){ int nx; if (fx[i]==2) nx=x*fx[i]; else nx=x+fx[i]; if (nx>=0&&nx<=N&&!vis[nx]){ q.push({nx,step+1}); vis[nx]=true; } } } return -1; } int main(){ cin >> n >> m; cout << bfs(); return 0; }

    userId_undefined

    一只小小苦

    倔强青铜
    9阅读
    0回复
    0点赞
首页