1 条题解

  • 0
    @ 2024-7-22 21:33:35

    数组要开大,不然会RE
    #include<bits/stdc++.h> using namespace std; int a[10000005]; int n,k; struct node { int x, step; }; queue<node> q; void bfs(int x){ a[x] = 1; q.push((node){x,0}); while(!q.empty()){ node front = q.front(); q.pop(); if(front.x == k){ cout << front.step; return; } int nt = front.step + 1; int nx = front.x + 1; if(!a[nx] && nx >= 0 && nx <= 100000){ a[nx] = 1; q.push((node){nx, nt}); } nx = front.x - 1; if(!a[nx] && nx >= 0 && nx <= 100000){ a[nx] = 1; q.push((node){nx, nt}); } nx = front.x * 2; if(!a[nx] && nx >= 0 && nx <= 100000){ a[nx] = 1; q.push((node){nx, nt}); } } } int main(){ cin >> n >> k; bfs(n); }

    100 Accepted

    # 状态 耗时 内存占用
    #1 Accepted 0ms 260 KiB
    #2 Accepted 3ms 628 KiB
    #3 Accepted 0ms 256 KiB
    #4 Accepted 2ms 776 KiB
    #5 Accepted 2ms 460 KiB
    #6 Accepted 0ms 256 KiB
    #7 Accepted 0ms 256 KiB
    #8 Accepted 2ms 472 KiB
    #9 Accepted 3ms 668 KiB
    #10 Accepted 1ms 812 KiB
    #11 Accepted 1ms 636 KiB
    #12 Accepted 1ms 432 KiB
    #13 Accepted 3ms 524 KiB
    • 1

    信息

    ID
    2485
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    33
    已通过
    10
    上传者