求助0pts!
查看原帖
求助0pts!
973480
封禁用户楼主2024/6/8 12:18
#include<bits/stdc++.h>
using namespace std;
int n,k,t;
struct node{
    int x;
    int step;
}q[100001];
int vis[100001];
void bfs(int sx,int ex){
    int head=1,tail=1;
    q[tail].x=sx,q[tail].step=0;
    tail++;
    vis[sx]=1;
    while(head<tail){
        int x0=q[head].x;
        int step=q[head].step;
        if(x0==ex){
            cout << step << endl;
            return;
        }
        for(int i=1;i<=3;i++){
            int nx;
            if(i==1) nx=x0+1;
            if(i==2) nx=x0-1;
            if(i==3) nx=x0*2;
            if(nx>=0&&nx<=100000&&!vis[nx]){
                q[tail].x=nx;
                q[tail].step=step+1;
                tail++;
                vis[nx]=1;
            }
        }
        head++;
    }
}
int main(){
    cin >> t;
    while(t--){
        cin >> n >> k;
        bfs(n,k);
    }
    return 0;
}
2024/6/8 12:18
加载中...