uva 101 The Blocks Problem

    1. 1.     move a onto b
      在將a搬到b上之前,先將ab上的積木放回原來的位置(例如:1就放回1的最開始位罝)
      2. move a over b
      在將a搬到b所在的那堆積木之上之前,先將a上的積木放回原來的位罝(b所在的那堆積木不動)
      3. pile a onto b
      a本身和其上的積木一起放到b上,在搬之前b上方的積木放回原位
      4. pile a over b
      a本身和其上的積木一起搬到到b所在的那堆積木之上

 

//用链表实现,没有任何算法可言,完全就是模拟,感觉用数组来做代码能更短

//由于用链表来做的原因,涉及到指针所以很多细节问题,实际上调试很久,指针的东西太久没写都忘记

//了,以后要适当复习

//时间0.012

 

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN1 sizeof(struct list)
#define LEN2 sizeof(struct node)
#define MAX 30  //最多有25个积木
struct list
{
    int num;
    struct list *prior,*next;
    struct node *head;
}*L;
struct node
{
    int num;
    struct node *next;
};
int n,a,b,ans[30];  //ans是在输出答案时保存答案的

void print_link()
{
    struct list *p=L;  struct node *q;
    while(p)
    {
        printf("%d\n",p->num); q=p->head;
        while(q) {printf("%3d",q->num); q=q->next;} printf("\n");
        p=p->next;
    }
}

void create_link()
{
    struct list *l1,*l2;  int i;
    L=l1=(struct list*)malloc(LEN1); L->prior=L->next=NULL; L->num=0;
    L->head=(struct node*)malloc(LEN2);  L->head->next=NULL;  L->head->num=0;
    for(i=1; i<n; i++)
    {
        l2=(struct list*)malloc(LEN1); l2->prior=l1; l1->next=l2; l2->next=NULL; l2->num=i; l1=l2;
        l2->head=(struct node*)malloc(LEN2); l2->head->next=NULL; l2->head->num=i;
    }
}

void move(struct list *s)
{
    struct node *p=s->head;  struct list *l;
    while(p && p->num!=a && p->num!=b)
    {
        p=s->head; l=L; 
        while(l->num!=p->num) 
            l=l->next;
        s->head=p->next; 
        p->next=l->head; 
        l->head=p; 
        p=s->head;
    }
}

//在收到一条指令后要先判断这条指令是否合法,非法的指令是指a和b在同一堆积木中
void command_1()  //move a onto b 在將a搬到b上之前,先將a和b上的積木放回原來的位置(例如:1就放回1的最開始位罝)
{
    struct list *l1=L,*l2; struct node *h1;  int count;
    while(l1)
    {
        h1=l1->head;  count=0;
        while(h1)  
        { if(h1->num==a || h1->num==b)  count++; h1=h1->next; }
        if(count==1)  { move(l1); /*printf("move is ok ");*/ }
        else if(count==2) return ;
        l1=l1->next;
    }
//    printf("\nmove is end\n");
    l1=L; 
    while(l1) 
    {
        if((l1->head) && (l1->head->num)==a)  break;
        l1=l1->next;
    }
    l2=L; 
    while(l2) 
    {
        if((l2->head) && (l2->head->num)==b)  break;
        l2=l2->next;
    }
    h1=l1->head; 
    l1->head=h1->next;
    h1->next=l2->head; 
    l2->head=h1;
//    printf("command_1 is end\n");
}
void command_2() //move a over b 在將a搬到b所在的那堆積木之上之前,先將a上的積木放回原來的位罝(b所在的那堆積木不動)
{
    struct list *l1=L,*l2; struct node *h1;  int flag,count;
    while(l1)
    {
        h1=l1->head;  count=flag=0;
        while(h1)  
        { 
            if(h1->num==a)  {flag=1;count++;}
            if(h1->num==b)  count++;
            h1=h1->next; 
        }
        if(count==1 && flag)  { move(l1); /*printf("move is ok ");*/ break;}
        else if(count==2) return ;
        l1=l1->next;
    }
//    printf("\nmove is end\n");
//    l1=L; while(l1->head->num!=a) l1=l1->next;
    l2=L;
    while(l2)
    {
        flag=0; h1=l2->head; while(h1)  {if(h1->num==b) {flag=1; break;} h1=h1->next;}
        if(flag) break;
        l2=l2->next;
    }
    h1=l1->head; 
    l1->head=h1->next;
    h1->next=l2->head; 
    l2->head=h1;
//    printf("command_2 is end\n");
}
void command_3()  //pile a onto b 將a本身和其上的積木一起放到b上,在搬之前b上方的積木放回原位
{
    struct list *l1,*l2; struct node *h1,*h2;  int flag,count;
    l1=L;
    while(l1)
    {
        h1=l1->head;  count=flag=0;
        while(h1)  
        { 
            if(h1->num==b)  {flag=1;count++;}
            if(h1->num==a)  count++;
            h1=h1->next; 
        }
        if(count==1 && flag)  { move(l1); /*printf("move is ok ");*/ break;}
        else if(count==2) return ;
        l1=l1->next;
    }
//    printf("\nmove is over\n");
//    l1=L; while(l1->head->num!=a) l1=l1->next;
    l2=L;
    while(l2)
    {
        flag=0; h1=l2->head; 
        while(h1)  {    if(h1->num==a) {flag=1; break;}   h1=h1->next;   }
        if(flag) break;
        l2=l2->next;
    }
    h2=h1->next; 
    h1->next=l1->head; 
    l1->head=l2->head; 
    l2->head=h2;
//    printf("command_3 is end\n");
}
void command_4() //pile a over b 將a本身和其上的積木一起搬到到b所在的那堆積木之上
{
    struct list *l1,*l2; struct node *h1,*h2,*temp1,*temp2;  int flag;
    l1=L;
    while(l1)
    {
        flag=0; h1=l1->head; while(h1) {if(h1->num==a) {flag=1; break;} h1=h1->next;}
        if(flag) break;
        l1=l1->next;
    }
    l2=L;
    while(l2)
    {
        flag=0; h2=l2->head; while(h2) {if(h2->num==b) {flag=1; break;} h2=h2->next;}
        if(flag) break;
        l2=l2->next;
    }
    if(l1==l2)  return ;
    temp1=h1->next; 
    h1->next=l2->head;  
    l2->head=l1->head;  
    l1->head=temp1;
//    printf("command_4 is end\n");
}
void print_answer()
{
    struct list *p=L;  struct node *q;  int i;
    while(p)
    {
        printf("%d:",p->num); q=p->head; i=-1;
        while(q) { ans[++i]=q->num; q=q->next; }
        while(i>=0) printf(" %d",ans[i--]);  printf("\n");
        p=p->next;
    }
}
int main()
{
    int i;  char s1[20],s2[20];
    scanf("%d",&n); create_link();  //print_link();
    while(1)
    {
        scanf("%s",s1);  if(!strcmp(s1,"quit"))  break;
        scanf("%d%s%d",&a,s2,&b);  //printf("%s %d %s %d\n",s1,a,s2,b);
        if(a==b) continue;
        if     ( !strcmp(s1,"move") && !strcmp(s2,"onto") )  
        { /*printf("command_1\n");*/command_1(); }
        else if( !strcmp(s1,"move") && !strcmp(s2,"over") )  
        { /*printf("command_2\n");*/command_2(); }
        else if( !strcmp(s1,"pile") && !strcmp(s2,"onto") )  
        { /*printf("command_3\n");*/command_3(); }
        else if( !strcmp(s1,"pile") && !strcmp(s2,"over") )  
        { /*printf("command_4\n");*/command_4(); }
//        print_answer();
    }
    print_answer();
    return 0;
}

 

 

 

posted @ 2012-10-05 22:16  Titanium  阅读(198)  评论(0编辑  收藏  举报