传送门:https://nanti.jisuanke.com/t/18
方法:DFS
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int length[510]; 5 int flag[510]; 6 int n; 7 8 bool dfs(int now) 9 { 10 if (now == n - 1) return true; 11 if (flag[now]) return false; 12 for (int i = length[now]; i > 0; --i) 13 if (dfs(now + i)) 14 return true; 15 flag[now] = 1;/*第一次忘记标记->然后一组数据超时*/ 16 return false; 17 } 18 int main() 19 { 20 while (cin >> n) { 21 fill(flag, flag + n, 0); 22 for (int i = 0; i < n; ++i) cin >> length[i]; 23 cout << (dfs(0) ? "true" : "false") << endl; 24 } 25 return 0; 26 }