zoukankan      html  css  js  c++  java
  • POJ 3278 Catch That Cow

    题意:在一条数轴上,一个人想从点n走到点k,他每分钟坐标可以+1或者-1或者×2,问最少多少分钟走到。

    解法:一个广搜……不知什么原因的wa了一天……最后重写了一遍才过……

    让我想到了某场cf的B题……也是从n走到k,可以+1或者/2,当时傻傻的写了好长的广搜……后来正解是贪心orz

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    bool vis[400010];
    struct node
    {
        int num, step;
        node(int num, int step) : num(num), step(step) {}
        node() {}
    };
    queue <node> q;
    int main()
    {
        int n, k;
        while(~scanf("%d%d", &n, &k))
        {
            while(!q.empty())
                q.pop();
            if(n >= k)
            {
                printf("%d
    ", n - k);
                continue;
            }
            int ans = k - n;
            memset(vis, 0, sizeof vis);
            q.push(node(n, 0));
            vis[n] = true;
            while(!q.empty())
            {
                node tmp = q.front();
                q.pop();
                if(tmp.num == k)
                {
                    ans = tmp.step;
                    break;
                }
                for(int i = 0; i < 3; i++)
                {
                    int ttmp;
                    switch(i)
                    {
                        case 0 : ttmp = tmp.num + 1;break;
                        case 1 : ttmp = tmp.num - 1;break;
                        case 2 : ttmp = tmp.num * 2;break;
                    }
                    if(ttmp >=0 && ttmp - k + tmp.step + 1 <= ans && !vis[ttmp])
                    {
                        vis[ttmp] = true;
                        q.push(node(ttmp, tmp.step + 1));
                    }
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    ajax
    ASP.net(c#)返回上一页效果(后退)代码
    审批时进行判断
    indexOf(String.indexOf 方法)判断标签是否存在
    不用代码隐藏自定义列表
    列表显示隐藏
    jq加载页面跳转指定的页面
    sharepoint2013部件保存后停留在保存页面
    Perl学习之四:语句(续)
    Perl学习之四:语句
  • 原文地址:https://www.cnblogs.com/Apro/p/4469022.html
Copyright © 2011-2022 走看看