标签:des style color java os io strong for
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11649    Accepted Submission(s): 4419
 
 
 
5 1 5 3 3 1 2 5 0
 
3
 
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
int n,s,e,bushu;
int g[210];
bool vis[210];
struct node
{
	int l;
	int setp;
	bool friend operator <(node a,node b)
	{
		return a.setp>b.setp;
	}
};
void bfs()
{
	node a,b;
	a.l=s;
	a.setp=0;
	if(a.l==e)
	{
		bushu=a.setp;return;
	}
	vis[a.l]=true;
	priority_queue<node>q;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		b.l=a.l+g[a.l];
		b.setp=a.setp+1;
		if(!vis[b.l]&&b.l>0&&b.l<=n)
		{
			if(b.l==e)
			{
				bushu=b.setp;return;
			}
			vis[b.l]=true;
			q.push(b);
		}
		b.l=a.l-g[a.l];
		b.setp=a.setp+1;
		if(!vis[b.l]&&b.l>0&&b.l<=n)
		{
			if(b.l==e)
			{
				bushu=b.setp;return;
			}
			vis[b.l]=true;
			q.push(b);
		}
	}
}
int main()
{    
	while(cin>>n)
	{
		if(n==0)break;
		int i;
		memset(g,0,sizeof g);
		memset(vis,0,sizeof vis);
		cin>>s>>e;
		for(i=1;i<=n;i++)
			cin>>g[i];
		bushu=-1;
		bfs();
		printf("%d\n",bushu);
	}
	return 0;
}
/*
题意:电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层
,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键
*/
 
HDU1548A strange lift(搜索),布布扣,bubuko.com
标签:des style color java os io strong for
原文地址:http://blog.csdn.net/fljssj/article/details/38553117