洛谷 奇怪的电梯 STL 版队列

下面是程序

苟了很久,90分,发现特殊情况没有处理qwq;

什么特殊情况呢?

就是当起点楼层和终点楼层的层数相同的时候,我并没有处理,最后输出的是"-1",显然不符题意;

#include<bits/stdc++.h>
using namespace std;
const int cntrl[3]={1,-1};
int a[100005],f[100005],n,s,e;
bool ok;
struct node{
	int flr,stp;
	node(): flr(0),stp(0){}
	node(const int flr,const int stp):
	flr(flr),stp(stp){}
};
queue < node > q;
void bfs(int A,int B){
	if(A==B){
		ok=true;
		cout<<"0";
		return ;
	}
	q.push(node(A,0));
	a[A]=1;
	while(!q.empty()){
		node tmpnode=q.front();
		for(int i=0;i<=1;i++){
			int tmpflr=tmpnode.flr+f[tmpnode.flr]*cntrl[i];
			if(tmpflr>0 && !a[tmpflr]){
				q.push(node(tmpflr,tmpnode.stp+1));
				a[tmpflr]=1;
				if(tmpflr==B){
					node x=q.back();
					cout<<x.stp;
					ok=true;
					return;
				}
			}
		}
		q.pop();
	}
}
int main(){
	cin>>n>>s>>e;
	for(int i=1;i<=n;i++)
	    cin>>f[i];
	bfs(s,e);
	if(!ok)
	    cout<<-1;
	return 0;
} 

特别说一下:增量数组-1,1;因为这里只有两种走法,上或下;

广搜什么的增量数组蛮重要的;


其实数组做队列和STL做队列都一样.只是queue<int> q要引用一些简单的出队入队函数而已;

注意打点,<>中式typename;


用struct存储floor->(flr),step->(stp)数据,背背板子就好了.注意存入队列时一定要 打"结构体名字( , )";是一种比较直观的方式存储;


ps:推荐wans的文章( 奇怪的电梯 洛谷);不过她好像是用的数组队列;当然数据(比如到了哪里)跟我的不太一样;但她的应该显然比我的要清楚一点QWQ

Ta 的 http://blog.csdn.net/wans__/article/details/78690780;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值