uva 101 The Blocks Problem

来源:互联网 发布:linux终端快捷键设置 编辑:程序博客网 时间:2024/05/17 08:36

题目描述

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

  1. move a onto b,先将方块a和方块b上面的方块放回原位(见下图),然后将方块a放在方块b上;
  2. move a over b,先将方块a上面的方块放回原位,然后将方块a放到方块b所在的堆的顶部;
  3. pile a onto b,先将方块b上面的方块放回原位,然后将方块a连同其上面的方块一同放到方块b上面;
  4. 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:
20move 7 over 11move 1 over 12pile 11 onto 12move 2 over 12move 18 onto 19move 17 over 1move 16 onto 19move 16 onto 0move 4 over 10move 1 over 10move 13 onto 14quit
Output:
0: 0 161:2:3: 34:5: 56: 67:8: 89: 910: 10 4 111:12: 12 11 7 213:14: 14 1315: 1516:17: 1718: 1819: 19
【Debug】
Runtime Error一般是指数组越界或者是指针引用不合法;
Wrong Answer是结果不正确;注意当a,b在同一个堆时,不做任何处理;
【源代码】
使用链表实现插入和删除操作
/*11628618101The Blocks ProblemAcceptedC++0.0202013-04-18 13:54:45*/#include <stdio.h>#include <stdlib.h>#include <string.h>#define ARRAY_SIZE 30typedef 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 pl3PList 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) { // moveif (pre == NULL)ls[idx] = NULL;elsepre->pNext = NULL;tail[idx] = pre;pos[t0] = pos[t1];pt = pl1->pNext;// return the above blocks to the initial positonwhile (pt) {num = pt->num;next = pt->pNext;pt->pNext = NULL;ls[num] = tail[num] = pt; pos[num] = num;pt = next;}}else {  // pilepl2 = 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) { // ontoif (flg == 0) { // movepl1->pNext = NULL;pl3->pNext = pl1;tail[idx] = pl1;}else { // pilepl2->pNext = NULL;pl3->pNext = pl1;tail[idx] = pl2;}// return the above blocks to the initial positonwhile (pt) {num = pt->num;next = pt->pNext;pt->pNext = NULL;ls[num] = tail[num] = pt; pos[num] = num;pt = next;}}else { // overif (flg == 1){ // move tail[idx]->pNext = pl1;pl1->pNext = NULL;tail[idx] = pl1;}else { // piletail[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;}






原创粉丝点击