1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 struct Node 5 { 6 int content; //存放节点的数字,就是砖块的编号 7 struct Node *next; //指向堆在自己上面的砖块 8 int location; //记录自己在第几堆位置上 9 }; 10 typedef struct Node NODE; 11 void Clear(int obj,NODE* nodelist[],NODE* location[]) //用于清除上面的砖块,放回原位 12 { 13 NODE *temp1; 14 NODE *temp2; 15 temp1 = nodelist[obj]; 16 temp2 = temp1; 17 temp1 = temp1->next; 18 temp2->next = NULL; 19 while(temp1 != NULL) 20 { 21 int tag; 22 tag = temp1->content; 23 location[tag]->next = temp1; 24 temp2 = temp1; 25 temp1->location = tag; 26 temp1 = temp1->next; 27 temp2->next = NULL; 28 } 29 } 30 void Moveonto(int obj,int objonto,NODE* nodelist[],NODE* location[]) //moveonto 31 { 32 NODE* temp; 33 temp = location[nodelist[obj]->location]; 34 while(temp->next->content != obj) 35 { 36 temp = temp->next; 37 } 38 temp->next = NULL; 39 Clear(obj,nodelist,location); 40 Clear(objonto,nodelist,location); 41 nodelist[objonto]->next = nodelist[obj]; 42 nodelist[obj]->next = NULL; 43 nodelist[obj]->location = nodelist[objonto]->location; 44 45 } 46 void Moveover(int obj,int objover,NODE* nodelist[],NODE* location[]) 47 { 48 NODE* temp; 49 temp = location[nodelist[obj]->location]; 50 while(temp->next->content != obj) 51 { 52 temp = temp->next; 53 } 54 temp->next = NULL; 55 Clear(obj,nodelist,location); 56 temp = nodelist[objover]; 57 while(temp->next != NULL) 58 { 59 temp = temp->next; 60 } 61 temp->next = nodelist[obj]; 62 nodelist[obj]->location = nodelist[objover]->location; 63 } 64 void Pileonto(int obj,int objonto,NODE* nodelist[],NODE* location[]) 65 { 66 NODE* temp; 67 temp = location[nodelist[obj]->location]; 68 while(temp->next->content != obj) 69 { 70 temp = temp->next; 71 } 72 temp->next = NULL; 73 Clear(objonto,nodelist,location); 74 nodelist[objonto]->next = nodelist[obj]; 75 nodelist[obj]->location = nodelist[objonto]->location; 76 } 77 void Pileover(int obj,int objover,NODE* nodelist[],NODE* location[]) 78 { 79 NODE* temp; 80 temp = location[nodelist[obj]->location]; 81 while(temp->next->content != obj) 82 { 83 temp = temp->next; 84 } 85 temp->next = NULL; 86 temp = nodelist[objover]; 87 while(temp->next != NULL) 88 { 89 temp = temp->next; 90 } 91 temp->next = nodelist[obj]; 92 nodelist[obj]->location = nodelist[objover]->location; 93 } 94 void Print(NODE* location[],int size) 95 { 96 int i; 97 NODE* temp; 98 for(i = 0;i < size;i++) 99 { 100 printf("%d:",i); 101 temp = location[i]; 102 temp = location[i]->next; 103 while(temp != NULL) 104 { 105 printf(" %d",temp->content); 106 temp = temp->next; 107 } 108 printf(" "); 109 } 110 } 111 int main (void) 112 { 113 int amount; 114 char quit[5] = "quit"; 115 char move[5] = "move"; 116 char pile[5] = "pile"; 117 char onto[5] = "onto"; 118 char over[5] = "over"; 119 char string1[5]; 120 char string2[5]; 121 scanf("%d",&amount); 122 NODE *nodelist[25]; 123 NODE *location[10]; 124 int i; 125 for(i = 0;i < amount;i++) //每个位置设置个头结点 ,仅用来标记位置 126 { 127 location[i] = (NODE*)malloc(sizeof(NODE)); 128 location[i]->next = NULL; 129 } 130 for(i = 0;i < amount;i++) //砖块节点集 用于搜索到每个砖块节点 131 { 132 nodelist[i] =(NODE*)malloc(sizeof(NODE)); 133 nodelist[i]->content = i; 134 nodelist[i]->next = NULL; 135 } 136 for(i = 0;i < amount;i++) 137 { 138 location[i]->next = nodelist[i]; 139 nodelist[i]->location = i; 140 } 141 while(scanf("%s",&string1)&&strcmp(string1,quit))//不是quit就继续读 142 { 143 int lag; 144 int op1; 145 int op2; 146 scanf("%d %s %d",&op1,&string2,&op2); 147 if(op1 == op2|| nodelist[op1]->location == nodelist[op2]->location) 148 { 149 continue; 150 }else 151 { 152 if(!strcmp(string1,move)) 153 { 154 lag = 1; 155 }else 156 if(!strcmp(string1,pile)) 157 { 158 lag = 2; 159 } 160 switch (lag) //加深switch注意点 161 { 162 case 1: 163 if(!strcmp(string2,onto)) 164 { 165 Moveonto(op1,op2,nodelist,location); 166 }else 167 if(!strcmp(string2,over)) 168 { 169 Moveover(op1,op2,nodelist,location); 170 } 171 break; 172 case 2: 173 if(!strcmp(string2,onto)) 174 { 175 Pileonto(op1,op2,nodelist,location); 176 }else 177 if(!strcmp(string2,over)) 178 { 179 Pileover(op1,op2,nodelist,location); 180 } 181 break; 182 } 183 } 184 185 Print(location,amount); //这是每轮测试用的PRINT提交时删掉 186 //scanf("%s",&string1); //注意这是错误的 187 } 188 Print(location,amount); 189 190 return 0 ; 191 192 }
暂未AC,TLE查错中