CodeForces - 520 Two Buttons(bfs,dfs,逆推)

本文解析了一道算法题目,目标是找到将一个数通过乘2或减1操作转换成另一个数所需的最少步骤。文章提供了三种解决方案:广度优先搜索(BFS)、深度优先搜索(DFS)和逆向思维法,并详细阐述了每种方法的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目(https://vjudge.net/problem/CodeForces-520B)OC

大致意思 :有两个数m,n,执行乘2或者减1的操作(m不能小于0),问要得到n所要执行的最少操作次数

//bfs的话注意边界的now.num<n*2,别的就是模板了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<cstring>
#include<math.h>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
using namespace std;
struct node
{
    int num;
    int step;
};
queue<node>q;
int m,n;
int book[100001];
int bfs()
{
    node f;
    f.num=m;
    book[m]=1;
    f.step=0;
    q.push(f);
    while(!q.empty())
    {
        node top=q.front();
        node now;
        q.pop();
        if(top.num==n)
            return top.step;
        for(int i=0;i<2;i++)
        {
            if(i==0)
            {
                now.num=top.num-1;
               if(now.num>0&&now.num<n*2&&book[now.num]==0)
               {
                   now.step=top.step+1;
                   book[now.num]=1;
                   q.push(now);
               }
            }
            if(i==1)
            {
                now.num=top.num*2;
                if(now.num>0&&now.num<n*2&&book[now.num]==0)
                {
                   now.step=top.step+1;
                   book[now.num]=1;
                   q.push(now);
                }
            }
         }
    }
}
int main()
{
    scanf("%d%d",&m,&n);
    memset(book,0,sizeof(book));
    if(m>=n)
    {
        printf("%d\n",m-n);
    }
    else
    printf("%d\n",bfs());
    return 0;
}
  • dfs的话刚开始写一直WA,后来一直卡在test9,11,应该是数字大了10000,数组没开大,溢出了,**然后是memset给数组a赋初值的时候,不能赋值为无穷大,改成fill函数(说实话,fill函数也蛮好用的QAQ)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include**<cstdlib>
#include<cstring>
#include<math.h>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int m,n;
int a[30001];
void dfs(int x,int step)
{
    if(x<=0)
        return;
    if(a[x]<step)//剪枝,这次的step与上条到达该出的路径长度比较
        return;
        
    a[x]=step;
    if(x>n)
    {
       dfs(n,step+x-n);
       return;
    }
    if(x==n)
    {
       a[x]=min(step,a[x]);//这次的step与上一次标记a[x]比较取小的
       return ;
    }
   dfs(2*x,step+1);
   dfs(x-1,step+1);
}
int main()
{
    scanf("%d%d",&m,&n);
    fill(a,a+30001,999999);
    a[m]=0;
    dfs(m,0);
    cout<<a[n]<<endl;
    return 0;
}
  • 然后是逆推了,这段代码比dfs还简单,如果结果是偶数,那一定是偶数/2变过来的,反过来*2,如果是奇数,就是-1变过来的,反过来加上1,当操作过程遇到m>n时,一步一步减1
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<cstring>
#include<math.h>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    int ans=0;
    while(m<n)
    {
            if(n%2==0)
            {
                n/=2;
            }
            else if(n%2==1)
            {
                n++;
            }
            ans++;

    }
        printf("%d\n",ans+m-n);
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值