当前位置:网站首页>栈的应用
栈的应用
2022-07-21 11:22:00 【我辈当自强】
1、迷宫求解
typedef struct{
int ord; //通道块在路径上的‘序号’
PosType seat; //通道块在迷宫中的‘坐标位置’
int di; //从此通道块走向下一通道块的“方向 ”
}SElemType; //栈的元素类型
Status MazePath(MazeType maze,PosType start,PosType end)
{
//若迷宫maze中存在从入口start到出口end的通道,则求得一条存放在栈中(从栈底到栈顶),并返回TRUE,否则返回FALSE
InitStack(S);curpos=start;//设定'当前位置'为‘入口位置’
curstep=1; //探索第一步
do{
if(Pass(curpos)){
//当前位置可以通过,即是未曾走到过的通道口
FootPrint(curpos);//留下足迹
e=(curstep,curpos,1);
Push(S,e);//加入路径
if(curpos==end) return (TRUE);//到达终点
curpos=NextPos(curpos,1);//下一位置是当前位置的东邻
curstep++;//探索下一步
}
else{
if(!StackEmpty(S)){
Pop(S,e);
while(e.di==4&&!StackEmpty(S)){
MarkPrint(e.seat);Pop(S,e);//留下不能通过的标记,并退回一步
}
if(e.di<4){
e.di++;Push(S,e);//换下一个方向探索
curPos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻块
}
}
}
}while(!StackEmpty(S));
return(FALSE);
}
2、表达式求值
OperandType EvaluateExpression(){
//算术表达式求值的算符优先算法。OPTR和OPND分别为运算符栈和运算数栈
//OP运算符集合
InitStack(OPTR);Push(OPTR,'#');
InitStack(OPND);c=getchar();
while(c!='#'||GetTop(OPTR)!='#'){
if(!In(c,OP)){
Push(OPND,c);
c=getchar();//不是运算符则进栈
}
else
switch(Precede(GetTop(OPTR),c)){
case '<'://栈顶元素优先权低
Push(OPTR,c);c=getchar();
break;
case '='://脱括号并接收下一字符
Pop(OPTR,x);c=getchar();
break;
case '>'://退栈并将运算结果入栈
Pop(OPTR,theta);
Pop(OPND,b);Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
}
}
return GetTop(OPND);
3、Hanoi问题
void hanoi(int n,char x,char y,char z)
{
//将塔座x上直径由小到大且自上而下编号为1至n的n个圆盘按规则搬到塔坐z上,y可用作辅助塔坐
//搬动操作move(x,n,z)可定义为(c是初值为0的全局变量,对搬动计数)
if(n==1)
move(x,1,z);//将编号为1的圆盘从x移到z
else{
hanoi(n-1,x,z,y);//将x上编号为1至n-1的圆盘移到y,z作辅助塔
move(x,n,z);//将编号为n的圆盘从x移到z
hanoi(n-1,y,x,z);//将y上编号为1至n-1的圆盘移到z,x作辅助塔
}
}
边栏推荐
- TCL/TK分组和替换规则
- nmap 扫描命令
- 代码管理(新手)
- Ansvc reactive power compensation device helps an environmental protection energy project in Jiangsu
- Record the problem of removing the limit of the number of connections at one time
- NFTScan 与 Port3 在 NFT 数据领域达成战略合作
- Application of DC distribution system products at the end of data center
- Sequences, Time Series and Prediction in Tessorflow quizs on Coursera (一)
- 图扑软件数字孪生民航飞联网,构建智慧民航新业态
- Tclsh Array操作
猜你喜欢
Flink使用api执行sql的时候报错
从0开始实现一个代理池
Alibaba programmer, working for 6 years, real salary exposure
一招教你拿捏网上视频
百度世界2022,明天见!
Idea建文件夹时,文件夹的空文件夹的展开与重叠
Learning notes of Stanford CV course (I)
Ansvc reactive power compensation device helps an environmental protection energy project in Jiangsu
PostgreSQL每周新闻—2022年7月13日
Talking about the application of service worker in caching resources and web push
随机推荐
Idea publishes executable jar packages
Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
Yii2 custom login authentication
数据模型子类化参考
定时任务框架
进程池及回掉函数[通俗易懂]
3.1栈
合并k个升序列表
莫名其妙的越界错误原因之条件判断顺序——基于LeetCode 99题,恢复二叉搜索树
抽象类名作为形参和返回值
mysql隐藏版本号
eBPF验证器
Tcl编程风格指南
[译] PostgreSQL 怎么决定PG 的备份策略
Application of DC distribution system products at the end of data center
Merge K ascending lists
Serial Vector Format(SVF)文件格式
Yii2 jsblock:: begin invalid problem
解决ssh登录后闲置时间过长而断开连接
OneManager与CloudFlare Workers部署安装-绑定域名和使用CloudFlare CDN加速