zoukankan      html  css  js  c++  java
  • POJ 3278

    最近做题养成了一个不太好的习惯,习惯性的先去看discuss有没有坑,越是惧怕错误越可能出错,之后的锻炼,出错再去check discuss吧

    简单的BFS

    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <stack>
    #include <map>
    #include <set>
    using namespace std;
    
    const int maxn= 1e5;
    
    bool vis[maxn+5];
    int dis[maxn+5];
    
    int BFS(const int s, const int e)
    {
    	if (s== e){
    		return 0;
    	}
    	memset(vis, 0, sizeof(vis));
    	queue<int> Q;
    	vis[s]= 1;
    	dis[s]= 0;
    	Q.push(s);
    	int cur, v;
    
    	while (!Q.empty()){
    		cur= Q.front();
    		Q.pop();
    		v= dis[cur]+1;
    
    		int np= cur<<1;
    		if (e== np){
    			return v;
    		}
    		if (np>= 0 && np<= maxn && !vis[np]){
    			vis[np]= 1;
    			dis[np]= v;
    			Q.push(np);
    		}
    
    		np= cur-1;
    		if (e== np){
    			return v;
    		}
    		if (np>= 0 && np<= maxn && !vis[np]){
    			vis[np]= 1;
    			dis[np]= v;
    			Q.push(np);
    		}
    
    		np= cur+1;
    		if (e== np){
    			return v;
    		}
    		if (np>= 0 && np<= maxn && !vis[np]){
    			vis[np]= 1;
    			dis[np]= v;
    			Q.push(np);
    		}
    	}
    
    	return -1;
    }
    int main()
    {
    	int n, k;
    	scanf("%d %d", &n, &k);
    	printf("%d
    ", BFS(n, k));
    
    	return 0;
    }
    
  • 相关阅读:
    Linux thread 泄露问题
    QQ通讯原理
    小谈Onlinegame服务器端设计
    Linux 如何打开端口
    linux系统软件更新
    MVC
    wubi+ubuntu
    [转]openfire源码部署~
    Apache开源项目分类列表[转]
    [转]java.util.MissingResourceException: Can't find bundle for base name
  • 原文地址:https://www.cnblogs.com/Idi0t-N3/p/14679884.html
Copyright © 2011-2022 走看看