Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数。

坑:标题写了。有点不会写bfs了。。。

ac代码

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    smain();
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
const int maxn = 1e5 + 5;
int vis[maxn];
struct node {
    int x, t;
    node(int n = 0, int k = 0) :x(n), t(k) {}
};

int n, k;
bool check(int x) {
    if ( x > 1e5 || x < 0|| vis[x])return 0;
    else return 1;
}
void Run() {
    queue<node> Q;
    Q.push(node(n,0));
    vis[n] = 1;
    while (!Q.empty()) {
        node now = Q.front();
        Q.pop();
        
        if (now.x == k) { cout << now.t; break; }
        if (check(now.x + 1))Q.push(node(now.x + 1, now.t + 1)),vis[now.x + 1] = 1;
        if (check(now.x - 1))Q.push(node(now.x - 1, now.t + 1)), vis[now.x - 1] = 1;
        if (check(now.x * 2))Q.push(node(now.x * 2, now.t + 1)), vis[now.x * 2] = 1;
        

    }
}

void smain() {
    
    
    cin >> n >> k;

    Run();
    
}

 

转载于:https://www.cnblogs.com/SuuT/p/8985493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值