Elevator Trouble
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 29, Accepted users: 29
Problem 12366 : No special judgement
Problem description
You are on your way to your rst job interview as a program tester, and you are already late. The interview is in a skyscraper and you are currently in floor s, where you see an elevator. Upon entering the elvator, you learn that it has only two buttons, marked "UP u" and "DOWN d". You conclude that the UP-button takes the elevator u floors up (if there aren't enough floors, pressing the UP-botton does nothing, or at least so you assume), whereas the DOWN-button takes you d stories down (or none if there aren't enough). Knowing that the interview is at floor g, and that there are only f floors in the building, you quickly decide to write a program that gives you the amount of button pushes you need to perform. If you simply cannot reach the correct floor, your program halts with the message "use the stairs".
Given input f, s, g, u and d (floors, start, goal, up, down), find the shortest sequence of button presses you must press in order to get from s to g, given a building of f floors, or output "use the stairs" if you cannot get from s to g by the given elevator.

Input
The input will consist of one line, namely f s g u d, where 1 <= s, g <=f <= 1000000 and 0 <= u, d <= 1000000. The oors are one-indexed, i.e. if there are 10 stories, s and g be in [1, 10].

Output
You must reply with the minimum numbers of pushes you must make in order to get from s to g, or output use the stairs if it is impossible given the con guration of the elvator

Sample Input
10 1 10 2 1
100 2 1 1 0
Sample Output
6
use the stairs
Problem Source
NCPC 2011

 

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 
 5 struct node
 6 {
 7     int floor;
 8     int times;
 9 };
10 queue<node> q;
11 int f,s,g,u,d,prove,visit[1000002];
12 
13 int check(int x)
14 {
15     if(x>=1&&x<=f)   return 1;
16     else return 0;
17 }
18 
19 void BFS()
20 {
21     while(!q.empty())
22         q.pop();
23         node st,e;
24         st.floor=s;
25         st.times=0;
26         q.push(st);
27         while(!q.empty())
28         {
29             st=q.front();
30             q.pop();
31             if(st.floor==g)
32             {
33                 cout<<st.times<<"\n";
34                 prove=1;
35                 break;
36             }
37             e.floor=st.floor+u;
38             e.times=st.times+1;
39             if(!visit[e.floor]&&check(e.floor))
40             {
41                 visit[e.floor]=1;
42                 q.push(e);
43             }
44             e.floor=st.floor-d;
45             e.times=st.times+1;
46             if(!visit[e.floor]&&check(e.floor))
47             {
48                 visit[e.floor]=1;
49                 q.push(e);
50             }
51         }
52 }
53 
54 int main()
55 {
56     while(cin>>f>>s>>g>>u>>d)
57     {
58         for(int i=1;i<=f;i++)
59         visit[i]=0;
60         prove=0;
61         BFS();
62         if(!prove)  cout<<"use the stairs"<<"\n";
63     }
64   return 0;
65 }
posted on 2012-07-17 22:26  黑色的铅笔  阅读(469)  评论(0编辑  收藏  举报