Description
Input
Output
题目大意:模拟一台电梯的运作,细节不多说了。要注意的是:如果电梯在闲置状态,电梯所在层同时有人上有人下,就优先上,这个题目说得不太好(还是我英语问题呢……);电梯会一直走同一个方向直到没有人须要电梯往那个方向走了;至于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 }