• Catch That Cow POJ


    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数。

    坑:标题写了。有点不会写bfs了。。。

    ac代码

    #define _CRT_SECURE_NO_WARNINGS
    #include<cstring>
    #include<cctype>
    #include<cstdlib>
    #include<cmath>
    #include<cstdio>
    #include<string>
    #include<stack>
    #include<ctime>
    #include<list>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<sstream>
    #include<iostream>
    #include<functional>
    #include<algorithm>
    #include<memory.h>
    //#define INF 0x3f3f3f3f
    #define eps 1e-6
    #define pi acos(-1.0)
    #define e exp(1.0)
    #define rep(i,t,n)  for(int i =(t);i<=(n);++i)
    #define per(i,n,t)  for(int i =(n);i>=(t);--i)
    #define mp make_pair
    #define pb push_back
    #define mmm(a,b) memset(a,b,sizeof(a))
    //std::ios::sync_with_stdio(false);
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    void smain();
    #define ONLINE_JUDGE
    int main() {
        ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        long _begin_time = clock();
    #endif
        smain();
    #ifndef ONLINE_JUDGE
        long _end_time = clock();
        printf("time = %ld ms.", _end_time - _begin_time);
    #endif
        return 0;
    }
    const int maxn = 1e5 + 5;
    int vis[maxn];
    struct node {
        int x, t;
        node(int n = 0, int k = 0) :x(n), t(k) {}
    };
    
    int n, k;
    bool check(int x) {
        if ( x > 1e5 || x < 0|| vis[x])return 0;
        else return 1;
    }
    void Run() {
        queue<node> Q;
        Q.push(node(n,0));
        vis[n] = 1;
        while (!Q.empty()) {
            node now = Q.front();
            Q.pop();
            
            if (now.x == k) { cout << now.t; break; }
            if (check(now.x + 1))Q.push(node(now.x + 1, now.t + 1)),vis[now.x + 1] = 1;
            if (check(now.x - 1))Q.push(node(now.x - 1, now.t + 1)), vis[now.x - 1] = 1;
            if (check(now.x * 2))Q.push(node(now.x * 2, now.t + 1)), vis[now.x * 2] = 1;
            
    
        }
    }
    
    void smain() {
        
        
        cin >> n >> k;
    
        Run();
        
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    Intellij 常用技巧-持续更新
    Android界面组件的四种启动方式
    Preference Screen 首选项
    Oracle OCI-22053:溢出错误
    SQLPLUS使用
    Oracle中数字格式的文本化处理
    MP4V2 移植 (基于imx6 平台)
    IMX6Q camera 应用编程之 摄像头裁剪
    IMX6Q camera驱动分析 (4)
    IMX6Q Camera驱动分析 (3)
  • 原文地址:https://www.cnblogs.com/SuuT/p/8985493.html
Copyright © 2020-2023  润新知