建议降橙,顺便奉上我ACdfs的代码(谁能告诉我怎么发题解!!!)
查看原帖
建议降橙,顺便奉上我ACdfs的代码(谁能告诉我怎么发题解!!!)
1058091
Kingsley_qu楼主2024/5/13 13:38

1.代码如下

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

int vis[10005];
int k[10005];
int n, a, b;

void bfs()
{
	queue<int> q;
	q.push(a);
	vis[a] = 0;
	while (!q.empty())
	{
		int now = q.front();
		q.pop();
		if (now == b) return ;
		int nxt = now + k[now];
		if (1 <= nxt && nxt <= n && vis[nxt] == -1)
		{
			q.push(nxt);
			vis[nxt] = vis[now] + 1;
		}
		nxt = now - k[now];
		if (1 <= nxt && nxt <= n && vis[nxt] == -1)
		{
			q.push(nxt);
			vis[nxt] = vis[now] + 1;
		}
	}
}
int main()
{
	cin >> n >> a >> b;
	for(int i = 1; i <= n; i++) cin >> k[i];
	memset(vis, -1, sizeof vis);
	bfs();
	cout << vis[b];
	return 0;
}
2024/5/13 13:38
加载中...