70分求助大佬
查看原帖
70分求助大佬
1124291
wangfeiwu楼主2024/1/28 18:52
#include<iostream>
#include<queue>
using namespace std;
struct node{
	int h;
	int step;
};
int mov[201];
int N,a,b;
bool vis[201];
bool isArv=false;
bool check(int y){
	if(y<1||y> b) return false;
	return true;
}
void bfs(node s){	
	std::queue<node >  q;
	q.push(s);
	vis[ s.h ] =true;
	while( !q.empty()){
		node u = q.front();
		q.pop();
		if(u.h==b)
		{
			printf("%d\n",u.step);
			isArv=true;
			break;
		}
		node v={u.h+mov[u.h],u.step + 1};
		if(!vis[v.h]&& check(v.h))
		{
			q.push( v );
			vis[v.h]=true;
		}
		node v2={u.h-mov[u.h],u.step + 1};
		if(!vis[v2.h]&& check(v2.h))
		{
			q.push( v2 ); 
			vis[v2.h]=true; 
		}
	}
}

int main()
{ 
	node a;
	a.step=0;
	cin>>N>>a.h>>b;
	for(int i=1;i<=N;i++)
	cin>>mov[i];
	bfs(a);
	if(!isArv) 
	cout<<-1;

    return 0;
}
2024/1/28 18:52
加载中...