46分bfs求助
查看原帖
46分bfs求助
1258100
wanttoac楼主2024/1/21 21:35
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define maxn 205
int elevator[maxn],n;
struct p {
	int i;
	int count;
};
int main() {
	int s,e;
	cin >> n>>s>>e;
	s--;
	e--;
	for (int i = 0; i < n; i++)cin >> elevator[i];
	queue<p>bfs;
	vector<int>vis;
	bfs.push({s,0});
	while (!bfs.empty()) {
		int ss = bfs.front().i;
		if (ss == e) {
			cout << bfs.front().count;
			break;
		}
		vis.push_back(ss);
		int l = ss - elevator[ss];
		int r = ss + elevator[ss];
		if (l >= 0 && find(vis.begin(), vis.end(), l) == vis.end())bfs.push({l,bfs.front().count+1});
		if (r < n && find(vis.begin(), vis.end(), r) == vis.end())bfs.push({r,bfs.front().count+1});
		bfs.pop();
	}
	return 0;
}
2024/1/21 21:35
加载中...