¾µäµÄ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");
else
printf("%d\n",bfs());
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
HDU最全ac代码
hdu 1166线段树代码
hdu动态规划算法集锦
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu题目分类
自己做的HDU ACM已经AC的题目
HDU图论题目分类
hdu 1005.比较简单的一道题,有兴趣的可以看看。