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]);
    }
}
目录
相关文章
UVa1531 - Problem Bee
UVa1531 - Problem Bee
56 0
lecture 3.2 problem set 3
"Radioactive decay" is the process by which an unstable atom loses energy and emits ionizing particles - what is commonly refered to as radiation.
1141 0
lecture 2.2 problem set 1 and 2
1 COUNTING VOWELS   (10/10 分数) Assume s is a string of lower case characters.
1045 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;
1169 0
uva 1326 - Jurassic Remains
点击打开链接uva 1326 题意:给定n个由大写字母组成的字符串,选择尽量多的串使得每个大写字母都能出现偶数次 分析: 1 在一个字符串中每个字符出现的次数是无关的,重要的是只是这些次数的奇偶性。
926 0