您的位置:首页 > 其它

uva 101 The Blocks Problem

2013-04-19 08:41 429 查看
题目描述

机器人手臂操作方块,有四种操作:

move a onto b,先将方块a和方块b上面的方块放回原位(见下图),然后将方块a放在方块b上;
move a over b,先将方块a上面的方块放回原位,然后将方块a放到方块b所在的堆的顶部;
pile a onto b,先将方块b上面的方块放回原位,然后将方块a连同其上面的方块一同放到方块b上面;
pile a over b,将方块a及其上面的方块,放到方块b所在的堆的顶部。



图:方块起始位置
下面看一个具体实例的操作:
Initial state of blocks are:

0:
1:
2:
3:
4:
5:
6:
7:
8: 0 1 2 3
9: 4 5 6 7
move 1 onto 5
0:
1:
2: 2
3: 3
4:
5:
6: 6
7: 7
8: 0
9: 4 5 1

move 1 over 5
0:
1:
2: 2
3: 3
4:
5:
6:
7:
8: 0
9: 4 5 6 7 1

pile 1 onto 5
0:
1:
2:
3:
4:
5:
6: 6
7: 7
8: 0
9: 4 5 1 2 3

pile 1 over 5
0:
1:
2:
3:
4:
5:
6:
7:
8: 0
9: 4 5 6 7 1 2 3
给出一组测试数据
Input:
20
move 7 over 11
move 1 over 12
pile 11 onto 12
move 2 over 12
move 18 onto 19
move 17 over 1
move 16 onto 19
move 16 onto 0
move 4 over 10
move 1 over 10
move 13 onto 14
quit

Output:
0: 0 16
1:
2:
3: 3
4:
5: 5
6: 6
7:
8: 8
9: 9
10: 10 4 1
11:
12: 12 11 7 2
13:
14: 14 13
15: 15
16:
17: 17
18: 18
19: 19
【Debug】
Runtime Error一般是指数组越界或者是指针引用不合法;
Wrong Answer是结果不正确;注意当a,b在同一个堆时,不做任何处理;
【源代码】
使用链表实现插入和删除操作
/*
11628618	101	The Blocks Problem	Accepted	C++	0.020	2013-04-18 13:54:45
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 30

typedef struct tagList{
int num;
struct tagList *pNext;
}List, *PList;

PList ls[ARRAY_SIZE];  //方块堆列表,不含头指针
PList tail[ARRAY_SIZE]; // 链尾指针
int pos[ARRAY_SIZE]; // 方块所在的堆

int n; // 方块数
int t0, t1; // t0方块a的标号,t1方块b的标号
int flg; //标志是哪种类型操作

// 初始化方块位置及辅助数组
void InitList()
{
int i;
PList pls = NULL;
for (i=0; i<n; ++i) {
pls = (PList)malloc(sizeof(List));
pls->num = i;
pls->pNext = NULL;
ls[i] = tail[i] = pls;
pos[i] = i;
}
}
// 输入操作
int getInput(char *ps0)
{
char s1[10];

scanf("%s", ps0);
if (strcmp(ps0, "quit") == 0) {
return -1;
}
scanf("%d%s%d", &t0, s1, &t1);
if (strcmp(ps0, "move") == 0) {
if (strcmp(s1, "onto") == 0) {
return 0;
}else{
return 1;
}
}else {
if (strcmp(s1, "onto") == 0) {
return 2;
}else{
return 3;
}
}
}
// move [pl1,pl2] to pl3
PList pt, next, pl1, pl2, pre, pl3;
int num;

void move(int idx)
{
pl1 = ls[idx];
pre = NULL;
while (pl1 && pl1->num != t0) {
pre = pl1;
pl1 = pl1->pNext;
}
if (flg <= 1) { // move
if (pre == NULL)
ls[idx] = NULL;
else
pre->pNext = NULL;
tail[idx] = pre;
pos[t0] = pos[t1];
pt = pl1->pNext;
// return the above blocks to the initial positon
while (pt) {
num = pt->num;
next = pt->pNext;
pt->pNext = NULL;
ls[num] = tail[num] = pt;
pos[num] = num;

pt = next;
}
}else {  // pile
pl2 = tail[idx];
if (pre == NULL) {
ls[idx] = tail[idx] = NULL;
}else {
pre->pNext = NULL;
tail[idx] = pre;
}
PList pl0 = pl1;
while (pl0 != pl2) {
pos[pl0->num] = pos[t1];
pl0 = pl0->pNext;
}
pos[pl0->num] = pos[t1];

}
}
// 插入操作
void insert(int idx)
{
pl3 = ls[idx];
while (pl3 && pl3->num != t1) {
pl3 = pl3->pNext;
}
pt = pl3->pNext;
if (flg == 0 || flg == 2) { // onto
if (flg == 0) { // move
pl1->pNext = NULL;
pl3->pNext = pl1;
tail[idx] = pl1;
}else { // pile
pl2->pNext = NULL;
pl3->pNext = pl1;
tail[idx] = pl2;
}
// return the above blocks to the initial positon
while (pt) {
num = pt->num;
next = pt->pNext;
pt->pNext = NULL;
ls[num] = tail[num] = pt;
pos[num] = num;

pt = next;
}
}else { // over
if (flg == 1){ // move
tail[idx]->pNext = pl1;
pl1->pNext = NULL;
tail[idx] = pl1;
}else { // pile
tail[idx]->pNext = pl1;
pl2->pNext = NULL;
tail[idx] = pl2;
}
}
}
// 输出结果
void getOutput()
{
int i;
PList pl;
for (i=0; i<n; ++i) {
printf("%d:", i);
pl = ls[i];
while (pl) {
printf(" %d", pl->num);
pl = pl->pNext;
}
printf("\n");
}
}

int main()
{
scanf("%d", &n);
InitList();
char s0[10];
int pos0, pos1;
while ((flg = getInput(s0)) >= 0) {
pos0 = pos[t0];
pos1 = pos[t1];
if (t0 == t1 || pos0 == pos1) continue;

move(pos0);
insert(pos1);

//	getOutput();
}
getOutput();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 101 链表 模拟