uva101 The Blocks Problem

简介: uva101 The Blocks Problem
#include <stdio.h>#include <stdlib.h>#include <string.h>#define LOCAL#define MAXN 30typedefstructnode{
intdata;
structnode*pre, *next;
}*Node;
Nodelink[MAXN] ;
intn;
voidinit();
Nodesearch(intdata, int*pos);
voidfreespace();
intmain()
{
charstr1[5], str2[5];
intsrc, dst;
Nodea, b, p, q;
intflag;
inti;
intpos1, pos2;
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifscanf("%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;
elseif (strcmp(str1, "move") ==0&&strcmp(str2, "over") ==0)
flag=2;
elseif (strcmp(str1, "pile") ==0&&strcmp(str2, "onto") ==0)
flag=3;
elseif (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;
        } elseif (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;
        } elseif (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;
        } elseif (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");
    }
return0;
}
voidinit()
{
inti;
Nodep;
for (i=0; i<n; i++)
    {
p= (Node)malloc(sizeof(structnode));
p->data=i;
p->next=NULL;
link[i] = (Node)malloc(sizeof(structnode));
link[i]->next=p;
p->pre=link[i];
    }
}
Nodesearch(intdata, int*pos)
{
inti;
Nodep;
for (i=0; i<n; i++)
    {
p=link[i]->next;
while(p)
        {
if (p->data==data)
            {
*pos=i;
returnp;
            }
p=p->next;
        }
    }
returnNULL;
}
voidfreespace()
{
inti;
Nodep, q;
for (i=0; i<n; i++)
    {
p=link[i]->next;
while(p)
        {
q=p->next;
free(p);
        }
free(link[i]);
    }
}
目录
相关文章
|
9月前
UVa1531 - Problem Bee
UVa1531 - Problem Bee
35 0
|
9月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
35 0
|
9月前
UVa389 - Basically Speaking
UVa389 - Basically Speaking
24 0
|
9月前
UVa11714 - Blind Sorting
UVa11714 - Blind Sorting
36 0
HDOJ(HDU) 1673 Optimal Parking
HDOJ(HDU) 1673 Optimal Parking
102 0
hdu 2818 Building Block
点击打开hdu 2818 思路: 带权并查集 分析: 1 题目给定2种指令 M x y把x的集合放在y集合的上面,C x求x的下面有多少个元素 2 我们用rank[x]表示x以下有多少个元素,那么对于指令M x y我们始终把左边的合并到右...
819 0
uva 100 The 3n+1 problem
题目链接: http://www.programming-challenges.com/pg.php?page=studenthome /* The 3n+1 problem 计算每个数的循环节长度,求给定区间的循环节长度的最大值。 */ #include&lt;iostream&gt; #include&lt;stdio.h&gt; using namespace std;
1151 0