资源简介
最近在leetcode上做到一个运用递归算法解决的题目。忽然记起大一自学数据结构那段岁月。在此拿出三年前写的老鼠走迷宫案例来进行一个简单的分析铺垫,顺便附上完整代码,关于本资源的博客地址:https://blog.csdn.net/qq_34901049/article/details/94403330
代码片段和文件信息
#include
#include
#define max 8 //设置迷宫有效宽度
using namespace std;
int bx=1by=1; //设置迷宫起点和终点(左上角为起点右下角为终点)
int fx=8fy=8;
void white()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
}
void red()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)FOREGROUND_INTENSITY|FOREGROUND_RED);
}
int maze[max+2][max+2]= //制作并赋值二维迷宫数组 为计算方便在迷宫外围加了一层 “墙“(‘0‘表示路 ‘1‘表示墙)
{
//0 1 2 3 4 5 6 7 8 9
{1 1 1 1 1 1 1 1 1 1}//0
{1 0 1 1 1 1 1 1 0 1}//1
{1 1 0 0 0 0 1 1 0 1}//2
{1 1 1 0 1 1 1 1 1 1}//3
{1 1 1 0 1 0 0 0 1 1}//4
{1 1 1 0 0 0 1 1 1 1}//5
{1 1 0 1 1 0 1 1 1 1}//6
{1 1 0 1 1 0 0 0 1 1}//7
{1 1 0 0 1 1 1 1 0 1}//8
{1 1 1 1 1 1 1 1 1 1} //9
};
typedef struct move_type//移动方向位置标示(八向)
{
int x;
int y;
}Move;
Move move[8];//创建八个移动方向
void init_move()//初始化方向进位表
{
move[0].x move[4].x=0;
move[1].x=1 move[2].x=1 move[3].x=1;
move[5].x=-1 move[6].x=-1 move[7].x=-1;
move[2].y=0 move[6].y=0;
move[7].y=1 move[0].y=1 move[1].y=1;
move[3].y=-1 move[4].y=-1 move[5].y=-1;
}
typedef struct stack_type//迷宫栈(用于保存行走路线)
{
int x;
int y;
int direction;
struct stack_type *next;
}stack1;
stack1 *head=new stack_type;//创建头结点
void init_stack()//初始化栈
{
head->next=NULL;
head->x=0;
head->y=0;
head->direction=-1;
}
int count_stack=0;
void push_stack(int xint yint direction)//进栈
{
stack1 *p1=new stack_type;
p1->next=head->next;
p1->x=x;
p1->y=y;
p1->direction=direction;
head->next=p1;
count_stack++;
}
void pop_stack(int &x1int &y1int &direction1)//出栈
{
stack1 *p1=head->next;
x1=p1->x;
y1=p1->y;
direction1=p1->direction;
head->next=p1->next;
delete p1;
count_stack--;
}
void trave_stack()
{
stack1 *p=new stack_type;
p=head->next;
int n=0;
while(p!=NULL)
{
n++;
if(n==1)
{
cout<<“还原路线(坐标(xy)):“< red();
cout<<“终点:“;
white();
cout<<“
- 上一篇:基于MFC的扫雷
- 下一篇:RS232串口通讯VS2010
相关资源
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
- VC++基于OpenGL模拟的一个3维空间模型
评论
共有 条评论