Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

有没有哪位大神帮我看看这代码,,跟求最小步数的题类似,但需要考虑的情况太多了

Posted by AK47biubiubiu at 2016-01-27 15:33:32 on Problem 3278
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,k,sum;
void dfs(int x,int y,int s)
{
    if(x<=0) return ;
    if(x==y)
    {
        sum=min(sum,s);
        return ;
    }
    if(s>sum) return ;
    s++;
    dfs(x+1,y,s);
    dfs(x-1,y,s);
    dfs(2*x,y,s);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        sum=100000;
        if(n>=k) printf("%d\n",n-k);
        else
        {
            if(n==0)
            {
                n=1;
                dfs(n,k,0);
                printf("%d\n",sum+1);
            }
            else
            {
                dfs(n,k,0);
                printf("%d\n",sum);
            }
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator