HDU 1548 A strange lift 題解
阿新 • • 發佈:2018-11-16
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33697 Accepted Submission(s): 12038
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.
1//Author:LanceYu 2 #include<iostream> 3 #include<string> 4 #include<cstring> 5 #include<cstdio> 6 #include<fstream> 7 #include<iosfwd> 8 #include<sstream> 9 #include<fstream> 10 #include<cwchar> 11 #include<iomanip> 12 #include<ostream> 13 #include<vector> 14 #include<cstdlib> 15 #include<queue> 16 #include<set> 17 #include<ctime> 18 #include<algorithm> 19 #include<complex> 20 #include<cmath> 21 #include<valarray> 22 #include<bitset> 23 #include<iterator> 24 #define ll long long 25 using namespace std; 26 const double clf=1e-8; 27 //const double e=2.718281828; 28 const double PI=3.141592653589793; 29 const int MMAX=2147483647; 30 //priority_queue<int>p; 31 //priority_queue<int,vector<int>,greater<int> >pq; 32 struct node 33 { 34 int x,step; 35 }; 36 int n,start,last; 37 int dir[2][201],vis[201];//dir分為兩個方向,向上和向下 38 int bfs(int start,int last) 39 { 40 int i; 41 queue<node> q; 42 q.push(node{start,0}); 43 vis[start]=1; 44 while(!q.empty()) 45 { 46 node t=q.front(); 47 q.pop(); 48 if(t.x==last) 49 return t.step; 50 for(int i=0;i<2;i++) 51 { 52 int dx=t.x+dir[i][t.x]; 53 if(dx>=0&&dx<n&&!vis[dx])//一維深度搜索 54 { 55 vis[dx]=1; 56 q.push(node{dx,t.step+1}); 57 } 58 } 59 } 60 return -1; 61 } 62 int main() 63 { 64 int temp; 65 while(scanf("%d",&n)!=EOF)//這裡輸入要分開,因為輸入為0時停止 66 { 67 if(n==0) 68 return 0; 69 scanf("%d%d",&start,&last); 70 memset(vis,0,sizeof(vis)); 71 for(int i=0;i<n;i++)//每一層的資料錄入,包括兩個方向 72 { 73 scanf("%d",&temp); 74 dir[0][i]=temp; 75 dir[1][i]=-temp; 76 } 77 int ans=bfs(start-1,last-1);//注意此處要-1,受到陣列的限制和影響 78 printf("%d\n",ans); 79 } 80 return 0; 81 }
2018-11-16 01:05:38 Author:LanceYu