首页 > 其他分享 >1013 [USACO 2007 Ope S]Catch That Cow bfs 剪枝

1013 [USACO 2007 Ope S]Catch That Cow bfs 剪枝

时间:2022-08-18 19:11:56浏览次数:86  
标签:剪枝 cow Cow point int USACO vis Farmer John

 链接:https://ac.nowcoder.com/acm/contest/26077/1013
来源:牛客网

题目描述

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

输入

复制
5 17

输出

复制
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.

分析

题目范围比较小,所以直接bfs+剪枝就可以做了。但是感觉应该是位运算的结论题。也不对,三种操作。+1 和 *2 倒是可以直接做,-1

//-------------------------代码----------------------------

//#define int ll
const int N = 2e5+10;
int n,m,k;
int vis[N];
void bfs(int u) {
    queue<int> q;
    q.push(u);
    vis[u] = 0;
    while(q.size()) {
        auto t = q.front();q.pop();
        if(t == k) {
            cout<<vis[k]<<endl;
            rt;
        }
        if(t-1>=0 && !vis[t-1])q.push(t-1),vis[t-1] = vis[t]+1;
        if(t+1<=k && !vis[t+1])q.push(t+1),vis[t+1] = vis[t] + 1;
        if(t*2<=2*k && !vis[t*2])q.push(t*2),vis[t*2] = vis[t]+1;
        
    }
}

void solve()
{
//    cin>>n>>m;
    cin>>n>>k;
    if(k <= n) {
        cout<<n - k<<endl;
        rt;
    }
    bfs(n);
}
void main_init() {}
signed main(){
    AC();clapping();TLE;
    cout<<fixed<<setprecision(12);
    main_init();
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

标签:剪枝,cow,Cow,point,int,USACO,vis,Farmer,John
From: https://www.cnblogs.com/er007/p/16599808.html

相关文章

  • PyTorch 剪枝
    pytorch实现剪枝的思路是生成一个掩码,然后同时保存原参数、mask、新参数,如下图 pytorch剪枝分为局部剪枝、全局剪枝、自定义剪枝;局部剪枝是对模型内的部分模......
  • P3120 [USACO15FEB]Cow Hopscotch G
    传送门思路朴素的想法就是一个\(O(n^2m^2)\)的转移:\[f_{i,j}=\sum_{x=1}^{i-1}\sum_{y=1}^{j-1}f_{x,y}*[a_{i,j}!=a_{x,y}]\]约束条件如此多,思考用cdq分治来优化......
  • P7154 [USACO20DEC] Sleeping Cows P(DP)
    主要是状态设计比较难想,但其实可以理性地推出来。P7154[USACO20DEC]SleepingCowsP考虑最终一个合法状态是怎么样的:一定是一堆小牛棚,一堆大奶牛,最大的牛棚小于最小的......
  • P6144 [USACO20FEB]Help Yourself P(DP+线段树)
    P6144[USACO20FEB]HelpYourselfP将线段按照了\(r\)排序,设右端点为\(r\)的答案为\(f_r\),发现这样转移非常困难。\(\color{yellow}{\bigstar\texttt{Trick}}\):区间......
  • NC24870 [USACO 2009 Dec G]Video Game Troubles
    题目链接题目题目描述FarmerJohn'scowslovetheirvideogames!FJnoticedthatafterplayingthesegamesthathiscowsproducedmuchmoremilkthanusual,s......