1 条题解
-
0
C++ :
#include <iostream> using namespace std; #include <cstdio> #include <cstring> #include <cmath> int n,a,b,t; int ans=0x7f; int edge[201][201]; int f[201][201]; int main() { cin>>n>>a>>b; memset(edge,0x7f/2,sizeof(edge)); memset(f,0x7f/2,sizeof(f)); for (int i=1; i<=n; i++) { f[i][i]=0; cin>>t; if (i+t<=n) { edge[i][i+t]=1; f[i][i+t]=edge[i][i+t]; } if (i-t>=1) { edge[i][i-t]=1; f[i][i-t]=edge[i][i-t]; } } for (int k=1; k<=n; k++) for (int i=1; i<=n; i++) if (i!=k) for (int j=1; j<=n; j++) if (i!=j&&j!=k) { f[i][j]=min(f[i][j],f[i][k]+f[k][j]); } if (f[a][b]!=f[0][0]) cout<<f[a][b]; else cout<<-1; }
Python :
# coding=utf-8 import queue def work(): v=[False]*(n+1) if a==b: return 0 q=queue.Queue() q.put( (0,a) ) v[a]=True while not q.empty(): t=q.get() for i in (1,-1): new_a=t[1]+c[t[1]]*i if 0<new_a<=n and not v[new_a]: if new_a==b: return t[0]+1 q.put( (t[0]+1,new_a) ) v[new_a]=True return -1 n,a,b=map(int,input().split()) c=map(int,input().split()) c=[0]+list(c) print(work())
- 1
信息
- ID
- 890
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者