BFS的题目用DFS嘎嘎上分

Papyrus在审判你  •  6个月前


#include<bits/stdc++.h>
using namespace std;
struct lift{
    int rise;
    int drop;
};
lift a[210];
int n,x,y;
int tk=1;
int visited[210]={0};
void bfs(int h,int m){
    if(h==y){
        tk=0;
        cout<<m;
        return;
    }
    visited[h]=1;
    if(a[h].rise>=1&&a[h].rise<=n&&!visited[a[h].rise]){
        bfs(a[h].rise,m+1);
    }
    if(a[h].drop>=1&&a[h].drop<=n&&!visited[a[h].drop]){
        bfs(a[h].drop,m+1);
    }
    else{
        return;
    }
}
int main(){
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++){
        int t;
        cin>>t;
        a[i].rise=i+t;
        a[i].drop=i-t;
    }
    bfs(x,0);
    if(tk){
        cout<<"-1";
    }
    return 0;
}

评论:

请先登录,才能进行评论