4 条题解

  • 1
    @ 2025-3-9 19:52:35
    #include <bitset>
    #include <cctype>
    #include <cerrno>
    #include <clocale>
    #include <cmath>
    #include <complex>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <ctime>
    #include <cwchar>
    #include <cwctype>
    #include <complex.h>
    #include <deque>
    #include <exception>
    #include <fstream>
    #include <functional>
    #include <fenv.h>
    #include <iomanip>
    #include <ios>
    #include <iosfwd>
    #include <iostream>
    #include <istream>
    #include <inttypes.h>
    #include <limits>
    #include <list>
    #include <map>
    #include <ostream>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdexcept>
    #include <streambuf>
    #include <string>
    #include <stdbool.h>
    #include <stdint.h>
    #include <tgmath.h>
    #include <utility>
    #include <vector>
    
    using namespace std;
    
    #define LL long long
    const int N = 2e5 + 10;
    const int INF = 0x3f3f3f3f;
    
    struct node{
    	int pos , step;
    };
    
    int n , k; 
    bool v[N];
    
    void bfs(int x){
    	queue<node> q;
    	q.push((node){x , 0});
    	v[x] = 1;
    	
    	while(!q.empty()){
    		node top = q.front();
    		q.pop();
    		if(top.pos == k){
    			cout << top.step;
    			exit(0);
    		}
    		
    		if(top.pos + 1 <= 200000 && !v[top.pos + 1]){
    			v[top.pos + 1] = 1;
    			q.push((node){top.pos + 1 , top.step + 1});
    		}
    		if(top.pos - 1 >= 0 && !v[top.pos - 1]){
    			v[top.pos - 1] = 1;
    			q.push((node){top.pos - 1 , top.step + 1});
    		}
    		if(top.pos * 2 <= 200000 && !v[top.pos * 2]){
    			v[top.pos * 2] = 1;
    			q.push((node){top.pos * 2 , top.step + 1});
    		}
    	}
    }
    int main() {	
    	cin >> n >> k;
    	bfs(n);
        return 0;
    }
    
    

    信息

    ID
    1345
    时间
    1500ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    483
    已通过
    127
    上传者