奇怪的电梯C++

#include<iostream>

using namespace std;

const int MAXN = 200 + 5;

int N, A, B;

bool vis[MAXN]; // 标记数组

int updown[] = { 0, 1, -1}; //方向数组 (上、下)

int floor[MAXN]; //每层楼上可以移动的数字

struct flow

{

int K; // 所在楼层可以移动的数字

int f; // 所在楼层

int step; // 已走步数

};

struct flow queue[MAXN];

void bfs(int a, int b) // 开搜 !

{

int head = 0, tail = 1, t; // 头, 尾, 移动后所在的楼层

queue[1].f = a; // 以下为初始化

queue[1].step = 0;

queue[1].K = floor[a];

vis[a] = true;

while(head < tail)

{

head++;

for(int i = 1; i <= 2; i++) // 枚举 上、下 两个方向

{

t = queue[head].f + updown[i] * queue[head].K; // 楼层移动

if(t >= 1 && t <= N && vis[t] == false)

{

tail++;

vis[t] = true;

queue[tail].K = floor[t];

queue[tail].f = t;

queue[tail].step = queue[head].step + 1;

if(t == b)

{

cout << queue[tail].step << endl;

return;

}

}

}

}

return;

}

int main()

{

cin >> N >> A >> B;

for(int i = 1; i <= N; i++)

{

cin >> floor[i];

}

bfs(A, B);

return 0;

}

这道题样例能过,但是只得了60分,不知道咋回事,求教,谢谢!

回答

你的我研究下

答案代码:

#include <bits/stdc++.h>

using namespace std;

struct node

{

int now;

int cnt;

}q,p;

int vis[400010],n,x,y,a[200000];

int main()

{

queue<node>line;

cin>>n>>x>>y;

for (int i = 1; i <= n; i++)

cin>>a[i];

memset(vis,0,sizeof(vis));

p.now = x;

p.cnt = 0;

// p.step = 1;

line.push(p);

vis[p.now] = 1;

while (!line.empty())

{

p = line.front();

if (p.now == y)

{

cout<<p.cnt;

return 0;

}

p.cnt++;

if (p.now < y)

{

q = p;

q.now += a[q.now];

if (vis[q.now] == 0)

{

line.push(q);

vis[q.now] = 1;

}

}

if (p.now - a[p.now] >= 1)

{

q = p; //给工具赋值

q.now -= a[q.now]; //进行操作

if (vis[q.now] == 0) //操作的意义和上面的一样

{

line.push(q);

vis[q.now] = 1;

}

}

line.pop();

}

cout<<-1;

return 0;

}

以上是 奇怪的电梯C++ 的全部内容, 来源链接: utcz.com/a/46193.html

回到顶部