题意:给出n个数,终点t
从第i点能够跳到i+a[i],问能否到达终点
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algorithm> 11 #define mod=1e9+7; 12 using namespace std; 13 14 typedef long long LL; 15 const int INF = 0x7fffffff; 16 const int maxn=50005; 17 int a[maxn],vis[maxn]; 18 int n,t; 19 int flag=0; 20 21 void dfs(int x){ 22 if(vis[x]) return; 23 vis[x]=1; 24 int xx=x+a[x]; 25 if(xx==t){ 26 flag=1; 27 return; 28 } 29 dfs(xx); 30 } 31 32 int main(){ 33 scanf("%d %d",&n,&t); 34 memset(vis,0,sizeof(vis)); 35 for(int i=1;i<n;i++) scanf("%d",&a[i]); 36 dfs(1); 37 if(flag) printf("YES\n"); 38 else printf("NO\n"); 39 return 0; 40 }
时间: 2024-10-25 05:04:04