您的位置:首页 > 其它

洛谷 奇怪的电梯 STL 版队列

2017-12-23 16:10 309 查看
下面是程序

苟了很久,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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs