6 条题解

  • 1
    @ 2025-10-5 15:17:13
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 10;
    int n, k;
    bool vis[N];
    struct node{
    	int pos, step;
    };
    void bfs(int x) {
    	queue <node> q;
    	q.push((node){x, 0});
    	vis[x] = 1;
    	while (!q.empty()) {
    		node top = q.front();
    		q.pop();
    		if (top.pos == k) {
    			cout << top.step;
    			exit(0);
    		}
    		if (top.pos + 1 <= 2e5 && vis[top.pos + 1] == 0) {
    			vis[top.pos + 1] = 1;
    			q.push((node){top.pos + 1, top.step + 1});	
    		}
    		if (top.pos - 1 >= 1 && vis[top.pos - 1] == 0) {
    			vis[top.pos - 1] = 1;
    			q.push((node){top.pos - 1, top.step + 1});	
    		}
    		if (top.pos * 2 <= 2e5 && vis[top.pos * 2] == 0) {
    			vis[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
    标签
    递交数
    593
    已通过
    149
    上传者