C語(yǔ)言通過(guò)棧實(shí)現(xiàn)小人走迷宮
本文實(shí)例為大家分享了C語(yǔ)言通過(guò)棧實(shí)現(xiàn)小人走迷宮的具體代碼,供大家參考,具體內(nèi)容如下
新建stack.h
#include "Data.h" #ifndef _STACK_H #define _STACK_H #define INIT_SIZE 10 #define INIT_INCREM 10 typedef struct _STACK{ ?? ?ElemType *Base; ?? ?ElemType *Top; ?? ?int size; } STACK; STACK* InitStack(); void DestroyStack(STACK* s); //壓棧 int Push(STACK* s, ElemType *e); //彈棧 int Pop(STACK* s, ElemType* e); //站是否為空 int IsEmpty(STACK* ?s); #endif;
新建stack.c
#include "stack.h" #include<stdlib.h> STACK* InitStack(){ ?? ?STACK* s = (STACK*)malloc(sizeof(STACK)); ?? ?if (s == NULL){ ?? ??? ?exit(0); ?? ?} ?? ?s->Base = (ElemType*)malloc(INIT_SIZE*sizeof(ElemType)); ?? ?if (s->Base == NULL){ ?? ??? ?free(s->Base); ?? ??? ?free(s); ?? ??? ?exit(0); ?? ?} ?? ?s->Top = s->Base; ?? ?s->size = INIT_SIZE; ?? ?return s; } void DestroyStack(STACK* s){ ?? ?free(s->Base); ?? ?free(s); } int Push(STACK* s, ElemType *e){ ?? ?if (s == NULL || e==NULL){ ?? ??? ?return 0; ?? ?} ?? ?if (s->Top - s->Base >= s->size){ ?? ??? ?s->Base = (ElemType*)realloc(s->Base, (s->size + INIT_INCREM)*sizeof(ElemType)); ?? ??? ?if (s->Base == NULL){ ?? ??? ??? ?return 0; ?? ??? ?} ?? ??? ?s->Top = s->Base + s->size; ?? ??? ?s->size = s->size + INIT_INCREM; ?? ? ?? ?} ?? ? ?? ?*s->Top = *e; ?? ?s->Top++; ?? ?return 1; } int Pop(STACK* s, ElemType* e){ ?? ?if (s == NULL || e==NULL){ ?? ??? ?return 0; ?? ?} ?? ?if (s->Base == s->Top){ ?? ??? ?return 0; ?? ?} ?? ?s->Top--; ?? ?*e = *s->Top; ?? ?return 1; } int IsEmpty(STACK* ?s){ ?? ?return s->Base == s->Top ? 1 : 0; }
新建Data.h
#ifndef _DATA_H #define _DATA_H ?? ?typedef struct ?? ?{ ?? ??? ?int y; ?? ??? ?int x; ?? ?}POS; ?? ?typedef struct{ ?? ??? ?int ord; ?? ??? ?POS seat; ?? ??? ?int di; ?? ?}ElemType; #endif
新建main.c
#include "Data.h" #include "stack.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> ?int item[10][10]={ ?? ?{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} }; static const POS inPos={1,1},outPos={8,8}; int IsPass(POS CurP){ ?? ?return item[CurP.y][CurP.x]==0?1:0; } POS NextPos(POS CurP,int di){ ?? ?POS p=CurP; ?? ?switch(di){ ?? ??? ?case 0: ?? ??? ??? ?p.x--;//向左 ?? ??? ??? ?break; ?? ??? ?case 1: ?? ??? ??? ?p.y++;//向下 ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?p.x++;//向右 ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?p.y--;//向上 ?? ??? ??? ?break; ?? ?} ?? ?return p; } void PrintItem(POS CurP){ ?? ?int i,j; ?? ?system("cls"); ?? ?for(i=0;i<10;i++){ ?? ??? ?for(j=0;j<10;j++){ ?? ??? ??? ?if(i==CurP.y && j==CurP.x){ ?? ??? ??? ??? ?printf("@"); ?? ??? ??? ??? ?continue; ?? ??? ??? ?} ?? ??? ??? ?if(item[i][j]==1){ ?? ??? ??? ??? ?printf("*"); ?? ??? ??? ?}else{ ?? ??? ??? ??? ?printf(" "); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?printf("\n"); ?? ?} } void main(){ ?? ?STACK* s=InitStack(); ?? ?ElemType e; ?? ?int setp=1; ?? ?POS CurPos=inPos; ?? ?PrintItem(inPos); ?? ?do{ ?? ??? ?if(IsPass(CurPos)){ ?? ??? ??? ?e.ord=setp; ?? ??? ??? ?e.di=0; ?? ??? ??? ?e.seat=CurPos; ?? ??? ??? ?Push(s,&e);//只有能通過(guò)才壓棧 ?? ??? ??? ?item[CurPos.y][CurPos.x]=2; ?? ??? ??? ?if(CurPos.y==outPos.y && CurPos.x==outPos.x){ ?? ??? ??? ??? ? ?? ??? ??? ??? ?PrintItem(CurPos); ?? ??? ??? ??? ?printf("ok!\n"); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?PrintItem(CurPos); ?? ??? ??? ?CurPos=NextPos(e.seat,0); ?? ??? ??? ?setp++; ?? ??? ??? ?getch(); ?? ??? ?}else{ ?? ??? ??? ?Pop(s,&e);//如果不能通過(guò)就彈棧 ?? ??? ??? ?if(e.di==4 && !IsEmpty(s)){ ?? ??? ??? ??? ??? ?item[CurPos.y][CurPos.x]=8; ?? ??? ??? ??? ??? ?Pop(s,&e); ?? ??? ??? ?} ?? ??? ??? ?if(e.di<3){ ?? ??? ??? ??? ?e.di++; ?? ??? ??? ??? ?Push(s,&e); ?? ??? ??? ??? ?CurPos=NextPos(e.seat,e.di); ?? ??? ??? ?} ?? ??? ?} ?? ?}while(!IsEmpty(s)); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++數(shù)組模擬之單鏈表與雙鏈表和棧和隊(duì)列的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了C++數(shù)組模擬之單鏈表與雙鏈表和棧和隊(duì)列的實(shí)現(xiàn)過(guò)程,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的,跟隨下文來(lái)具體了解吧2023-02-02Qt跨平臺(tái)窗口選擇功能的實(shí)現(xiàn)過(guò)程
很多時(shí)候?yàn)榱朔奖丬浖氖褂?我們需要讓編寫(xiě)的界面程序顯示在最上層,這時(shí)候就需要對(duì)窗口屬性進(jìn)行調(diào)整,下面這篇文章主要給大家介紹了關(guān)于Qt跨平臺(tái)窗口選擇功能的實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2022-12-12C++?數(shù)據(jù)結(jié)構(gòu)超詳細(xì)講解順序表
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示2022-03-03C++深入探索類和對(duì)象之封裝及class與struct的區(qū)別
C++?類與對(duì)象涉及的知識(shí)點(diǎn)非常廣泛,所以我準(zhǔn)備寫(xiě)成幾個(gè)特定的部分來(lái)作為博文分享,這次的blog將詳細(xì)講解類的屬性、行為、訪問(wèn)權(quán)限,class與struct的區(qū)別以及具體案例,希望能夠?qū)δ銈冇袔椭?,解決入門(mén)小白或者對(duì)這方面了解不多的朋友們,那么接下來(lái)開(kāi)始今天的內(nèi)容2022-05-05詳解C++編程中的條件判斷語(yǔ)句if-else與switch的用法
這篇文章主要介紹了C++編程中的條件判斷語(yǔ)句if-else與switch的用法,是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01