您的位置:首页 > 其它

uva101 The Blocks Problem

2010-10-22 11:33 337 查看
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define LOCAL

#define MAXN 30

typedef struct node

{

    int data;

    struct node  *pre, *next;

}*Node;

Node link[MAXN] ;

int n;

void init();

Node search(int data, int *pos);

void freespace();

int main()

{

    char str1[5], str2[5];

    int src, dst;

    Node a, b, p, q;

    int flag;

    int i;

    int pos1, pos2;

    #ifdef LOCAL

        freopen("c://uva_in.txt", "r", stdin);

    #endif

    scanf("%d", &n);

    init();

    while (scanf("%s", str1) && (strcmp(str1, "quit") != 0))

    {

        scanf("%d%s%d", &src, str2, &dst);

        a = search(src, &pos1);

        b = search(dst, &pos2);

        if (strcmp(str1, "move") == 0 && strcmp(str2, "onto") == 0)

            flag = 1;

        else if (strcmp(str1, "move") == 0 && strcmp(str2, "over") == 0)

            flag = 2;

        else if (strcmp(str1, "pile") == 0 && strcmp(str2, "onto") == 0)

            flag = 3;

        else if (strcmp(str1, "pile") == 0 && strcmp(str2, "over") == 0)

            flag = 4;

        if (flag == 1 && pos1 != pos2)

        {

            p = a->next;

            a->next = NULL;

            while (p)

            {

                q = p->next;

                p->next = NULL;

                p->pre = link[p->data ];

                link[p->data ]->next = p;

                p = q;

            }

            p = b->next;

            b->next = NULL;

            while (p)

            {

                q = p->next;

                p->next = NULL;

                p->pre = link[p->data];

                link[p->data]->next = p;

                p = q;

            }

            a->pre->next = NULL;

            a->pre = b;

            b->next = a;

        } else if (flag == 2 && pos1 != pos2)

        {

            p = a->next;

            a->next = NULL;

            while (p)

            {

                q = p->next;

                p->next = NULL;

                p->pre = link[p->data];

                link[p->data]->next = p;

                p = q;

            }

            while (b->next)

                b = b->next;

            a->pre->next = NULL;

            a->pre = b;

            b->next = a;

        } else if (flag == 3 && pos1 != pos2)

        {

            p = b->next;

            b->next = NULL;

            while (p)

            {

                q = p->next;

                p->next = NULL;

                p->pre = link[p->data];

                link[p->data]->next = p;

                p = q;

            }

            a->pre->next = NULL;

            a->pre = b;

            b->next = a;

        } else if (flag == 4 && pos1 != pos2)

        {

            while (b->next)

                b = b->next;

            a->pre->next = NULL;

            a->pre = b;

            b->next = a;

        }

    }

    for (i = 0; i < n; i++)

    {

        p = link[i]->next;

        printf("%d:", i);

        while (p)

        {

            printf(" %d", p->data);

            p = p->next;

        }

        printf("/n");

    }

    return 0;

}

void init()

{

    int i;

    Node p;

    for (i = 0; i < n; i++)

    {

        p = (Node)malloc(sizeof(struct node));

        p->data = i;

        p->next = NULL;

        link[i] = (Node)malloc(sizeof(struct node));

        link[i]->next = p;

        p->pre = link[i];

    }

}

Node search(int data, int *pos)

{

    int i;

    Node p;

    for (i = 0; i < n; i++)

    {

        p = link[i]->next;

        while(p)

        {

            if (p->data == data)

            {

                *pos = i;

                return p;

            }

            p = p->next;

        }

    }

    return NULL;

}

void freespace()

{

    int i;

    Node p, q;

    for (i = 0; i < n; i++)

    {

        p = link[i]->next;

        while(p)

        {

            q = p->next;

            free(p);

        }

        free(link[i]);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null search struct dst include c