文章
49
粉丝
90
获赞
9
访问
26.7k
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int dir[2] = { -1,1 };
int least(int a, int b, vector<int> v, int n, vector<bool> visited)
{
bool flag = 0;
int sum = 0;
queue<int> q;
visited[a] = true;
q.push(a);
while (!q.empty())
{
int size = q.size();//最关键的地方
while (size--)
{
int t = q.front();
q.pop();
for (int i = 0; i < 2; i++)
{
int cur = t + dir[i] * v[t];
if (v[t] == 0)
return -1;
if (cur == b)
{
flag = 1;
return sum + 1;
}
if (cur > 0 && cur <= n && !visited[cur])
{
visited[cur] = true;
q.push(cur);
}
}
}
sum++;
}
if (flag == 0)
return -1;
}
int main()
{
int n, a, b;
cin >> n >> a >> b;
vector<int> v(n + 1);
vector<bool>visited(n + 1, false);
for (int i = 1; i <= n; i++)
{
int t;
cin >> t;...
登录后发布评论
暂无评论,来抢沙发