`
java-mans
  • 浏览: 11429133 次
文章分类
社区版块
存档分类
最新评论

hdu1548

 
阅读更多
¾­µäµÄ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;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics