hdu1548

来源:互联网 发布:ubuntu浏览器无法上网 编辑:程序博客网 时间:2024/05/16 05:53
¾­µäµÄbfs:#include<cstdio>#include<iostream>#include<queue>#include<string>using namespace std;struct step{int m,st;};int n,map[202];int f[202];int s,e;int bfs(){queue<step>Q;step p;p.m=s;p.st=0;Q.push(p);memset(f,0,sizeof(f));while(!Q.empty()){step temp=Q.front();Q.pop();step next;next.st=temp.st+1;next.m=temp.m+map[temp.m];if(next.m>0&&next.m<=n&&f[next.m]==0){if(next.m==e)return next.st;Q.push(next);f[temp.m]=1;}next.m=temp.m-map[temp.m];if(next.m>0&&next.m<=n&&f[next.m]==0){if(next.m==e)return next.st;Q.push(next);f[temp.m]=1;}}return -1;}int main(){while(scanf("%d",&n),n){scanf("%d%d",&s,&e);for(int i=1;i<=n;i++)scanf("%d",&map[i]);if(s==e)printf("0\n");elseprintf("%d\n",bfs());}return 0;}

原创粉丝点击