首页 > 代码库 > [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

[kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<vector> 5 #include<queue> 6 #include<cstring> 7 using namespace std; 8 int n, k, c, ans; 9 int num[100005];10 int main()11 {12 //    freopen("in.in","r",stdin);13     while(cin>>n>>k)14     {15         ans = 0;16         queue<int>q;17         q.push(n);18         num[n] = 1;19         while(q.size())20         {21             c = q.front();22             if(c == k)23             {24                 ans = num[c];25                 break;26             }    27             q.pop();28             if(!num[c-1] && c-1>=0)29             {30                 num[c-1] = num[c] + 1;31                 q.push(c-1);32             }33             if(c+1<=100000 && !num[c+1])34             {35                 num[c+1] = num[c] + 1;36                 q.push(c+1);37             }38             if(c*2<=100000 && !num[2*c])39             {40                 num[2*c] = num[c] + 1;41                 q.push(2*c);42             }43         }44         cout<<ans-1<<endl;45     }46     return 0;47 } 

 

[kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow