您的位置:首页 > 其它

hdu 2717 Catch That Cow

2015-08-10 15:02 162 查看
题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2717

解题思路:

简单的搜索题,直接搜就可以了。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 100000;
int n,k;
int vis[N+10];

void bfs(){
    queue<int> q;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    q.push(n);
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        if(cur == k)
            return;
        int next = cur-1;
        if(next>=0 && !vis[next]){
            vis[next] = vis[cur]+1;
            q.push(next);
        }
        next = cur+1;
        if(!vis[next]){
            vis[next] = vis[cur]+1;
            q.push(next);
        }
        next = 2*cur;
        if(next<=N && (next-k < k-cur) && !vis[next]){
            vis[next] = vis[cur]+1;
            q.push(next);
        }
    }
}

int main(){
    while(~scanf("%d%d",&n,&k)){
        if(n >= k)
            vis[k] = n-k;
        else
            bfs();
        printf("%d\n",vis[k]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: