$POJ3278 Catch That Cow$

\(problem\)

深搜怕深 宽搜怕宽。

一般深搜T的宽搜就能A
一般宽搜T的深搜就能A

那么\(DINIC\)是什么(滑稽)

不说\(DINIC\)
回到\(BFS\)

BFS就是一层一层的搜。
支持queue。
也支持数组模拟队列。(数组模拟的话 数组大小把握好)

\(queue\)戳这里

回到题目。
仔细思考。

这题\(DFS\)肯定是行不通的 所以往BFS的方向去想。
那么怎么搜。

三种操作 *2 +1 -1
对吧。

+1 -1 难免会遇到重复的数字 然后可能喜提\(TLE\) or \(MLE\)
所以我们要带上记忆化。

然而。 记忆化的数组要开两倍。(因为可能*2)
有的时候稍稍不注意。就会RE。
而且还会检查不出来。
有时候数组开不下 还会MLE。
所以我建议还是用\(MAP\)
\(MAP\)有兴趣的戳这里

#ifdef Dubug

#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() {
    LL res(0),f(1);
    register char c ;
    while(isspace(c=getchar())) ;
    c == '-'? f = -1 , c = getchar() : 0 ;
    while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
    return res * f ;
}

int n , k ;
struct node {
    int x ;
    int step ;
};
queue <node> q ;
map<int,bool>vis ;
inline void bfs() {
    if(n >= k) {//因为只有-1 没有÷2
        cout << n - k << endl ;
        return ;
    }
    vis.clear() ;
    q.push(node {n,0}) ;
    while(!q.empty()) {
        int x = q.front().x , step = q.front().step ;
        q.pop() ;
        if(x == k) {
            cout << step << endl ;
            return ;
        }
        if(vis[x] or x < 1 or x > 100000) continue ;//判断边界以及记忆化
        vis[x] = true ;
        if(x < k) {//比它小只能增加值。 反之比它大就不能增加值了。
            q.push(node {x <<1,step+1}) ;
            q.push(node {x + 1,step+1}) ;
        }
        q.push(node {x - 1,step+1}) ;
    }
}
signed main () {
    n = In() , k = In() ;
    return bfs(),0;
}

猜你喜欢

转载自www.cnblogs.com/qf-breeze/p/10586007.html