牛客假日团队赛5 LCatch That Cow HDU 2717 (BFS)
鏈接:https://ac.nowcoder.com/acm/contest/984/L
來源:牛客網(wǎng)
Catch That Cow
時(shí)間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 <= N <= 100,000) on a number line and the cow is at a point K (0 <= K <= 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
Walking: FJ can move from any point X to the points X-1 or X+1 in a single minute Teleporting: FJ can move from any point X to the point 2*X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
輸入描述:
Line 1: Two space-separated integers: N and K
輸出描述:
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
示例1
輸入
復(fù)制
5 17
輸出
復(fù)制
4
說明
Farmer John starts at point 5 and the fugitive cow is at point 17.
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
題意:
給你一個(gè)位置n,一個(gè)位置k,都在水平的坐標(biāo)軸上,你每一次可以向左或者向右走一步,或者從當(dāng)先的x移動(dòng)到2*x 位置,問你從n到k最小需要多少步?
思路:
直接BFS搜,用一個(gè)數(shù)組vis 維護(hù) 每一個(gè)位置是否被訪問,由于BFS的性質(zhì),一個(gè)位置如果已經(jīng)被訪問過了,就不需要再訪問了,因?yàn)楫?dāng)前的步數(shù)一定大于等于訪問時(shí)候的步數(shù)。
細(xì)節(jié)見代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int* p); const int maxn = 2000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ bool vis[maxn]; queue<pii> q; int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);int n,k;cin>>n>>k;vis[n]=1;q.push(mp(n,0));pii t;while(!q.empty()){t=q.front();q.pop();if(t.fi==k){cout<<t.se<<endl;break;}else{if(t.fi+1<maxn&&!vis[t.fi+1]){vis[t.fi+1]=1;q.push(mp(t.fi+1,t.se+1));}if(t.fi-1>=0&&!vis[t.fi-1]){vis[t.fi-1]=1;q.push(mp(t.fi-1,t.se+1));}if(t.fi*2<maxn&&!vis[t.fi*2]){vis[t.fi*2]=1;q.push(mp(t.fi*2,t.se+1));}}}return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11253060.html
總結(jié)
以上是生活随笔為你收集整理的牛客假日团队赛5 LCatch That Cow HDU 2717 (BFS)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CCPC-Wannafly Winter
- 下一篇: 高效便捷地创建单元格数据图表