the blocks problem(uva 101 or poj 1208)

题目描述见:uva 101 or poj 1208

关键在于彻底理解题目中搬积木的几个命令的含义,见具体分析

如果还不能理解题意,那么找一个正确通过的代码,编译并输入测试数据,查看其每一个命令的执行情况。如我的代码中162行注释内容取消后即可显示每一步执行后当前积木的情况)

这个题目似乎没有合适的数据结构,由于插入删除操作较多,所以直接用了链表,并且使用双重指针,差点把自己搞晕。(另外,刚开始写的代码能在poj1208上通过,但不能在uva 101上通过,后来发现是141行中的continue误写成了break。网上也有类似的情况,似乎是因为没有考虑到特殊情况的处理(即两个积木块号相同或者处于同一列时需要忽略该命令))

相关C语言代码:

/* uva 101 or poj 1208 */
/*the Blocks problem */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct node {int data;struct node *prev;struct node *next;
};
typedef struct node *pBlock;
static void CreateHeader(pBlock *ppBlk)
{pBlock newBlk = malloc(sizeof(*newBlk));newBlk -> next = newBlk->prev = NULL;(*ppBlk) = newBlk;
}
static void InsertOnFirst(pBlock *ppBlk,  int data)
{pBlock newBlk = malloc(sizeof(*newBlk));newBlk -> data = data;newBlk -> next = NULL;newBlk -> prev = *ppBlk;(*ppBlk)->next = newBlk;
}
static int FindBlock(pBlock *ppBlk, int blockNumber, pBlock *ppretBlock)
{pBlock tmpBlk = (*ppBlk)->next;while(tmpBlk != NULL)if(tmpBlk->data == blockNumber){*ppretBlock = tmpBlk;return 1;}elsetmpBlk = tmpBlk->next;return 0;
}
static void PopAllFollowBlks(pBlock *ppBTab,pBlock *ppBlk)
{pBlock tmpBlk,curBlk = (*ppBlk)->next;(*ppBlk)->next = NULL;while(curBlk != NULL){tmpBlk = curBlk;curBlk = curBlk->next;ppBTab[tmpBlk->data] ->next = tmpBlk;tmpBlk->next = NULL;tmpBlk->prev = ppBTab[tmpBlk->data];}
}static void MoveOnto(pBlock *ppBTab,pBlock *ppSrcBlk, pBlock *ppDstBlk)
{PopAllFollowBlks(ppBTab,ppSrcBlk);PopAllFollowBlks(ppBTab,ppDstBlk);/*pBlock pSrcTmpBlk = pSrcBlk->next,pDstTmpBlk=pDstBlk->next;*/(*ppSrcBlk)->prev->next = NULL;(*ppSrcBlk) ->next = (*ppDstBlk)->next;(*ppSrcBlk)->prev = (*ppDstBlk);(*ppDstBlk)->next = (*ppSrcBlk);
}
static void MoveOver(pBlock *ppBTab,pBlock *ppSrcBlk, pBlock *ppDstBlk)
{pBlock lastDstBlk = (*ppDstBlk);PopAllFollowBlks(ppBTab,ppSrcBlk);(*ppSrcBlk)->prev->next = NULL;while(lastDstBlk->next != NULL)lastDstBlk = lastDstBlk->next;(*ppSrcBlk)->next = lastDstBlk->next;(*ppSrcBlk)->prev = lastDstBlk;lastDstBlk->next = (*ppSrcBlk);
}
static void PileOnto(pBlock *ppBTab,pBlock *ppSrcBlk, pBlock *ppDstBlk)
{PopAllFollowBlks(ppBTab,ppDstBlk);(*ppSrcBlk)->prev->next = NULL;(*ppDstBlk)->next = *ppSrcBlk;(*ppSrcBlk)->prev = *ppDstBlk;
}
static void PileOver(pBlock *ppBTab,pBlock *ppSrcBlk, pBlock *ppDstBlk)
{pBlock lastDstBlk = (*ppDstBlk);(*ppSrcBlk)->prev->next = NULL;while(lastDstBlk->next != NULL)lastDstBlk = lastDstBlk->next;(*ppSrcBlk)->prev = lastDstBlk;lastDstBlk->next = (*ppSrcBlk);
}   static void PrintBlock(pBlock pBlk)
{pBlock tmpBlk = (pBlk) -> next;while(tmpBlk != NULL){printf(" %d",tmpBlk->data);tmpBlk = tmpBlk -> next;}printf("\n");
}
static void PrintBlks(pBlock *ppBlk, int arraysize)
{int i;for(i = 0; i < arraysize; i++){printf("%d:",i);PrintBlock(ppBlk[i]);}
}int main(void)
{int n;pBlock *ppBlks,pSrcBlk,pDstBlk;char str1[10],str2[10];int fstBlkNum,scdBlkNum,i,j;int isFound1, isFound2;scanf("%d",&n);getchar();ppBlks = malloc(n * sizeof(*ppBlks));for(i = 0; i < n; i++){CreateHeader(&ppBlks[i]);InsertOnFirst(&ppBlks[i],i);}/*  PrintBlks(ppBlks,n);*/while(1){scanf("%s",str1);if(strcmp(str1,"quit") == 0)break;scanf("%d %s %d",&fstBlkNum,str2,&scdBlkNum);if(fstBlkNum == scdBlkNum)continue;for(i = 0; i < n; i++){isFound1 = FindBlock(&ppBlks[i],fstBlkNum,&pSrcBlk);if(isFound1)break;}for(j = 0; j < n; j++){isFound2 = FindBlock(&ppBlks[j],scdBlkNum,&pDstBlk);if(isFound2)break;}if(i != j && strcmp(str1,"move") == 0 && strcmp(str2,"onto") == 0)MoveOnto(ppBlks,&pSrcBlk,&pDstBlk);else if(i != j && strcmp(str1,"move") == 0 && strcmp(str2,"over") == 0)MoveOver(ppBlks,&pSrcBlk,&pDstBlk);else if(i != j && strcmp(str1,"pile") == 0 && strcmp(str2,"onto") == 0)PileOnto(ppBlks,&pSrcBlk,&pDstBlk);else if(i != j && strcmp(str1,"pile") == 0 && strcmp(str2,"over") == 0)PileOver(ppBlks,&pSrcBlk,&pDstBlk);/*      PrintBlks(ppBlks,n);*/}PrintBlks(ppBlks,n); return 0;
}
 

 另外一种方案(使用栈进行操作):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MINSTACKSIZE 30
#define STACKINCREMENT 30
struct stack{int capacity;int top;int *array;
};
typedef struct stack Stack;Stack *CreateStack()
{Stack *pStk = malloc(sizeof(*pStk));pStk->array = malloc(MINSTACKSIZE*sizeof(*(pStk->array)));if(pStk->array == NULL){fprintf(stderr,"error:no space to allocate.\n");exit(EXIT_FAILURE);}else{pStk->capacity = MINSTACKSIZE;pStk->top = 0;}return pStk;
}int IsStackEmpty(Stack *pStk)
{return pStk->top == 0;
}
int IsStackFull(Stack *pStk)
{return pStk->capacity == pStk->top + 1;
}
int Push(Stack *pStk, int data)
{if(IsStackFull(pStk)){pStk->array = realloc(pStk, (pStk->capacity+STACKINCREMENT)*sizeof(*(pStk->array)));if(pStk->array == NULL){fprintf(stderr, "error: no space to allocate\n");exit(EXIT_FAILURE);}else{pStk -> capacity += STACKINCREMENT;}}pStk -> array[++(pStk -> top)] = data;return 0;
}
int Pop(Stack *pStk)
{if(IsStackEmpty(pStk)){fprintf(stderr,"error: empty stack can pop nothing\n");exit(EXIT_FAILURE);}elsereturn pStk->array[(pStk -> top) --] ;
}
int Top(Stack *pStk)
{if(IsStackEmpty(pStk)){fprintf(stderr,"error: empty stack  don't have top element\n");exit(EXIT_FAILURE);}elsereturn pStk->array[pStk -> top] ;
}int Find(Stack *pStk, int data)
{int i;for(i = 1; i <= pStk->top; i++)if(pStk -> array[i] == data)break;if(i > pStk->top)return 0;elsereturn i;
}int PopFollowingBack(Stack **ppStkArray, int index, int pos)  
{Stack *pCurStk = ppStkArray[index];int curData;while(pCurStk -> top > pos){curData = Pop(pCurStk);/*      pTmpStk = ppStkArray[curData];*/Push(ppStkArray[curData],curData);}return 0;
}
int PushCurAndFollowing(Stack **ppStkArray, int srcIndex, int srcPos, int dstIndex, int dstPos)
{int i,srcTop = ppStkArray[srcIndex] -> top;for(i = srcPos; i <= srcTop; i++){Push(ppStkArray[dstIndex],ppStkArray[srcIndex]->array[i]);}ppStkArray[srcIndex] -> top -= srcTop - srcPos + 1;return 0;
}
int MoveOnto(Stack **ppStkArray,int srcIndex,int srcPos,int dstIndex,int dstPos)
{PopFollowingBack(ppStkArray,srcIndex,srcPos);PopFollowingBack(ppStkArray,dstIndex,dstPos);PushCurAndFollowing(ppStkArray,srcIndex,srcPos,dstIndex,dstPos);return 0;
}
int MoveOver(Stack **ppStkArray,int srcIndex,int srcPos,int dstIndex,int dstPos)
{PopFollowingBack(ppStkArray,srcIndex,srcPos);PushCurAndFollowing(ppStkArray,srcIndex,srcPos,dstIndex,dstPos);return 0;
}
int PileOnto(Stack **ppStkArray,int srcIndex,int srcPos,int dstIndex, int dstPos)
{PopFollowingBack(ppStkArray,dstIndex,dstPos);PushCurAndFollowing(ppStkArray,srcIndex,srcPos,dstIndex,dstPos);return 0;
}
int PileOver(Stack **ppStkArray,int srcIndex,int srcPos,int dstIndex,int dstPos)
{PushCurAndFollowing(ppStkArray,srcIndex,srcPos,dstIndex,dstPos);return 0;
}void PrintStack(Stack *pStk)
{int i;for(i = 1; i <= pStk->top; i++)printf(" %d",pStk->array[i]);printf("\n");
}
void PrintAllBlocks(Stack **ppStk, int blockNum)
{int i;for(i = 0; i < blockNum; i++){printf("%d:",i);PrintStack(ppStk[i]);}
}
int main(void)
{Stack **ppStkArray;int n;int i,j;char str1[10],str2[10];int srcData,dstData;int srcPos,dstPos;scanf("%d",&n);ppStkArray = malloc(n*sizeof(*ppStkArray));for(i = 0; i < n; i++){  ppStkArray[i] = CreateStack();Push(ppStkArray[i],i);}/*  PrintAllBlocks(ppStkArray,n);printf("after popping %d\n",Pop(ppStkArray[2]));PrintAllBlocks(ppStkArray,n);*/while(1){scanf("%s",str1);if(strcmp(str1,"quit")==0)break;scanf("%d %s %d",&srcData,str2,&dstData);if(srcData == dstData)continue;for(i = 0; i < n; i++){srcPos = Find(ppStkArray[i],srcData);if(srcPos)break;}for(j = 0; j < n; j++){dstPos = Find(ppStkArray[j],dstData);if(dstPos)break;}if(i == j)continue;else{if(strcmp(str1,"move") == 0 && strcmp(str2, "onto") == 0)MoveOnto(ppStkArray,i,srcPos,j,dstPos);else  if(strcmp(str1,"move") == 0 && strcmp(str2, "over") == 0)MoveOver(ppStkArray,i,srcPos,j,dstPos);else  if(strcmp(str1,"pile") == 0 && strcmp(str2, "onto") == 0)PileOnto(ppStkArray,i,srcPos,j,dstPos);else  if(strcmp(str1,"pile") == 0 && strcmp(str2, "over") == 0)PileOver(ppStkArray,i,srcPos,j,dstPos);/*PrintAllBlocks(ppStkArray,n);*/}}PrintAllBlocks(ppStkArray,n);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546046.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

调整灰度图像的大小,而无需在Python中使用任何内置函数

In this program, we will be using two functions of OpenCV-python (cv2) module. Lets see their syntax and descriptions first. 在此程序中&#xff0c;我们将使用OpenCV-python(cv2)模块的两个功能。 首先让我们看看它们的语法和说明。 1) imread():It takes an absolu…

第一章 认识计算机

*(%)^*&!*第一讲 了解计算机基础知识一、计算机的发展历程1、计算机的起源&#xff08;1&#xff09;世界上第一台计算机&#xff1a;1946年诞生&#xff0c;名称为ENIAC。&#xff08;2&#xff09;世界上第一台并行计算机&#xff1a;1950年诞生&#xff0c;名称为EDVAC&…

scoket多线程例子

大体思路&#xff0c;有n台mc&#xff0c;要dump出数据&#xff0c;n台进行对比&#xff0c;看数据是否一致&#xff0c;设计到同时dump的问题&#xff0c;server断发条指令给这n台mc&#xff0c;mc同时去dump把结果返回给server端&#xff0c;server端把这些结果进行对比serve…

csapp bufbomb实验

csapp (《深入理解计算机系统》&#xff09;一书中有一个关于缓冲区溢出的实验&#xff0c;其程序代码如下&#xff1a; /* Bomb program that is solved using a buffer overflow attack */#include <stdio.h> #include <stdlib.h> #include <ctype.h> #in…

漫画:对象是如何被找到的?句柄 OR 直接指针?

小贴士&#xff1a;想要使用并定位 Java 对象&#xff0c;就要用到 Java 虚拟机栈&#xff08;Java Virtual Machine Stack&#xff09;&#xff0c;它描述的是 Java 方法执行的线程内存模型&#xff1a;每个方法被执行的时候&#xff0c;Java 虚拟机都会同步创建一个栈帧&…

在C ++中检查一个数组是否是另一个数组的子数组

Prerequisite: std::equal() function 先决条件&#xff1a; std :: equal()函数 Problem statement: 问题陈述&#xff1a; Check if one array is subarray of another or not. 检查一个数组是否是另一个数组的子数组。 Example: 例&#xff1a; Input 1:Arr1 [3, 4, 5, …

第二章 认识计算机硬件

*(%)^*&!*第一讲 认识计算机主板一、主板的结构1、主板结构分类&#xff08;2&#xff09;AT、Baby-AT型&#xff08;2&#xff09;ATX型&#xff08;3&#xff09;Micro ATX板型&#xff08;4&#xff09;LPX、NLX、Flex ATX板型&#xff08;5&#xff09;EATX、WATX板型&…

IDEA 不为人知的 5 个骚技巧!真香!

工欲善其事&#xff0c;必先利其器&#xff0c;磊哥最近发现了几个特别棒的 IDEA“骚”技巧&#xff0c;已经迫不及待的想要分享给你了&#xff0c;快上车...1.快速补全行末分号使用快捷键 Shfit Ctrl Enter 轻松实现。2.自带的 HTTP 请求工具IDEA 自带了 HTTP 的测试工具&am…

教育编程语言(转)

这是wikipedia上的内容&#xff0c;转载保存&#xff0c;以便以后查阅&#xff0c;英文版见Educational programming language 主要是介绍了一些适合于教育的编程语言&#xff0c;分别适合于不同的个人需求。 详细内容如下&#xff1a; 许多教育性质的程序设计语言都提供建议…

JavaScript | 将十进制转换为十六进制,反之亦然

Sometimes we need to convert an integer value which is in decimal format to the hexadecimal string in JavaScript or need a decimal value from a given hexadecimal string. 有时&#xff0c;我们需要将十进制格式的整数值转换为JavaScript中的十六进制字符串&#xf…

漫画:Integer 竟然有 4 种比较方法?

代码测试public class IntegerTest {public static void main(String[] args) {Integer i1 127;Integer i2 127;System.out.println(i1 i2);Integer i3 128;Integer i4 128;System.out.println(i3 i4);} }以上代码的执行结果为&#xff1a;truefalse首先&#xff0c;当我…

第三章 组装个人计算机

*(%)^*&!*第一讲 选购个人计算机部件1、计算机配件选购的基本原则&#xff08;1&#xff09;组装电脑按需配置&#xff0c;明确电脑使用范围&#xff1b;&#xff08;2&#xff09;衡量装机预算&#xff1b;&#xff08;3&#xff09;衡量整机运行速度。2、电脑配件选购注意…

IP地址的分类——a,b,c 类是怎样划分的

如今的IP网络使用32位地址&#xff0c;以点分十进制表示&#xff0c;如172.16.0.0。地址格式为&#xff1a;IP地址网络地址&#xff0b;主机地址 或 IP地址主机地址&#xff0b;子网地址&#xff0b;主机地址。 IP地址类型 最初设计互联网络时&#xff0c;为了便于寻址以及层次…

《Introduction to Computing Systems: From bits and gates to C and beyond》

很好的一本计算机的入门书&#xff0c;被很多学校采纳作为教材&#xff0c;作者Yale N. Patt 是计算机界的泰斗。中文版名为《计算机系统概论》&#xff08;译者&#xff1a;梁阿磊 , 蒋兴昌, 林凌&#xff09; 书籍首页 (旧版首页 &#xff09; LC-3相关工具 LC-3Help 采…

在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居

在数组中查找第k个最大元素Problem statement: 问题陈述&#xff1a; Given an array of elements, find the nearest (on the right) greatest element ofeach element in the array. (Nearest greatest means the immediate greatest one on the right side). 给定一个元素数…

6种快速统计代码执行时间的方法,真香!(史上最全)

我们在日常开发中经常需要测试一些代码的执行时间&#xff0c;但又不想使用向 JMH&#xff08;Java Microbenchmark Harness&#xff0c;Java 微基准测试套件&#xff09;这么重的测试框架&#xff0c;所以本文就汇总了一些 Java 中比较常用的执行时间统计方法&#xff0c;总共…

fltk 库

fltk是一个小型、开源、支持OpenGL 、跨平台&#xff08;windows,linux,mac OSX)的GUI库&#xff0c;它兼容xforms 图形库&#xff08;unix/linux下的一个C语言图形库)&#xff0c;所以可以用来开发模块化的程序&#xff0c;同时也可以使用面向对象开发程序&#xff0c;使用起来…

人工智能ai 学习_人工智能中强化学习的要点

人工智能ai 学习As discussed earlier, in Reinforcement Learning, the agent takes decisions in order to attain maximum rewards. These rewards are the reinforcements through which the agent learns in this type of agent. 如前所述&#xff0c;在“ 强化学习”中 &…

第四章 计算机软件安装与调试

*(%)^*&!*第一讲 系统BIOS和CMOS参数设置&#xff08;1&#xff09;一、BIOS、CMOS的基本概念1.BIOS的含义BIOS是只读存储器基本输入/输出系统的简写&#xff0c;是被雇花道计算机主板ROM芯片上的一组程序&#xff0c;为计算机提供最低级、最直接的硬件控制。2.CMOS的含义C…

连夜整理了几个开源项目,毕设/练手/私活一条龙!

一直以来&#xff0c;总有小伙伴问说&#xff1a;诶&#xff0c;有没有什么好的项目推荐啊&#xff0c;想参考使用。一般用途无非如下几种情况&#xff1a;自学练手&#xff1a;从书本和博客的理论学习&#xff0c;过渡到实践练手吸收项目经验&#xff0c;找工作写简历时能参考…