1 条题解

  • 0
    @ 2024-7-22 21:49:47
    #include<bits/stdc++.h>
    using namespace std;
    int a[1005];
    bool  vis[205];
    int n,m,b,flag = -1;
    struct node {
    	int y, step;
    };
    queue<node> q;
    void bfs(int y){
    	vis[y] = 1;
    	q.push((node){y,0});
    	while(!q.empty()){
    		node front = q.front();
    		q.pop();
    		
    		if(front.y == b){
    			flag = front.step;
    			return;
    		}
    		int ny = front.y + a[front.y];
    		int nt = front.step + 1;
    		if(vis[ny] == 0 && ny > 0 && ny <= 200){
    			vis[ny] = 1;
    			q.push((node){ny,nt});
    		}
    		
    		ny = front.y - a[front.y];
    		if(vis[ny] == 0 && ny > 0 && ny <= 200){
    			vis[ny] = 1;
    			q.push((node){ny,nt});
    		}
    	}
    }
    int main(){
    	cin >> n >> m >> b;
    	for(int i = 1;i <= n;i++){
    		cin >> a[i];
    	}
    	bfs(m);
    	cout << flag;
    	
    }

    100 Accepted

    # 状态 耗时 内存占用
    #1 Accepted 1ms 256 KiB
    #2 Accepted 1ms 304 KiB
    #3 Accepted 0ms 384 KiB
    #4 Accepted 1ms 312 KiB
    #5 Accepted 0ms 384 KiB
    • 1

    信息

    ID
    3064
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    51
    已通过
    19
    上传者