2014-06-15 17:38:11
题意&思路:操作描述上并不复杂的表/栈模拟题,但是写起来一定要注意细节!(被逻辑混乱WA了好几次,。TaT!),最后把代码逻辑优化了下,恩恩。
1 #include <cstring> 2 #include <iostream> 3 using namespace std; 4 5 struct blocks{ 6 int num,pos; 7 blocks *higher,*lower; 8 }; 9 blocks b[30]; 10 void Pop_top(blocks *a){ 11 blocks *p = a; 12 blocks *q = a->higher;//build mapping 13 p->higher = NULL;//cut p's two relations 14 while(q != NULL){//cut relations of blocks,which are on the p 15 q->pos = q->num; 16 p = q; 17 q = q->higher; 18 p->higher = p->lower = NULL; 19 } 20 } 21 void Pile_pos(blocks *a){ 22 blocks *q = a->higher; 23 while(q != NULL){ 24 q->pos = a->pos; 25 q = q->higher; 26 } 27 } 28 int main(){ 29 int n,n1,n2; 30 char s1[10],s2[10]; 31 blocks *p,*q; 32 while(cin >> n){ 33 memset(b,0,sizeof(b)); 34 for(int i = 0; i < n; ++i) 35 b[i].num = b[i].pos = i; 36 while(cin >> s1){ 37 if(s1[0] == 'q') 38 break; 39 cin >> n1 >> s2 >> n2; 40 p = &b[n1]; 41 q = &b[n2]; 42 if(n1 == n2 || p->pos == q->pos) 43 continue; 44 if(p->lower != NULL) 45 p->lower->higher = NULL; 46 if(s2[1] == 'n') 47 Pop_top(q); 48 else 49 while(q->higher != NULL) 50 q = q->higher; 51 if(s1[0] == 'm') 52 Pop_top(p); 53 p->lower = q; 54 q->higher = p; 55 p->pos = q->pos; 56 if(s1[0] == 'p') 57 Pile_pos(p); 58 } 59 for(int i = 0; i < n; ++i){ 60 cout<< i << ":"; 61 if(b[i].pos == i){ 62 p = &b[i]; 63 while(p != NULL){ 64 cout << " "<<p->num; 65 p = p->higher; 66 } 67 } 68 cout << endl; 69 } 70 } 71 return 0; 72 }