程序的主要思想是标识盘子的位置,和该入的柱子号,很容易让人联想到栈实现,设立三个栈,动态记录盘子数和要出栈的序号
定义t11.h包含常用头文件
#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"malloc.h"
#include"stdlib.h" //atoi(),exit();
#include"io.h" //eof()
#include"math.h"
#include"windows.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int Boolean;
int p=1; // 全局变量,记录移动步数
定义数据类型包含于头文件hrt.h中
#define INIT_STACK_SIZE 40
#define STACK_ADD 10
typedef struct
{
int xu; // 对应的盘子序数
int zbj; // 对应的纵坐标
}cun;
typedef struct // 把位子放入栈中 线性表操作
{
cun *top;
cun *bottom;
int stacksize;
} Sqstack;
定义实现函数包含于hrt.cpp中
extern p; // 引用全局变量
void initstack(Sqstack &L) // 初始化栈操作
{
L.bottom=(cun*)malloc(INIT_STACK_SIZE*sizeof(cun));
if(!L.bottom)
{
printf("内存分配失败!!");
exit(-1);
}
L.top=L.bottom;
L.stacksize=INIT_STACK_SIZE;
}
Status push(Sqstack &L,int xu,int zbj) // 压入位置 q表示行,p表示列
{
if(L.top-L.bottom >= L.stacksize)
{ L.bottom=(cun*)realloc(L.bottom,(L.stacksize+STACK_ADD)*sizeof(cun));
if(!L.bottom)
{
printf("内存分配失败!!");
exit(-1);
}
L.top=L.bottom+L.stacksize;
L.stacksize+=STACK_ADD;
}
L.top->xu=xu;
(L.top++)->zbj=zbj;
return OK;
}
Status pop(Sqstack &L,int &xu,int&zbj) // 弹出 将栈顶的元素带回
{
if(L.top == L.bottom)