NC25080. [USACO 2007 Ope S]Catch That Cow
描述
输入描述
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
输入:
5 17
输出:
4
说明:
Farmer John starts at point 5 and the fugitive cow is at point 17.C++(g++ 7.5.0) 解法, 执行用时: 4ms, 内存消耗: 896K, 提交时间: 2023-04-24 17:26:25
#include<bits/stdc++.h> int n,k,a[100001]; std::queue<int>q; int main(){ std::cin>>n>>k; q.push(n); a[n]=1; while(!a[k]){ int u=q.front(); q.pop(); if(u<=99999&&!a[u+1])a[u+1]=a[u]+1,q.push(u+1); if(u>=1&&!a[u-1])a[u-1]=a[u]+1,q.push(u-1); if(u<=50000&&!a[u*2])a[u*2]=a[u]+1,q.push(u*2); } std::cout<<a[k]-1; }