P1135 奇怪的电梯

在这里插入图片描述

代码:

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

int main()
{
	int visit[1000] = { 0 };
	int n, a, b;
	cin >>n >>  a >> b;
	int num[1000];
	for (int i = 1; i <= n; i++)//输入每层可以移动的层数
	{
		cin >> num[i];
	}
	queue<int>floor;//层次队列
	queue<int>count;//次数队列
	floor.push(a); count.push(0);//入栈第一个
	visit[a] = 1;//将当前层置为一访问
	while (!floor.empty())
	{
		if (floor.front() == b)//当层次相同了,出口
		{
			cout << count.front();
			return 0;
		}
		int t = floor.front() + num[floor.front()];//进入到可进入的下一层
		if (t <= n && visit[t] == 0)//如果当前层数符合要求,并且没有访问过
		{
			floor.push(t);
			count.push(count.front() + 1);
			visit[t] = 1;
		}
		 t = floor.front() - num[floor.front()];
		if (t >= 1 && visit[t] == 0)如果当前层数符合要求,并且没有访问过
		{
			floor.push(t);
			count.push(count.front() + 1);
			visit[t] = 1;
		}
		floor.pop();
		count.pop();
	}
	cout << -1;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值