Catch That Cow POJ

来源:互联网 发布:淘宝钻号出售 编辑:程序博客网 时间:2024/05/24 22:42

[0+) 一行上有A,B两个位置,A要通过一些操作到达B点,求最短操作数。操作为左右移动一个单位或者坐标翻倍。
AB坐标<=(N=1e5)
分析:
构建BFS模型。node内保存位置和操作数。
坑点:
没注意 0 也符合题意,设置判断范围时要取 2*N 才合理。

#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <cstdio>#include <queue>#include <iostream>using namespace std;typedef long long ll;typedef pair <int,int> pii;#define mem(s,t) memset(s,t,sizeof(s))#define D(v) cout<<#v<<" "<<v<<endl#define inf 0x3f3f3f3f#define pb push_back//#define LOCALconst int mod=1e9+7;const int MAXN =2e5+10;int n,k;struct node{    node(int X,int c){x=X,cnt=c;}    int x,cnt;};int main(){    cin>>n>>k;    queue<node> que;    que.push(node(n,0));    int vis[MAXN];    mem(vis,0);    vis[n]=1;    while(!que.empty()){        node t=que.front();        que.pop();        if(t.x==k){            printf("%d\n",t.cnt);            break;        }        if(t.x>=1 && !vis[t.x-1]){            que.push(node(t.x-1,t.cnt+1));            vis[t.x-1]=1;        }        if(2*t.x<MAXN && !vis[2*t.x]) {            que.push(node(2*t.x,t.cnt+1));            vis[2*t.x]=1;        }        if(t.x+1<MAXN && !vis[t.x+1]){            que.push(node(t.x+1,t.cnt+1));            vis[t.x+1]=1;        }    }    return 0;}
原创粉丝点击