NYOJ - 汉诺塔(三)
来源:互联网 发布:python time.sleep修改 编辑:程序博客网 时间:2024/05/22 06:24
93 汉诺塔(三)
定义一个栈一维数组,包含3个栈,代表3个柱子,依次执行指令
第一次提交时,注释什么多余的语句没删除,每个栈开的长度大了,提交结果 内存 972
然后将神马多余的都删除,栈长度改的正合适,提交结果 内存 308
[code]
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 66
#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "cstring"
using namespace std;
typedef int Status; //Status 相当于 int
typedef int ElemType;
typedef struct Stack
{
ElemType * base, * top;
}SqStack;
void InitStack(SqStack &S)//创建一个空栈
{
S.base = (ElemType *)malloc(sizeof(ElemType) *STACK_INIT_SIZE);
S.top = S.base;
}
Status StackEmpty(SqStack S)//判断是否为空
{
if(S.top == S.base) return TRUE;
else return FALSE;
}
void Push(SqStack &S,ElemType e)//插入元素e为新的栈顶元素
{
* S.top++ = e;
}
void Pop(SqStack &S,ElemType&e)//出栈
{
e = * --S.top;
}
ElemType GetTop(SqStack S)
{
return *(S.top-1);
}
int main()
{
int n;
scanf("%d",&n);
while(n--){
int p,q;
scanf("%d%d",&p,&q);
SqStack S[3];//定义3个栈,代表3个柱子
InitStack(S[0]); InitStack(S[1]); InitStack(S[2]);
for(int i=p; i>=1; i--)Push(S[0],i);//第一步,初始化1柱上的金片
int flag = 1;
int a,b,e;
for(int i=0; i<q; i++)//Q条指令
{
scanf("%d%d",&a,&b);//从a柱到b柱
if(StackEmpty(S[a-1])) { flag = 0;break;}
else Pop(S[a-1],e);
if(!StackEmpty(S[b-1]) &&e>GetTop(S[b-1])) { flag = 0;break;}
else Push(S[b-1],e);
}
if(flag) printf("legal\n");
else printf("illegal\n");
}
return 0;
}
[\code]
0 0
- NYOJ - 汉诺塔(三)
- NYOJ—88 汉诺塔(一)
- nyoj 587 houseblocks
- nyoj 592 spiral grid
- nand flash读写 (三) (转)
- nyoj 811
- nyoj 大数加法
- Robocode 编写机器人 三
- C# 多线程(三)
- nyoj 108 士兵杀敌
- nyoj 58 最小步数
- nyoj 1058 部分和
- nyoj 27 水池数目
- nyoj 116 士兵杀敌二
- nyoj 117 逆序数
- nyoj 722 数独
- nyoj 28 大数阶乘
- nyoj 45 棋盘覆盖
- 已知二叉树的中序遍历和前序遍历,…
- 二叉树
- sscanf函数的用法[百度收藏]
- 常用字符,整形,浮点型相互装换函…
- NYOJ—88 汉诺塔(一)
- NYOJ - 汉诺塔(三)
- 汇编习题
- 二叉排序树
- VS2012简单的使用感受+插件推荐【转】
- NYOJ-题目221 Tree
- NYOJ—题目88 找球号(一)
- 二叉平衡树
- 无向图的邻接表创建以及图的深度和…
- NYOJ-题目27 水池数目