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

    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<string.h>
    #include<algorithm>
    
    using namespace std;
    const int maxn=100007;
    int n, k, vis[maxn];;
    struct node
    {
        int x, step;
    };
    
    int bfs()
    {
        queue<node>Q;
        node p, q;
        p.x=n;
        vis[p.x]=1;
        p.step=0;
        Q.push(p);
        while(!Q.empty())
        {
            q=Q.front();
            Q.pop();
            if(q.x==k)return q.step;
    
            for(int i=0; i<3; i++)
            {
                if(i==0)
                    p.x=q.x+1;
                if(i==1)
                    p.x=q.x-1;
                if(i==2)
                    p.x=q.x*2;
    
                if(p.x>=0&&p.x<=100000&&!vis[p.x])
                {
                    vis[p.x]=1;
                    p.step=q.step+1;
                    Q.push(p);
                }
            }
        }
        return -1;
    }
    int main()
    {
        while(~scanf("%d%d", &n, &k))
        {
            memset(vis, 0, sizeof(vis));
            int ans=bfs();
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    多行文字垂直居中效果(利用flex)
    Switch
    Scanner
    Method
    Recursion递归
    for
    if
    dowhile
    while
    DataType 数据类型
  • 原文地址:https://www.cnblogs.com/w-y-1/p/6539027.html
Copyright © 2011-2022 走看看