首页 > 其他分享 >栈和队列4 顺序栈的应用实例(迷宫求解)

栈和队列4 顺序栈的应用实例(迷宫求解)

时间:2024-05-26 09:03:11浏览次数:18  
标签:队列 PosType ++ 迷宫 pos else int 实例 printf

#include <stdio.h>
#include <stdlib.h>

#define INITSIZE 100
#define INCREAMENT 10
#define M 8
#define N 8

typedef struct{
    int x;
    int y;
}PosType;

typedef struct{
    int ord;
    PosType seat;
    int di;
}SElemType;

typedef struct SqStack{
    SElemType * data;
    SElemType * top;
    int stacksize;
}SqStack;

void InitStack(SqStack * S){
    S -> data = (SElemType *)malloc(INITSIZE * sizeof(SElemType));
    if (!(S -> data)) exit(0);
    S -> top = S -> data;
    S -> stacksize = INITSIZE;
}

void Push(SqStack * S, SElemType e){
    if(S -> top - S -> data >= S -> stacksize){
        S -> data = (SElemType *)realloc(S -> data, (S -> stacksize + INCREAMENT) * sizeof(SElemType));
        if (!(S -> data)) exit(0);
        S -> top = S -> data + S -> stacksize;
        S -> stacksize = S -> stacksize + INCREAMENT;
    }
    * (S -> top ++) = e;
}

void Pop(SqStack * S, SElemType * e){
    if (S -> data == S -> top) exit(0);
    * e = *(-- S -> top);
}

void GetTop(SqStack S, SElemType * e){
    if (S.data == S.top) exit(0); 
    * e = * (S.top - 1);
}

bool Pass(int a[M + 2][N + 2], PosType pos){
    int x = pos.x;
    int y = pos.y;
    if (a[x][y] == 0)  //可通 
        return true;
    else
        return false;    
}

bool Footprint(int a[M + 2][N + 2], PosType pos){
    int x = pos.x;
    int y = pos.y;
    a[x][y] = 2;  //通过留下的足迹 
}

PosType NextPos(PosType pos, int i){
    PosType nextpos;
    if (i == 1){
        nextpos.x = pos.x;
        nextpos.y = pos.y + 1;
    }
    else if (i == 2){
        nextpos.x = pos.x + 1;
        nextpos.y = pos.y;
    }
    else if (i == 3){
        nextpos.x = pos.x;
        nextpos.y = pos.y -1;
    }
    else if (i == 4){
        nextpos.x = pos.x -1;
        nextpos.y = pos.y;
    }
    return nextpos;
}

void MarkPrint(int a[M + 2][N + 2], PosType pos){
    int x = pos.x;
    int y = pos.y;
    a[x][y] = 3;  //走过,不通 
}

bool MazePath(int a[M + 2][N + 2], PosType start, PosType end){
    SqStack S;
    SElemType e;
    InitStack(&S); 
    PosType curpos = start;
    int curstep = 1;
    do{
        if(Pass(a, curpos)) {
            Footprint(a, curpos);
            e.di = 1;
            e.ord = curstep;
            e.seat = curpos;
            Push(&S, e);
            if(curpos.x == end.x && curpos.y == end.y)
                return true;
            curpos = NextPos(curpos, 1);
            curstep++;
        }
        else{
            if(S.data != S.top){
                Pop(&S, &e);
                while(e.di == 4 && (S.data != S.top)){
                    MarkPrint(a, e.seat);
                    Pop(&S, &e);
                }
                if(e.di < 4){
                    e.di ++;
                    Push(&S, e);
                    curpos = NextPos(e.seat, e.di);            
                }
            }
        }
    }while(S.data != S.top);
    return false;
}


int main(){
    int a[M + 2][N + 2] = {    
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,0,0,1,1,0,0,1},
    {1,0,1,1,1,0,0,0,0,1},
    {1,0,0,0,1,0,0,0,0,1},
    {1,0,1,0,0,0,1,0,0,1},
    {1,0,1,1,1,0,1,1,0,1},
    {1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
    };
    PosType start = {1, 1};
    PosType end = {8, 8};
    bool flag = true;
    flag = MazePath(a, start, end);
    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            if(a[i][j] == 1)
                printf("# "); 
            else if(a[i][j] == 2)
                printf("* ");
            else if(a[i][j] == 3)
                printf("@ ");
            else if(a[i][j] == 0)
                printf("  ");
        }
        printf("\n");
    }
    return 0;
}

为增加趣味性,可以用下面主函数替换(此外还要修改宏定义M,N),生成30*50迷宫。为显示清晰,这里省去了@,不再显示访问过但不在最终路径上的节点。需要特殊说明的是,下面的数据来源于网上的蓝桥杯竞赛。代码中一些特殊处理,是为了将其转化为数组传参,其过程与迷宫求解无关。

int main(){
    int c[M + 2][N + 2] = {0};
    for(int k = 0; k < 52; k++){
        c[0][k] = 1;
        c[31][k] = 1;
    }
    for(int k = 0; k < 32; k++){
        c[k][0] = 1;
        c[k][51] = 1;
    }
    char a[]="010101010010110010010101100101101001000010001010100000100010000010101001000010000000100110011010010101111011010010001000001101001011100011000000010000010000000010101000110100001010000010101010110010110001111100000010100001001010001010000010110000000011001000110101000010101100011010011010101011110111000110110101010010010010100000010001010011100000001010000010100010011010101011111001100001000011101000111000001010100001100010000001000101001100001001110001101000011100100010010101010101010100011010000001000010010000010100101010111010001010101000010111100100101001001000010000010101010100100100010100000000100000001010110011110100011000001010101000111010101001110000100001100001011001111011010000100010101010100001101010100101000010100000111011101001100000001011000100001011001011010010111000000001001010100100000001010010000100010000010001111010100100101001010101101001010100011010101101110000110101110010100001000011000000101001010000010001110000100000100011000011010110100000010010100100100001110110100101000101000000001110110010110101101010100001001010000100001101010100001000100010010001000101011010000100011001000100001010100101010101111101001000000100101000000110010100101001000001000000000010110100000010011101110010010000111010010110111010000000011010001000100010000000100001110100000011001110101000101000100010001111100010101001010000001000100000101001010010101100000001001010100010111010000011110000100001000000011011100000000100000000101110000001100111010111010001000110111010101101111000#";
    char * p = a;
    int b;
    int i = 1, j = 1;
    int m = 0;
    while (*p != '#'){
        if(*p == '0')
            b = 0;
        else
            b = 1;
        m ++;
        if(*p != '#'){
            c[i][j] = b;
            j ++;
            if(j >= 51){
                j = 1;
                i++;
            }
        }
        p ++;
    }
    for(int i = 0; i < 32; i++){
        printf("         ");
        for(int j = 0; j < 52; j++){
            if(c[i][j] == 1)
                printf("# "); 
            else if(c[i][j] == 2)
                printf("o ");
            else if(c[i][j] == 0)
                printf("  ");
        }
        printf("\n");
    }
    printf("\n\n\n\n");
    
    PosType start = {1, 1};
    PosType end = {30, 50};
    bool flag = true;
    flag = MazePath(c, start, end);
    for(int i = 0; i < 32; i++){
        printf("         ");
        for(int j = 0; j < 52; j++){
            if(c[i][j] == 1)
                printf("# "); 
            else if(c[i][j] == 2)
                printf("o ");
            else if(c[i][j] == 3)
                printf("  ");
            else if(c[i][j] == 0)
                printf("  ");
        }
        printf("\n");
    }
    return 0;
}

标签:队列,PosType,++,迷宫,pos,else,int,实例,printf
From: https://blog.csdn.net/l1784312/article/details/139202857

相关文章

  • Django分页操作实例
    分页操作Django提供了一些类实现管理数据分页,这些类位于django/core/paginator.py中Paginator对象Paginator(列表,int):返回分页对象,参数为列表数据,每面数据的条数属性count:对象总数num_pages:页面总数page_range:页码列表,从1开始,例如[1,2,3,4]方法page(num):下标以1开始......
  • uniapp-vue3-oadmin|vite5.x手机后台实例多端仿ios管理系统
    uniapp-vue3-oadmin手机后台实例|vite5.x+uniapp多端仿ios管理系统 原创vue3+uniapp+uni-ui跨端仿ios桌面后台OA管理模板Uni-Vue3-WeOS。uniapp-vue3-os一款基于uni-app+vite5.x+pinia等技术开发的仿ios手机桌面OA管理系统。实现了自定义桌面栅格磁贴布局、多分屏滑动管理、......
  • 244. 高端大气的蛋糕点响应式网页设计实例 大学生期末大作业 Web前端网页制作 html+cs
    目录前言一、网页概述二、网页文件 三、网页效果四、代码展示1.html2.CSS五、总结1.简洁实用2.使用方便3.整体性好4.形象突出5.交互式强六、更多推荐前言高端大气的蛋糕点响应式网页设计实例,应用html+css:Div、导航栏、图片轮翻效果、登录页面等。适用于大......
  • 【数据结构】栈和队列
    个人主页~栈和队列一、栈1、概念2、栈的实现Stack.hStack.ctest.c二、队列1、概念2、队列的实现Queue.hQueue.ctest.c三、深入了解栈和队列的特性1、用队列实现栈2、用栈实现队列3、循环队列一、栈1、概念栈是一种特殊的线性表,只允许在固定的一端进行插入和......
  • 800个程序实例、5万行代码!清华大学出版【Python王者归来】
     Python的丰富模块(module)以及广泛的应用范围,使Python成为当下最重要的计算机语言之一,本书尝试将所有常用模块与应用分门别类组织起来,相信只要读者遵循本书实例,定可以轻松学会Python语法与应用,逐步向Python高手之路迈进,这也是撰写本书的目的。本书以约800个程序实......
  • mysql多实例创建
    mysql数据库(DBMS+数据库)系统:rock8.8mysql:mariabd-server10.3前提:关闭SElinux关闭防火墙时间同步安装mariabdyum-yinstallmariadb-server准备三个实例的目录mkdir-pv/mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}生成数据文件mysql_install_db--user=......
  • Redis Stream消息队列
    工具类部分内容packagecom.hwd.campus.common.redis.utils;importcom.hwd.campus.common.redis.constant.RedisKeyPrefixConst;importcom.hwd.campus.common.redis.service.RedisListSelect;importcom.hwd.campus.common.redis.service.RedisSelect;importlombok.AllA......
  • web前端网页课程设计大作业 html+css+javascript天津旅游(11页) dw静态旅游网页设计实
    ......
  • 数组类型的有界阻塞队列-ArrayBlockingQueue
    一:ArrayBlockingQueue简介  一个由循环数组支持的有界阻塞队列。它的本质是一个基于数组的BlockingQueue的实现。它的容纳大小是固定的。此队列按FIFO(先进先出)原则对元素进行排序。队列的头部是在队列中存在时间最长的元素。队列的尾部是在队列中存在时间最短的元素。......
  • 模板与实例在系统中的应用
    模板与实例在系统中的应用模板是什么模板定义了通用的结构和行为,包含了一系列方法和属性,这些方法和属性为系统提供了参考。实例是什么实例是模板的具体实现,继承了模板的结构和行为,并为模板中的抽象和属性提供了实现。模板是定义系统有什么功能,实例就是这些功能的具体实现。在......