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]);
}
}
#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]);
}
}
相关文章推荐
- Uva-101-The Blocks Problem
- UVA-101 The Blocks Problem
- 【例题5-2 UVA - 101】The Blocks Problem
- uva 101 木块问题 The Blocks Problem
- 数据结构 uva 101 - The Blocks Problem
- uva101 The Blocks Problem
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- uva101 - The Blocks Problem
- UVa 101 - The Blocks Problem
- UVa系列——101/The Blocks Problem
- Uva 101 - The Blocks Problem
- UVa101 - The Blocks Problem
- UVa 101 The Blocks Problem (超级模拟)
- UVA-101 The Blocks Problem 栈
- UVA - 101 The Blocks Problem
- UVA 101 The blocks problem 例题整理
- uva_101 - The Blocks Problem
- uva 101 The Blocks Problem 解题报告
- UVA 101 The Blocks Problem【vector】
- The Blocks Problem-UVA 101