HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)

Description

Too worrying about the house price bubble, poor Mike sold his house and rent an apartment in a 50-floor building several months ago. This building has only one elevator because it is a so called “rotten tail building”. There are always a lot of people crowding at the gate of the elevator on every floor. Many people have to climb hundreds of steps in order to save time.  
After months of climbing, Mike feels that he can’t stand it any more. He wants to sue the building owner. In order to let the judge understand how terrible the situation is, he decides to write a program to simulate the running of the elevator in a day. You’d better let him copy one from you .  
At first, the elevator is at the status of “idle”. If the three conditions below are all satisfied at the same time, we say the elevator is at “idle” status:   1)  The elevator is stopped.   2)  Nobody outside is waiting for the elevator.   3)  There is nobody in the elevator or all people in the elevator are just on their destination floor.  
There are an up button and a down button at the elevator gate on every floor except that only up button on the first floor, and only down button on the 50th floor. When someone wants to take the elevator, he pushes a button according to the direction he wants to go, and then wait. If the elevator is not moving towards his destination floor, he will not get in even the elevator comes and opens its door. When someone pushes a button, we say that he send a request to the elevator.  
When the elevator is idle and then some requests are sent to it, it will move towards the direction from which the first request is sent. If more than one request is sent at the same time, the requests sent form the same floor where the elevator stays have higher priority. In other cases, requests which will make the elevator go up, have higher priority than the same time requests which will make the elevator go down.  
Once the elevator starts moving, it keeps its moving direction until the three conditions below are all satisfied at the same time:   1)  All the people in the elevator have reached their destination floor.   2)  There is nobody waiting for the elevator at the elevator’s moving direction.   3)  Nobody on the floor where the elevator stays wants to go towards the elevator’s moving direction.   When the three conditions above are all satisfied at the same time, if there are requests from the direction opposite to the elevator’s last moving direction, the elevator will turn around and start moving; and if there are no requests at that time, the elevator will stay there and become idle.  
When the elevator reaches a certain floor, it will stop and open its door when one of the two conditions below is satisfied:   1) Someone inside the elevator wants to get off on that floor.   2) Someone on that floor wants to go towards the elevator’s moving direction.  
It takes one second for the elevator to move from one floor to another.   It takes one second for the elevator to open the door or close the door.   It takes one second for people outside the elevator to get in, no mater how many people.   It takes one second for people inside the elevator go get out, no mater how many people.  
The elevator can’t stop between two floors.  
 

Input

The first line is an integer T indicating the number of test cases. ( T <= 20)  
For each test case:  
The first line contains two integers: i and n. The elevator is on the i-th floor at first, and n is the total number of requests. ( 1 <= i <= 50, 1<=n<=100)   Then n lines follow. Each line contains three integers: t, s and d. It means that at the time of t-th second, a person on the s-th floor sends a request, and he wants to go to the d-th floor.  
 

Output

For each test case, print “Case N:” in a line at first. N is the test case number starting from 1.   Then, print the details of how the elevator runs. You should print information like:  

mm:ss The elevator starts to move (up|down) from floor x.   mm:ss The elevator stops at floor x.   mm:ss The elevator door is opening.   mm:ss x people leave the elevator.   mm:ss x people enter the elevator.   mm:ss The elevator door is closing.  
"mm:ss" means time, "mm" for minute, "ss" for second .   Please append a blank line to the end of the output of each test case.   It is guaranteed that the elevator will finish all requests within 3600 seconds。

 

题目大意:模拟一台电梯的运作,细节不多说了。要注意的是:如果电梯在闲置状态,电梯所在层同时有人上有人下,就优先上,这个题目说得不太好(还是我英语问题呢……);电梯会一直走同一个方向直到没有人须要电梯往那个方向走了;至于POJ的DISCUSS里面有人说有进出是同一层的情况,我测试了一下(if(from == to) tle();),是没有这种情况的……

思路:丧心病狂模拟题,打错一个字母就没有然后了(还好我是复制的)。每次时间+1都判断一下有没有新的人来坐电梯,慢慢搞总会AC的……

PS:本人第一条200+行的题(大概是)。做了几个小时,现场肯定不能做了,可能我做法有点挫就不写做法误导大家了(捂脸)。

 

代码(HDU 0MS/POJ 16MS):

  1 #include <cstdio>

  2 #include <algorithm>

  3 #include <iostream>

  4 #include <cstring>

  5 using namespace std;

  6 

  7 const int MAXN = 110;

  8 const int MAXT = 3610;

  9 

 10 struct Node {

 11     int t, from, to;

 12     void read() {

 13         scanf("%d%d%d", &t, &from, &to);

 14     }

 15     bool operator < (const Node &rhs) const {

 16         return t < rhs.t;

 17     }

 18 };

 19 

 20 Node a[MAXN];

 21 int leave[MAXN], sum_leave;//要在第几层离开

 22 int into[MAXN][2], sum_into;//要在第几层进来,0:down,1:up

 23 bool have_in[MAXN];//已进入电梯

 24 int T, n, rec_t, rec_a, rec_f, step, state;

 25 

 26 void inc_time() {

 27     ++rec_t;

 28     while(rec_a < n && a[rec_a].t <= rec_t) {//更新请求

 29         if(a[rec_a].from > a[rec_a].to) {

 30             ++into[a[rec_a].from][0];

 31         }

 32         else {

 33             ++into[a[rec_a].from][1];

 34         }

 35         ++sum_into;

 36         ++rec_a;

 37     }

 38 }

 39 

 40 void solve() {

 41     sort(a, a + n);

 42     step = 1;

 43     sum_leave = sum_into = 0;

 44     rec_a = 0;

 45     rec_t = -1; inc_time();

 46     memset(have_in, 0, sizeof(have_in));

 47     while(rec_a < n || sum_into || sum_leave) {

 48         switch(step) {

 49             case 1: { //空闲

 50                 if(sum_into == 0) {

 51                     inc_time();

 52                     continue;

 53                 }

 54                 if(into[rec_f][1]) {

 55                     step = 2;//开门

 56                     state = 1;//向上

 57                     continue;

 58                 }

 59                 if(into[rec_f][0]) {

 60                     step = 2;//开门

 61                     state = 0;//向下

 62                     continue;

 63                 }

 64                 for(int i = rec_f + 1; i <= 50; ++i)

 65                     if(into[i][0] || into[i][1]) {//上面有请求

 66                         step = 4;//向上

 67                         state = 1;

 68                         break;

 69                     }

 70                 if(step == 4) {

 71                     printf("%02d:%02d The elevator starts to move up from floor %d.\n", rec_t / 60, rec_t %60, rec_f);

 72                     continue;

 73                 }

 74                 for(int i = 0; i < rec_f; ++i)

 75                     if(into[i][0] || into[i][1]) {//下面有请求

 76                         step = 5;//向下

 77                         state = 0;

 78                         break;

 79                     }

 80                 if(step == 5) {

 81                     printf("%02d:%02d The elevator starts to move down from floor %d.\n", rec_t / 60, rec_t %60, rec_f);

 82                     continue;

 83                 }

 84                 break;

 85             }

 86             case 2: {//开门

 87                 printf("%02d:%02d The elevator door is opening.\n", rec_t / 60, rec_t %60);

 88                 inc_time();

 89                 step = 7;//离开

 90                 break;

 91             }

 92             case 3: {//关门

 93                 printf("%02d:%02d The elevator door is closing.\n", rec_t / 60, rec_t %60);

 94                 inc_time();

 95                 if(into[rec_f][state]) {//有人要进来

 96                     step = 2;

 97                     continue;

 98                 }

 99                 step = 8;

100                 break;

101             }

102             case 4: {//向上

103                 ++rec_f;

104                 inc_time();

105                 if(into[rec_f][1] || leave[rec_f]) {

106                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);

107                     step = 2;

108                     continue;

109                 }

110                 if(sum_leave) continue;

111                 bool flag = true;

112                 for(int i = rec_f + 1; i <= 50; ++i) {

113                     if(into[i][1] || into[i][0]) {

114                         flag = false;

115                         break;

116                     }

117                 }

118                 if(flag) {

119                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);

120                     state = 0, step = 2;

121                 }

122                 break;

123             }

124             case 5: {//向下

125                 --rec_f;

126                 inc_time();

127                 if(into[rec_f][0] || leave[rec_f]) {

128                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);

129                     step = 2;

130                     continue;

131                 }

132                 if(sum_leave) continue;

133                 bool flag = true;

134                 for(int i = 0; i < rec_f; ++i) {

135                     if(into[i][1] || into[i][0]) {

136                         flag = false;

137                         break;

138                     }

139                 }

140                 if(flag) {

141                     printf("%02d:%02d The elevator stops at floor %d.\n", rec_t / 60, rec_t %60, rec_f);

142                     state = 1, step = 2;

143                 }

144                 break;

145             }

146             case 6: {//进入

147                 if(into[rec_f][state]) {

148                     printf("%02d:%02d %d people enter the elevator.\n", rec_t / 60, rec_t %60, into[rec_f][state]);

149                     sum_into -= into[rec_f][state];

150                     into[rec_f][state] = 0;

151                     for(int i = 0; i < rec_a; ++i)

152                         if(a[i].from == rec_f && state == (a[i].from < a[i].to) && !have_in[i]) {

153                             have_in[i] = true;

154                             ++leave[a[i].to];

155                             ++sum_leave;

156                             //in_ele[i] = true;

157                         }

158                     inc_time();

159                 }

160                 if(!into[rec_f][state]) step = 3;//有人要进来就不关门

161                 break;

162             }

163             case 7: {//离开

164                 if(leave[rec_f]) {

165                     printf("%02d:%02d %d people leave the elevator.\n", rec_t / 60, rec_t %60, leave[rec_f]);

166                     sum_leave -= leave[rec_f];

167                     leave[rec_f] = 0;

168                     inc_time();

169                 }

170                 if(sum_leave == 0 && state == 1) {

171                     bool flag = true;

172                     for(int i = 0; i < rec_a; ++i) {

173                         if(!have_in[i] && a[i].from == rec_f && a[i].to > rec_f) {

174                             flag = false;

175                             break;

176                         }

177                         if(!have_in[i] && a[i].from > rec_f) {

178                             flag = false;

179                             break;

180                         }

181                     }

182                     if(flag) state = 0;

183                 }

184                 else if(sum_leave == 0 && state == 0) {

185                     bool flag = true;

186                     for(int i = 0; i < rec_a; ++i) {

187                         if(!have_in[i] && a[i].from == rec_f && a[i].to < rec_f) {

188                             flag = false;

189                             break;

190                         }

191                         if(!have_in[i] && a[i].from < rec_f) {

192                             flag = false;

193                             break;

194                         }

195                     }

196                     if(flag) state = 1;

197                 }

198                 step = 6;

199                 break;

200             }

201             case 8: {//判断关门后动作

202                 if(sum_into == 0 && sum_leave == 0) {//没人要进来没人在电梯里

203                     step = 1;

204                     continue;

205                 }

206                 if(sum_leave == 0) {//本层没人上,电梯没人,有请求

207                     if(state == 1) {//是否继续向上

208                         int i;

209                         for(i = rec_f + 1; i <= 50; ++i) {

210                             if(into[i][1] || into[i][0]) break;

211                         }

212                         if(i > 50) state = 0;

213                     }

214                     else {//是否继续向下

215                         int i;

216                         for(i = 1; i < rec_f; ++i) {

217                             if(into[i][1] || into[i][0]) break;

218                         }

219                         if(i == rec_f) state = 1;

220                     }

221                 }

222                 if(state) printf("%02d:%02d The elevator starts to move up from floor %d.\n", rec_t / 60, rec_t %60, rec_f);

223                 else printf("%02d:%02d The elevator starts to move down from floor %d.\n", rec_t / 60, rec_t %60, rec_f);

224                 if(state) step = 4;

225                 else step = 5;

226                 break;

227             }

228         }

229     }

230     printf("%02d:%02d The elevator door is closing.\n", rec_t / 60, rec_t %60);

231 }

232 

233 int main() {

234     scanf("%d", &T);

235     for(int t = 1; t <= T; ++t) {

236         scanf("%d%d", &rec_f, &n);

237         for(int i = 0; i < n; ++i) a[i].read();

238         printf("Case %d:\n", t);

239         solve();

240         puts("");

241     }

242 }
View Code

 

你可能感兴趣的:(2008)