最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)迷宮實(shí)驗(yàn)

 更新時(shí)間:2020年06月16日 11:44:46   作者:燃燒的鑰匙  
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)迷宮實(shí)驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)簡單的數(shù)據(jù)結(jié)構(gòu)迷宮實(shí)驗(yàn),供大家參考,具體內(nèi)容如下

分析:迷宮實(shí)驗(yàn)主要有兩部分操作,其一是對迷宮的生成,其二是尋路使用棧的操作。

步驟:

一、.h文件

1、首先是迷宮的生成,可以使用隨機(jī)數(shù)種子生成,但主要邏輯部分并不在此,所以在這里直接寫死,固定下來。
定義一個(gè)坐標(biāo)類型的結(jié)構(gòu)體,和二維數(shù)組迷宮:

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

//迷宮類型
typedef struct {
 int square[10][10] = 
 {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,0,1,1,1,0,1},
{1,0,0,0,0,1,0,1,0,1},
{1,0,1,1,1,1,0,1,1,1},
{1,0,0,0,0,1,0,0,0,1},
{1,0,1,1,0,0,0,1,0,1},
{1,0,1,1,1,0,1,1,1,1},
{1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
 };
}Maze;
typedef Pos SElemType;

2、然后是對棧的聲明,棧里儲存的元素為坐標(biāo)類型

//順序棧
#define MAXSIZE 50
typedef struct {
 SElemType *base;
 SElemType *top; //棧頂指針
 int stacksize;
}SqStack;

3、棧操作函數(shù)聲明

typedef int Status;
#define OK 1;
#define ERROR 0;

//棧的相關(guān)操作
//初始化棧
Status initStack(SqStack &s);
//壓棧
Status push(SqStack &s, SElemType e);
//出棧
SElemType pop(SqStack &s);
//清空棧
Status clearStack(SqStack &s);
//摧毀棧
void destroyStack(SqStack &s);
//遍歷棧
Status stackTravel(SqStack s);

4、迷宮操作函數(shù)聲明

//初始化迷宮(同時(shí)生成起始點(diǎn)和終點(diǎn))
void initMaze(Maze &maze);
//打印迷宮
void showMaze(Maze maze);
//尋找出路;傳入一個(gè)迷宮和棧找出出路
void findWay(Maze &maze,SqStack &s);
//判斷該點(diǎn)的四個(gè)方向是否有通路,有就前進(jìn)
Pos isExit(Pos p, Maze maze);

二、.cpp文件

1、導(dǎo)入所需頭文件

#include "pch.h"
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;

2、棧操作實(shí)現(xiàn)

//構(gòu)造空棧
Status initStack(SqStack &s) {
 s.base = new SElemType[MAXSIZE];
 if (!s.base)
 {
 exit(OVERFLOW);//分配失敗
 }
 s.top = s.base;
 s.stacksize = MAXSIZE;
 return OK;
}

//入棧
Status push(SqStack &s, SElemType e) {
 //判斷棧滿
 if (s.top-s.base == s.stacksize)
 {
 return ERROR;
 }
 //存入元素,*為取指針的值
 s.top++;
 *s.top = e;
 return OK;
}

//出棧,用e返回棧頂值
SElemType pop(SqStack &s) {
 SElemType e;
 //判斷棧為空
 if (s.top == s.base)
 {
 //若為空則返回一個(gè)(-1,-1)的點(diǎn),判斷由外部調(diào)用時(shí)進(jìn)行
 e.x = -1;
 e.y = -1;
 return e;
 }
 e = *s.top;
 s.top--;
 return e;
}

Status clearStack(SqStack &s) {
 s.top = s.base;
 return OK;
}

void destroyStack(SqStack &s) {
 s.top = NULL;
 s.stacksize = 0;
 free(s.base);
}

Status stackTravel(SqStack s) {
 while (s.top != s.base)
 {
 s.base++;
 Pos p = *s.base;
 //輸出走過的路徑
 cout <<"("<<p.x<<","<<p.y<<")"<< "-->";
 if ( p.x == 0 || p.y == 0|| p.x == 9 ||p.y == 9)
 {
 //終點(diǎn)輸出為“End”
 cout << "End";
 }
 }
 cout << endl;
 return 0;
}

3、迷宮操作實(shí)現(xiàn)

///////////////////////////////////////迷宮操作////////////////////////////////
//初始化函數(shù),傳入一個(gè)迷宮,隨機(jī)生成起點(diǎn)和終點(diǎn),由于起點(diǎn)有一定限制,所以這里起點(diǎn)也固定為幾個(gè)最合適的點(diǎn)
void initMaze(Maze &maze) {
 //生成隨機(jī)數(shù)
 srand((unsigned)time(NULL));
 int index = rand() % 36 + 1;
 int start = index % 6 + 1;
 //生成起始點(diǎn)數(shù)值為‘s'
 switch (start)
 {
 case 1:
 maze.square[1][1] = 's';
 break;
 case 2:
 maze.square[3][8] = 's';
 break;
 case 3:
 maze.square[3][6] = 's';
 break;
 case 4:
 maze.square[6][8] = 's';
 break;
 case 5:
 maze.square[8][3] = 's';
 break;
 case 6:
 maze.square[8][8] = 's';
 break;
 }
 //隨機(jī)生成終點(diǎn)'e'表示
 while (index = rand()%36+1)
 {
 //出口在頂部
 if (index >1 &&index<10 && maze.square[1][index-1]!='s')
 {
 maze.square[0][index-1] = 'e';
 break;
 }
 //出口在右側(cè)
 else if (index>10 &&index <19)
 {
 if (maze.square[index-10][8] != 1 && maze.square[index-10][8]!='s') {
 maze.square[index-10][9] = 'e';
 break;
 }
 }
 //底部出口
 else if (index >19&&index<28)
 {
 if (maze.square[8][index - 19] != 's' && maze.square[8][index - 19] != 1) {
 maze.square[9][index - 19] = 'e';
 break;
 }
 }
 //左側(cè)出口
 else if (index >28 && index <=36)
 {
 if (maze.square[index-28][1] != 1 &&maze.square[index-28][1] != 's')
 {
 maze.square[index - 28][0] = 'e';
 break;
 }
 }
 }
}

void showMaze(Maze maze) {
 for (int i = 0; i < 10; i++)
 {
 for (int j = 0; j < 10; j++)
 {
 if (maze.square[i][j] == 1)
 {
 cout << "* ";
 }
 else if (maze.square[i][j] == 0)
 {
 cout << " ";
 }
 else
 {
 cout << (char)maze.square[i][j]<<" ";
 }
 }
 cout << endl;
 }
}
//尋找迷宮路徑
void findWay(Maze &maze,SqStack &s) {
//首先遍歷找出起始點(diǎn)和終點(diǎn)并保存下來
 Pos start,end;
 for (int i = 0; i < 10; i++)
 {
 for (int j = 0; j < 10; j++) {
 if (maze.square[i][j] == 's')
 { //起點(diǎn)壓入棧內(nèi)
 start.x = i;
 start.y = j;
 push(s, start);
 }
 else if (maze.square[i][j] == 'e')
 { //出口
 end.x = i;
 end.y = j;
 }
 }
 }
 //尋找路徑
 Pos go = start;
 //直到找到出口才結(jié)束
 while ( s.top->x != end.x || s.top->y != end.y)
 {
 //獲得下一步坐標(biāo)
 Pos path = isExit(go, maze);
 if (path.x != go.x || path.y != go.y)
 {
 //前進(jìn)
 maze.square[path.x][path.y] = 'p';
 push(s, path);
 go = path;
 }
 //如果所有放向都走不通(即返回的點(diǎn)是傳入的點(diǎn)),則將其標(biāo)為“@”,出棧到上一個(gè)點(diǎn),繼續(xù)判斷
 else
 {
 //走不通pop
 maze.square[path.x][path.y] = '@';
 pop(s);
 go = *s.top;
 }
 }
 maze.square[end.x][end.y] = 'e';
}

//判斷返回下一步路徑(順序:右下左上),傳入所處位置,從右邊開始判斷是否又通路或者出口,有就返回哪個(gè)方向上的點(diǎn)
Pos isExit(Pos p,Maze maze) {
 Pos tempP = p;
if (maze.square[tempP.x][tempP.y+1] == 0 || maze.square[tempP.x][tempP.y + 1] == 'e')
 {
 tempP.y++;
 }
 else if(maze.square[tempP.x+1][tempP.y] == 0 || maze.square[tempP.x +1][tempP.y] == 'e')
 {
 tempP.x++;
 }
 else if (maze.square[tempP.x][tempP.y - 1] == 0 || maze.square[tempP.x][tempP.y - 1] == 'e')
 {
 tempP.y--;
 }
 else if (maze.square[tempP.x - 1][tempP.y] == 0 || maze.square[tempP.x - 1][tempP.y] == 'e')
 {
 tempP.x--;
 }
 return tempP;
}

三、main函數(shù)調(diào)用

int main()
{
 while (true)
 {
 //創(chuàng)建一個(gè)迷宮
 Maze maze;
 initMaze(maze);
 //初始化一個(gè)棧
 SqStack S;
 initStack(S);
 cout << "*****************************" << endl;
 cout << "* 1、生成迷宮 2、退出 *" << endl;
 cout << "*****************************" << endl;
 cout << "請輸入你的選擇:";
 int select = 0;
 cin >> select;
 if (select == 1)
 {
 cout << "生成隨機(jī)起點(diǎn)和出口迷宮:" << endl;
 showMaze(maze);
 cout << "生成迷宮路徑:" << endl;
 findWay(maze, S);
 stackTravel(S);
 showMaze(maze);
 cout << endl;
 }
 if (select == 2)
 {
 clearStack(S);
 break;
 }
 }
 return 0;
}

四、評價(jià)

這是個(gè)叫簡易的迷宮,但基本實(shí)現(xiàn)了迷宮的尋路邏輯,可改進(jìn)的地方有:

1、因?yàn)楹芏嗟胤綄懰懒耍詮?fù)用性不高,可以用循環(huán)遍歷來隨機(jī)生成起點(diǎn),同理迷宮的生成也是這樣
2、判斷路徑可以用遞歸調(diào)用實(shí)現(xiàn)前進(jìn)邏輯

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++11的for循環(huán)的新用法(推薦)

    C++11的for循環(huán)的新用法(推薦)

    C++11這次的更新帶來了令很多C++程序員期待已久的for range循環(huán),每次看到j(luò)avascript, lua里的for range,心想要是C++能有多好,心里別提多酸了。這次C++11不負(fù)眾望,再也不用羨慕別家人的for range了。下面看下C++11的for循環(huán)的新用法
    2021-11-11
  • C++實(shí)現(xiàn)LeetCode(114.將二叉樹展開成鏈表)

    C++實(shí)現(xiàn)LeetCode(114.將二叉樹展開成鏈表)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(114.將二叉樹展開成鏈表),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 詳解C++調(diào)用Python腳本中的函數(shù)的實(shí)例代碼

    詳解C++調(diào)用Python腳本中的函數(shù)的實(shí)例代碼

    這篇文章主要介紹了C++調(diào)用Python腳本中的函數(shù) ,需要的朋友可以參考下
    2018-11-11
  • C++?Boost?Spirit精通教程

    C++?Boost?Spirit精通教程

    Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱
    2022-11-11
  • C++與C#互調(diào)dll的實(shí)現(xiàn)步驟

    C++與C#互調(diào)dll的實(shí)現(xiàn)步驟

    這篇文章主要介紹了C++與C#互調(diào)dll的實(shí)現(xiàn)步驟,dll動(dòng)態(tài)鏈接庫的共享在一些大型項(xiàng)目中有一定的應(yīng)用價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C/C++實(shí)現(xiàn)三路快速排序算法原理

    C/C++實(shí)現(xiàn)三路快速排序算法原理

    這篇文章主要為大家詳細(xì)介紹了C/C++實(shí)現(xiàn)三路快速排序算法原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C++實(shí)現(xiàn)多線程查找文件實(shí)例

    C++實(shí)現(xiàn)多線程查找文件實(shí)例

    這篇文章主要介紹了C++實(shí)現(xiàn)多線程查找文件實(shí)例,對于深入學(xué)習(xí)C++程序設(shè)計(jì)有著很好的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C++另辟蹊徑計(jì)算1到n的和

    C++另辟蹊徑計(jì)算1到n的和

    從1加到100,高斯的故事,我們學(xué)過。今天,我們寫一個(gè)程序來試試。首先,用笨方法。一個(gè)數(shù)一個(gè)數(shù)的加,我們一般人就是這樣干的嗎。在計(jì)算機(jī)程序里面,怎么辦呢?1我們把求和的功能寫成一個(gè)可以針對不同的N運(yùn)用的,C++里面叫函數(shù)
    2023-02-02
  • C++中友元函數(shù)(friend)解析

    C++中友元函數(shù)(friend)解析

    這篇文章主要分享了C++友元函數(shù)講解,C++提供了一種形式的訪問權(quán)限,叫做友元,友元有三種,分別是友元函數(shù)、友元類和友元成員函數(shù),下面將詳細(xì)介紹該內(nèi)容,需要的小伙伴可以參考一下
    2022-01-01
  • C基礎(chǔ) redis緩存訪問詳解

    C基礎(chǔ) redis緩存訪問詳解

    下面小編就為大家?guī)硪黄狢基礎(chǔ) redis緩存訪問詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06

最新評論

泰兴市| 苗栗县| 新沂市| 阿城市| 天峨县| 油尖旺区| 茌平县| 乐业县| 丹寨县| 双鸭山市| 淮滨县| 岳阳市| 益阳市| 邵阳市| 惠来县| 鲁山县| 固安县| 和平区| 平远县| 安远县| 修武县| 将乐县| 南皮县| 蕉岭县| 调兵山市| 广宗县| 白水县| 太康县| 新竹市| 潢川县| 阜康市| 石阡县| 太谷县| 永城市| 临潭县| 资阳市| 车致| 西城区| 武义县| 泽库县| 娱乐|