4 条题解

  • 0
    @ 2025-3-9 20:23:19
    #include <bitset>
    #include <cctype>
    #include <cerrno>
    #include <clocale>
    #include <cmath>
    #include <complex>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <ctime>
    #include <cwchar>
    #include <cwctype>
    #include <complex.h>
    #include <deque>
    #include <exception>
    #include <fstream>
    #include <functional>
    #include <fenv.h>
    #include <iomanip>
    #include <ios>
    #include <iosfwd>
    #include <iostream>
    #include <istream>
    #include <inttypes.h>
    #include <limits>
    #include <list>
    #include <map>
    #include <ostream>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdexcept>
    #include <streambuf>
    #include <string>
    #include <stdbool.h>
    #include <stdint.h>
    #include <tgmath.h>
    #include <utility>
    #include <vector>
    
    using namespace std;
    
    #define LL long long
    const int N = 1e5 + 10;
    const int INF = 0x3f3f3f3f;
    
    struct node{
    	int pos , step;
    };
    
    int n , l , r , a[N]; 
    bool v[N];
    
    void bfs(int x){
    	queue<node> q;
    	q.push((node){x , 0});
    	v[x] = 1;
    	
    	while(!q.empty()){
    		node top = q.front();
    		q.pop();
    		
    		if(top.pos == r){
    			cout << top.step;
    			exit(0);
    		}
    		
    		if(top.pos + a[top.pos] <= n && !v[top.pos + a[top.pos]]){
    			v[top.pos + a[top.pos]] = 1;
    			q.push((node){top.pos + a[top.pos] , top.step + 1});
    		}
    		if(top.pos - a[top.pos] >= 1 && !v[top.pos - a[top.pos]]){
    			v[top.pos - a[top.pos]] = 1;
    			q.push((node){top.pos - a[top.pos] , top.step + 1});
    		} 
    	}
    }
    int main() {	
    	cin >> n >> l >> r;
    	for(int i = 1;i <= n;i++){
    		cin >> a[i];
    	}
    	bfs(l);
    	cout << -1;
        return 0;
    }
    
    
    • 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
        @ 2025-7-19 11:36:49
        #include <bits/stdc++.h>
        #define LL long long
        using namespace std;
        const int N = 200 + 10;
        const int INF = 0x3f3f3f3f;
        int n , k , b ,x;
        int a [N];
        bool v [N];
        struct node 
        {
        	int t , step;
        };
        queue < node > q;
        void bfs ()
        {
        	q.push( (node) {k , 0});
        	v [k] = 1;
        	while (!q.empty())
        	{
        		node top = q.front();
        		if ( top.t == b )
        		{
        			cout << top.step;
        			exit (0);
        		}
                q.pop();
        		if ( !v [top.t - a [top.t]]  && top.t - a [top.t] >= 1)
        		{
        			v [top.t - a [top.t]] = 1;
        			q.push ( (node) { top.t - a [top.t] , top.step + 1 });
        		}
        		    
        		if ( !v [top.t + a [top.t]]  && top.t + a [top.t] <= n)
        		{
        			v [top.t + a [top.t]] = 1;
        			q.push ( (node) { top.t + a [top.t] , top.step + 1 });
        		}
        	}
        	cout << -1;
        }
        int main()
        {
        	    cin >> n >> k >> x;
        	    for (int i = 1; i <= n; i++)
        	        cin >> a [i];
          		return 0;
        }
        
      • -1
        @ 2025-3-9 18:40:19

        #include<bits/stdc++.h> using namespace std; const int N=200+10; const int INF=0x3f3f3f3f; struct node { int pos,step; }; int n,l,r,a[N]; bool v[N]; void bfs(int x) { queue q; q.push((node){x,0}); v[x]=1; while(!q.empty()){ node top=q.front(); q.pop(); if(top.pos==r){ cout<<top.step; exit(0); } if(top.pos+a[top.pos]<=n&&v[top.pos+a[top.pos]]==0){ v[top.pos+a[top.pos]]=1; q.push((node){top.pos+a[top.pos],top.step+1}); } if(top.pos-a[top.pos]>0&&v[top.pos-a[top.pos]]==0){ v[top.pos-a[top.pos]]=1; q.push((node){top.pos-a[top.pos],top.step+1}); } }

        } int main(){ cin>>n>>l>>r; for(int i=1;i<=n;i++){ cin>>a[i]; }

        bfs(l);
        cout<<-1;
        return 0;
        

        }

        • 1

        信息

        ID
        3064
        时间
        1000ms
        内存
        256MiB
        难度
        6
        标签
        递交数
        290
        已通过
        94
        上传者