#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int c,b;
int n;
void print_anwser();
typedef struct llist{
int number;
struct llist* prev;
struct llist* next;
} list;
typedef struct lnode{
int top;
int first;
}node;
void init();
node* newnode(int k);
list* newlist(int k);
list* slist[30];
node* snode[30];
node* search(int a){
int i;
for(i=0;i<n;i++){
if(snode[i]->first==-1)continue;
list* l1=slist[snode[i]->first];
while(l1!=NULL){
if(l1->number==a)return snode[i];
else l1=l1->next;
}
}
return snode[i];
}
void rmove(int a){
list* l2=slist[a]->next;
while(l2!=NULL){
slist[a]->next=NULL;
list* l4=l2->next;
node* n2=snode[l2->number];
if(n2->top==-1){
n2->top=l2->number;
n2->first=l2->number;
l2->next=NULL;
l2=l4;
}
else{
list* l3=slist[n2->top];
l3->next=l2;
l2->prev=l3;
n2->top=l2->number;
l2=l4;
}
}
}
void moveonto(int a,int b){
node* na=search(a);
node* nb=search(b);
list* la=slist[a];
list* lb=slist[b];
if(na->first==na->top){
na->first=na->top=-1;
nb->top=la->number;
lb->next=la;
la->prev=lb;
}
else{
list* l1=slist[a]->prev;
l1->next=NULL;
na->top=l1->number;
nb->top=la->number;
lb->next=la;
la->prev=lb;
}
}
void moveover(int a,int b){
node* nb=search(b);
list* lc=slist[nb->top];
int t=lc->number;
moveonto(a,t);
}
void pileonto(int a,int b){
rmove(b);
node* na=search(a);
node* nb=search(b);
list* la=slist[a];
list* lb=slist[b];
if(na->first==a){
na->first=na->top=-1;
nb->top=a;
lb->next=la;
la->prev=lb;
}
else{
list* l1=slist[a]->prev;
l1->next=NULL;
nb->top=na->top;
na->top=l1->number;
lb->next=la;
la->prev=lb;
}
}
void pileover(int a,int b){
node* na=search(a);
node* nb=search(b);
list* la=slist[a];
list* c=slist[nb->top];
if(na->first==a){
nb->top=na->top;
na->first=na->top=-1;
c->next=la;
la->prev=c;
}
else{
list* l1=slist[a]->prev;
l1->next=NULL;
nb->top=na->top;
na->top=l1->number;
c->next=la;
la->prev=c;
}
}
int do1(){
if(search(c)==search(b))return 0;
rmove(b);
rmove(c);
moveonto(b,c);
return 1;
}
int do2(){
if(search(c)==search(b))return 0;
rmove(b);
moveover(b,c);
return 1;
}
int do3(){
if(search(c)==search(b))return 0;
pileonto(b,c);
return 1;
}
int do4(){
if(search(c)==search(b))return 0;
pileover(b,c);
return 1;
}
void init(int n){
int i;
for(i=0;i<n;i++){
snode[i]=newnode(i);
slist[i]=newlist(i);
snode[i]->top=i;snode[i]->first=i;
}
}
node* newnode(int k){
node* u=(node*)malloc(sizeof(node));
return u;
}
list* newlist(int k){
list* u=(list*)malloc(sizeof(list));
u->prev=NULL;
u->next=NULL;
u->number=k;
return u;
}
void print_anwser(){
int i;
for(i=0;i<n;i++){
printf("%d: ",i);
node* na=snode[i];
if(na->first!=-1){
list* l=slist[na->first];
printf("%d ",l->number);
list* l2=l->next;
while(l2!=NULL){
printf("%d ",l2->number);
l2=l2->next;
}
}
printf("\n");
}
}
int main(){
char s1[30],s2[30];
scanf("%d",&n);
init(n);
while(1){
scanf("%s",s1);if(!strcmp(s1,"quit"))break;
scanf("%d%s%d",&b,s2,&c);
if(b==c)continue;
if(!strcmp(s1, "move")&&!strcmp(s2,"onto")){do1();}
else if(!strcmp(s1, "move")&&!strcmp(s2,"over")){do2();}
else if(!strcmp(s1, "pile")&&!strcmp(s2,"onto")){do3();}
else if(!strcmp(s1, "pile")&&!strcmp(s2,"over")){do4();}
}
print_anwser();
return 0;
}
待修改
uva 101
最新推荐文章于 2022-04-17 12:29:49 发布