大家好,又见面了,我是全栈君。
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11341 Accepted Submission(s): 4289
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button “UP” or “DOWN”?
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,….kn.
A single 0 indicate the end of the input.
5 1 5 3 3 1 2 5 0
3
#include<iostream> #include<cstring> #include<queue> using namespace std; #define N 205 int v[N],c[N]; int n,a,b,t; struct node { int i,time; }; void bfs(int x) { node now,tmp; queue<node> q; now.i=x,now.time=0; memset(v,0,sizeof(v)); q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.i==b) { t=0; cout<<now.time<<endl; return ; } for(int k=0;k<2;k++) { if(k==0) tmp.i=now.i+c[now.i]; if(k==1) tmp.i=now.i-c[now.i]; if(tmp.i>0&&tmp.i<=200&&!v[tmp.i]) { v[tmp.i]=1; tmp.time=now.time + 1; q.push(tmp); } } } } int main() { while(cin>>n) { if(n==0) break; cin>>a>>b; for(int k=1;k<=n;k++) cin>>c[k]; t=1; bfs(a); if(t) cout<<-1<<endl; } return 0; }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115634.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...