chen711

· · 个人记录

T582141

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int vis[200010];
int dx[]={1,-1};
queue<int>q;
signed main()
{
    int n,m;
    cin>>n>>m;
    q.push(n);
    q.push(0);
    vis[n]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        int dis=q.front();
        q.pop();
        if(m==x){//找到了
            cout<<dis;
            return 0;
        }
        int xx=x+dx[0];//三种情况
        if(xx<=m&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
        xx=x+dx[1];
        if(xx>0&&vis[xx]==0){//没去过
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
        xx=x*2;
        if(x<=m&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
    }
    return 0;
}

T579124

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int vis[100010],a[100010];
int dx[]={1,-1};
queue<int>q;
signed main()
{
    int n,m;    int sx,sy,ex,ey;
    cin>>n;
    fr(i,1,n)
        cin>>a[i];
    q.push(1);
    q.push(0);
    vis[1]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        int dis=q.front();
        q.pop();
        fr(i,x+1,x+a[x]){
            if(vis[i]==0){
                q.push(i);
                q.push(dis+1);
                vis[i]=1;
                if(i>=n){
                    cout<<dis+1;//下一步才找到
                    return 0;
                }
            }//没必要往回走,肯定亏
        }
    }
    cout<<-1;//过不去
    return 0;
}

p1588

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int vis[200010];
int dx[]={1,-1};
queue<int>q;
signed main()
{
    int t;
    cin>>t;
    while(t--){
        memset(vis,0,sizeof(vis));
        while(!q.empty())q.pop();
        int n,m;
        cin>>n>>m;
        q.push(n);
    q.push(0);
    vis[n]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        int dis=q.front();
        q.pop();
        if(m==x){
            cout<<dis<<"\n";
            break;
        }
        int xx=x+dx[0];
        if(x<=m&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
        xx=x+dx[1];
        if(x>0&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
        xx=x*2;
        if(x<=m&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
    }
    } 

    return 0;
}

B3626

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int vis[200010];
int dx[]={1,-1};
queue<int>q;
signed main()
{
    int t;
    cin>>t;
    while(t--){
        memset(vis,0,sizeof(vis));
        while(!q.empty())q.pop();
        int n,m;
        cin>>n>>m;
        q.push(n);
    q.push(0);
    vis[n]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        int dis=q.front();
        q.pop();
        if(m==x){
            cout<<dis<<"\n";
            break;
        }
        int xx=x+dx[0];
        if(x<=m&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
        xx=x+dx[1];
        if(x>0&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
        xx=x*2;
        if(x<=m&&vis[xx]==0){
            q.push(xx);
            q.push(dis+1);
            vis[xx]=1;
        }
    }
    } 

    return 0;
}