codeforce295# B

来源:互联网 发布:c语言编写的聊天程序 编辑:程序博客网 时间:2024/05/23 02:01

广度搜索求最短距离.

/*********************************************** * Author: fisty * Created Time: 2015/3/3 21:27:29 * File Name   : 295B.cpp *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define Memset(x, a) memset(x, a, sizeof(x))const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;#define FOR(i, a, b) for(int i = a;i < b; i++)#define MAX_N 10100int n, m;int d[MAX_N];int bfs(){    queue<int> que;    Memset(d, 0x3f);    d[n] = 0;    que.push(n);    while(que.size()){        int v = que.front(); que.pop();        if(v == m) return d[m];        for(int i = 0;i < 2; i++){            int _v;            if(i == 0){                _v = 2 * v;               }else{                 _v = v - 1;            }            if(d[_v] == INF && _v <= 10000 && _v >= 0){                d[_v] = d[v] + 1;                que.push(_v);            }        }    }}int main() {    //freopen("in.cpp", "r", stdin);    cin.tie(0);    ios::sync_with_stdio(false);    cin >> n >> m;    int ans = bfs();    cout << ans << endl;    return 0;}


0 0