走迷宫动画演示

需下载EasyX库 https://easyx.cn/

 这是无动画演示的version https://www.cnblogs.com/xxxsans/p/13930576.html

 

 

 

 

 

code

#include<fstream>
#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<graphics.h>//引用图形库
using namespace std;

const int MaxSize = 12;//迷宫的最大规模
int maze[MaxSize][MaxSize];//迷宫矩阵二维数组
int m, n;//迷宫的行列数
int speed = 1000;//设置动画速度,越大演示的越慢
int win_high = 500, win_width = 500;
int rectangle_high = 30, rectangle_width = 30;
struct node
{
    int x;
    int y;
};
node current_node;//当前点
node next_node;//下一个点
node start_node;//迷宫起点
node end_node;//迷宫终点
stack<node> S;
vector<node>path;

int has_nextnode();//在current_node处是否存在可向前的方向
void forward();//在current_node点,向next_node前进一步
void backward();//后退一步
void draw_rectangle(int x, int y, int high, int width, int type);//在x行y列绘制高high宽widt,类型为type的矩形

void init_win() {
    initgraph(win_high, win_width);//创建窗口
    setorigin(100, 100);//设置原点
    cleardevice();//清理窗口
    setcolor(BLACK);//设置背景为黑色
    return;
}

void init_maze()//初始化迷宫矩阵
{
    int i, j;
    string filename = "../maze55.txt";
    ifstream infile(filename);
    infile >> m >> n;//读文件,获取行数m,列数n
    infile >> start_node.x >> start_node.y;//迷宫起点
    infile >> end_node.x >> end_node.y;//迷宫终点
    for (i = 0; i <= m; i++)//第0行和第n+1行设为障碍
    {
        maze[i][0] = 1;
        maze[i][n + 1] = 1;
    }
    for (j = 0; j <= n; j++)//第0列和第n+1列设为障碍
    {
        maze[0][j] = 1;
        maze[m + 1][j] = 1;
    }
    for (i = 1; i <= m; i++)
    {
        for (j = 1; j <= n; j++)
        {
            infile >> maze[j][i];//为了和text文档的行列相符合,把矩阵做了一个转置
        }
    }
    infile.close();
}

void draw_rectangle(int x, int y, int high, int width, int type) {
    switch (type) {//根据type的值这的不同的颜色;
    case 0:setfillcolor(WHITE); break;//未走过的路
    case 1:setfillcolor(DARKGRAY); break;//障碍
    case 2:setfillcolor(BLUE); break;//已经走过的路
    case 3:setfillcolor(RED); break;//当前位置
    case 4:setfillcolor(GREEN); break;//回退的点
    case 5:setfillcolor(YELLOW); break;//求解出的路径
    default:setfillcolor(-1 * type * 0x001100);//负数的时候显示灰度值
    }
    solidrectangle((x - 1) * rectangle_width, (y - 1) * rectangle_high, (x - 1) * rectangle_width + rectangle_high, (y - 1) * rectangle_high + rectangle_width);
}

void draw_maze() {//绘制迷宫的各个矩形
    setlinestyle(PS_SOLID, 3);
    setlinecolor(BLACK);
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            draw_rectangle(i, j, rectangle_high - 3, rectangle_width - 3, maze[i][j]);//画迷宫i行j列的顶点
        }
    }
}

void display_DFSpath() {//根据栈中保留的路径突出显示起点到终点的路径
    while (!S.empty()) {
        current_node = S.top();
        draw_rectangle(current_node.x, current_node.y, rectangle_high - 3, rectangle_width - 3, 5);
        S.pop();
    }
}

int  has_nextnode()
{
    for (int i = 0; i <= 3; i++)//判断当前current_node是否可以继续向前
    {
        next_node = current_node;
        switch (i)
        {
        case 0:
            next_node.x--;
            break;
        case 1:
            next_node.y--;
            break;
        case 2:
            next_node.x++;
            break;
        case 3:
            next_node.y++;
            break;
        }
        if (maze[next_node.x][next_node.y] == 0)
            return 1;//试探i号方向下的next_node是否可走,若可以,返回1;
    }
    return 0;//不存在可向前的方向
}

void forward()//在current_node点,向next_node前进
{
    S.push(next_node);// 把next_node压栈
    draw_rectangle(current_node.x, current_node.y, rectangle_high - 3, rectangle_width - 3, 2);
    current_node = next_node;//更新current_node为next_node;
    maze[current_node.x][current_node.y] = 2;//标志当前点为已经被走过
    draw_rectangle(current_node.x, current_node.y, rectangle_high - 3, rectangle_width - 3, 3);
    Sleep(speed);
}

void backward()//后退一步
{
    draw_rectangle(current_node.x, current_node.y, rectangle_high - 3, rectangle_width - 3, 2);
    S.pop();// 弹栈;
    current_node = S.top();// current_node更新为回退的点;
    draw_rectangle(current_node.x, current_node.y, rectangle_high - 3, rectangle_width - 3, 4);
    Sleep(speed);
}

int DFS_maze()//深度优先搜索迷宫的出口
{
    current_node = start_node;
    S.push(current_node);
    maze[current_node.x][current_node.y] = 2;//标记为已经访问的点
    while (!S.empty())//栈不空
    {
        if (has_nextnode())//可以向前走
        {
            forward();//向前走一步;
            if (current_node.x == end_node.x && current_node.y == end_node.y)
                return 1;
        }
        else
        {
            backward();//后退一步    
        }
    }
    if (S.empty())
        return 0;
}

int main()
{
    init_maze();
    init_win();
    draw_maze();
    Sleep(speed);
    DFS_maze();
    display_DFSpath();
    Sleep(10000);
}
原文地址:https://www.cnblogs.com/xxxsans/p/13962116.html