国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C數(shù)據(jù)結(jié)構(gòu)與算法——隊列 應(yīng)用(C語言純享版 迷宮)

這篇具有很好參考價值的文章主要介紹了C數(shù)據(jù)結(jié)構(gòu)與算法——隊列 應(yīng)用(C語言純享版 迷宮)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

實驗任務(wù)

(1) 掌握順序循環(huán)隊列及其C語言的表示;
(2) 掌握入隊、出隊等基本算法的實現(xiàn);
(3) 掌握順序循環(huán)隊列的基本應(yīng)用(求解迷宮通路)。

實驗內(nèi)容

  • 使用C語言實現(xiàn)順序循環(huán)隊列的類型定義與算法函數(shù);
  • 編寫main()函數(shù)并根據(jù)需要修改、補充相關(guān)的類型定義與函數(shù),以實現(xiàn)“求解迷宮通路”問題:
  • 求解迷宮通路問題描述:
    • 給定一個M×N的迷宮圖,指定一個入口與一個出口;
    • 規(guī)定行走規(guī)則為:按“上右下左”優(yōu)先順序向相鄰空位移動1格,用(i,j)表示迷宮中的第i行第j列的一個方塊
    • 在迷宮外圍加上圍墻;
  • 實現(xiàn)指定入口和出口的固定迷宮;
  • 實現(xiàn)隨機入口和出口的固定迷宮;
  • 實現(xiàn)障礙、入口和出口都隨機的迷宮。

實驗源碼

注意:必須在Dos窗口下運行,并且以管理員身份打開Dos窗口最佳

#include <stdio.h>
#include <time.h>
#include "windows.h"

#define MAXSIZE 1000
#define ROW 15
#define LINE 15
#define RATIO 0.6875 // 44/64的比例
#define COORDINATE -1 // 坐標默認 值
#define DISTOP 5 // 迷宮距離頂端距離格數(shù)

#define PASS 0 // 通路
#define WALL 1 // 墻
#define ENTRY 2 // 入口
#define EXIT 3 // 出口
#define DEAD 5 // 死路

// 延時設(shè)置
int walkDelay = 500;
int dirDelay = 1000;

typedef struct Box {
    int x;           // 點的橫坐標(行)
    int y;           // 點的縱坐標(列)
    int pre;         // 上一個點的下標
} Box;

typedef struct {
    Box *base;
    int front;
    int rear;
} SqQueue;

void Map(int map[][LINE]); // 生成地圖

void KnuthShuffle(int map[], int length); // 洗牌算法

void swapInt(int *a, int *b); // 輔助洗牌算法 交換

void PrintMap(int map[][LINE]); // 打印迷宮地圖

boolean InitQueue(SqQueue *queue); // 循環(huán)隊列的初始化

void Walk(SqQueue *queue, int in_x, int in_y, int map[][LINE]); // 移動迷宮

boolean EnQueue(SqQueue *queue, Box node); // 循環(huán)隊列入隊列

boolean IsFull(SqQueue *queue); // 判隊滿

boolean IsEmpty(SqQueue *queue); // 判隊空

Box DeQueue(SqQueue *queue); // 循環(huán)隊列出隊列

void ShowPath(SqQueue *queue, int end); // 求解最短路徑

void Color(short x); // 自定義函根據(jù)參數(shù)改變顏色

void DirTest(int map[][LINE], int dir, int j, int k); // 方向試探

void DeadPath(int j, int k); // 置為死路

void GotoXY(int x, int y); // 將光標移至屏幕 第x列,第y行 處

void DisplayQueue(SqQueue *queue); // 隊列動態(tài)展示

void HideCursor(void); // 隱藏光標

/**
 * <h2>順序隊列實驗</h2>
 * <h3>隨機迷宮問題</h3>
 * <h3>注意:請在Dos窗口下運行</h3>
 * <h4>非循環(huán)隊列,并不是真的退出隊列</h4>
 * @return 0
 */
int main() {

    GotoXY(0, 0);
    Color(9);
    printf("  使用隊列解決迷宮通路問題 \n");
    GotoXY(0, 1);
    printf("==============================\n");
    GotoXY(0, 2);
    Color(12);
    printf("X--走過的無效通路");
    Color(9);
    printf("  囚--起點\n");
    GotoXY(0, 3);
    Color(13);
    printf("O--走過的有效通路");
    Color(11);
    printf("  口--終點\n");
    GotoXY(0, 4);
    printf("------------------------------\n");

    srand(time(NULL));

    int map[ROW][LINE];
    Map(map);
    PrintMap(map);

    SqQueue queue;
    if (!(InitQueue(&queue))) {
        printf("隊列初始化失敗~~\n");
        return 0;
    }

    int in_x, in_y;
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < LINE; j++) {
            if (map[i][j] == ENTRY) {
                in_x = i;
                in_y = j;
            }
        }
    }

    HideCursor();
    DisplayQueue(&queue);
    Walk(&queue, in_x, in_y, map);

    getchar();
}

void Map(int map[][LINE]) {
    int length = (ROW - 2) * (LINE - 2); // 8 * 8 內(nèi)區(qū)域
    int randArr[length];
    for (int i = 0; i < length; i++) {
        if (i == 0) {
            randArr[i++] = ENTRY;
            randArr[i++] = EXIT;
        }
        if (i < (length * RATIO) + 2) {
            randArr[i] = PASS;
        } else {
            randArr[i] = WALL;
        }
    }
    KnuthShuffle(randArr, length); // 打亂 內(nèi)區(qū)域
    // 賦值整張地圖
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < LINE; j++) {
            // 這里一個小技巧:只要前面四個表達式一個為假,說明未到邊界賦值,保證Length不會越界
            if (i != 0 && i != ROW - 1 && j != 0 && j != LINE - 1 && length--) {
                map[i][j] = randArr[length];
            } else {
                map[i][j] = WALL;
            }
        }
    }
}

void KnuthShuffle(int map[], int length) {
    for (int i = length - 1; i >= 1; i--) {
        swapInt(&map[i], &map[rand() % (i + 1)]);
    }
}

void swapInt(int *a, int *b) {
    int t;
    t = *a;
    *a = *b;
    *b = t;
}

void PrintMap(int map[][LINE]) {
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < LINE; j++) {
            GotoXY(j * 2, i + DISTOP);
            switch (map[i][j]) {
                case PASS:
                    printf(" ");
                    break;
                case WALL:
                    Color(10);
                    printf("圍");
                    break;
                case ENTRY:
                    Color(9);
                    printf("囚");
                    break;
                case EXIT:
                    Color(11);
                    printf("口");
                    break;
            }
        }
        printf("\n");
    }
    Sleep(3000);
}

boolean InitQueue(SqQueue *queue) {
    queue->base = (Box *) malloc(sizeof(Box) * MAXSIZE);
    if (!(queue->base)) {
        return FALSE;
    }
    queue->front = queue->rear = 0;
    return TRUE;
}

void Walk(SqQueue *queue, int in_x, int in_y, int map[][LINE]) {
    // 起點先入隊列
    Box node; // 生成當前位置(起點)
    node.x = in_x;
    node.y = in_y;
    node.pre = COORDINATE; // 起點位置下標 -1

    EnQueue(queue, node); // 起點入隊列

    while (!(IsEmpty(queue))) { // 無路可走的情況,回到起點
        node = DeQueue(queue); // 取出隊頭位置 隊頭指針front++

        if (map[node.x][node.y] == EXIT) { // 判斷當前位置是否是終點
            ShowPath(queue, queue->front);
            return;
        }
        int dir; // 裝方向
        Box tNode; // 生成下一個位置
        for (dir = 0; dir < 4; dir++) { // 判斷當前位置各個方向是否可走
            switch (dir) {
                case 0:
                    tNode.x = node.x - 1;
                    tNode.y = node.y;
                    DirTest(map, dir, node.x, node.y);
                    break;
                case 1:
                    tNode.x = node.x;
                    tNode.y = node.y + 1;
                    DirTest(map, dir, node.x, node.y);
                    break;
                case 2:
                    tNode.x = node.x + 1;
                    tNode.y = node.y;
                    DirTest(map, dir, node.x, node.y);
                    break;
                case 3:
                    tNode.x = node.x;
                    tNode.y = node.y - 1;
                    DirTest(map, dir, node.x, node.y);
                    break;
            }
            if (map[tNode.x][tNode.y] == PASS || map[tNode.x][tNode.y] == EXIT) { // 判斷這個方向 是否可走
                tNode.pre = queue->front - 1; // 把節(jié)點位置的下標給 新位置
                EnQueue(queue, tNode); // 入隊
                if (map[tNode.x][tNode.y] == PASS) {
                    map[tNode.x][tNode.y] = DEAD;
                    DeadPath(tNode.x, tNode.y);

                }
            }
        }
    }
    // 這里加二號條件的原因是:此程序使用的是終點出隊列即停止,但是也不排除 到終點即為空
    if (IsEmpty(queue) && map[node.x][node.y] != EXIT) {
        GotoXY(0, ROW + DISTOP + 2);
        Color(12);
        printf("\t無路可走,死翹翹了~~\n");
    }
}

boolean EnQueue(SqQueue *queue, Box node) {
    if (IsFull(queue)) {
        return FALSE;
    }
    queue->base[queue->rear] = node; // 新元素插入隊尾
    queue->rear = queue->rear + 1; // 隊尾指針加 1
    DisplayQueue(queue);
    return TRUE;
}

boolean IsFull(SqQueue *queue) {
    return queue->rear + 1 == queue->front; // 非循環(huán)隊列
}

boolean IsEmpty(SqQueue *queue) {
    return queue->rear == queue->front;
}

Box DeQueue(SqQueue *queue) {
    Box box = queue->base[queue->front++];
    DisplayQueue(queue);
    return box; // 取出隊頭元素,并把其出隊列
}

void ShowPath(SqQueue *queue, int end) {
    int i, tmp;
    for (i = end - 1; i >= 0;) {
        tmp = queue->base[i].pre;
        queue->base[i].pre = COORDINATE;
        i = tmp;
    }
    int count = 0, ti;
    for (i = 1; i < end; i++) { // i = 1, 保證不是終點即可
        if (queue->base[i].pre == COORDINATE) {
            if (count == 0) {
                GotoXY(LINE * 2 + 35, DISTOP - 1);
                printf("↓ 路徑打印 ↓");
                GotoXY(LINE * 2 + 35, DISTOP);
                printf("|__i__j__pre__|");
            }
            count++;
            GotoXY(LINE * 2 + 35, DISTOP + count);
            printf("|_____________|\n");
            Color(11);
            GotoXY(LINE * 2 + 35 + 3, DISTOP + count);
            printf("%d", queue->base[i].x);
            GotoXY(LINE * 2 + 35 + 7, DISTOP + count);
            printf("%d", queue->base[i].y);
            GotoXY(LINE * 2 + 35 + 10, DISTOP + count);
            printf("%d", queue->base[i].pre);
            if (count == 1) {
                ti = i;
                continue;
            }
            GotoXY(queue->base[ti].y * 2, queue->base[ti].x + DISTOP);
            Color(15);
            if (queue->base[i].x - queue->base[ti].x == -1 &&
                queue->base[i].y - queue->base[ti].y == 0) {
                printf("↑");
            } else if (queue->base[i].x - queue->base[ti].x == 0 &&
                       queue->base[i].y - queue->base[ti].y == 1) {
                printf("→");
            } else if (queue->base[i].x - queue->base[ti].x == 1 &&
                       queue->base[i].y - queue->base[ti].y == 0) {
                printf("↓");
            } else {
                printf("←");
            }
            ti = i;
        }
    }
}

void Color(short x) {
    if (x >= 0 && x <= 15) { // 參數(shù)在0-15的范圍顏色
        SetConsoleTextAttribute( // 調(diào)用設(shè)置控制臺文本屬性函數(shù)(調(diào)用獲取句柄函數(shù)(不理解), 不理解)
                GetStdHandle(STD_OUTPUT_HANDLE), x);    // 只有一個參數(shù),改變字體顏色
    } else { // 默認的顏色白色
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    }
}

void DirTest(int map[][LINE], int dir, int j, int k) {
    GotoXY(k * 2, j + DISTOP);
    Color(15);
    switch (dir) {
        case 0:
            printf("↑");
            break;
        case 1:
            printf("→");
            break;
        case 2:
            printf("↓");
            break;
        case 3:
            printf("←");
            break;
    }
    Sleep(dirDelay);
    GotoXY(k * 2, j + DISTOP);
    Color(13);
    switch (map[j][k]) {
        case ENTRY:
            Color(9);
            printf("囚");
            break;
        case DEAD:
            Color(12);
            printf("X");
            break;
    }
}

void DeadPath(int j, int k) {
    GotoXY(k * 2, j + DISTOP);
    Color(12);
    printf("X");
    Sleep(walkDelay);
}

void GotoXY(int x, int y) {
    COORD pos = {x, y}; // 坐標
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 獲取句柄(標準輸出句柄)
    SetConsoleCursorPosition(hOut, pos); // 設(shè)置控制臺光標位置
}

void DisplayQueue(SqQueue *queue) {
    int len = ROW - 1;
    Color(12);
    GotoXY(LINE * 2 + 10, DISTOP);
    printf("|__i__j__di__| <- top");
    for (int j = 1; j <= len; j++) {
        GotoXY(LINE * 2 + 10, DISTOP + j);
        printf("|____________|\n");
    }
    int length = queue->rear;
    for (int i = 0; i < length; i++, len--) {
        if (len == 0) {
            len = ROW - 1;
            for (int j = 1; j <= len; j++) {
                GotoXY(LINE * 2 + 10, DISTOP + j);
                printf("|____________|\n");
            }
        }
        Color(11);
        GotoXY(LINE * 2 + 10 + 3, DISTOP + len);
        printf("%d", queue->base[i].x);
        GotoXY(LINE * 2 + 10 + 7, DISTOP + len);
        printf("%d", queue->base[i].y);
        GotoXY(LINE * 2 + 10 + 10, DISTOP + len);
        printf("%d", queue->base[i].pre);
    }
}

void HideCursor(void) {
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

實驗結(jié)果

C數(shù)據(jù)結(jié)構(gòu)與算法——隊列 應(yīng)用(C語言純享版 迷宮),C,c語言,開發(fā)語言,學習,經(jīng)驗分享,算法,數(shù)據(jù)結(jié)構(gòu)文章來源地址http://www.zghlxwxcb.cn/news/detail-609734.html

到了這里,關(guān)于C數(shù)據(jù)結(jié)構(gòu)與算法——隊列 應(yīng)用(C語言純享版 迷宮)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【腳踢數(shù)據(jù)結(jié)構(gòu)】圖(純享版)

    【腳踢數(shù)據(jù)結(jié)構(gòu)】圖(純享版)

    (??? ),Hello我是 祐言QAQ 我的博客主頁:C/C++語言,Linux基礎(chǔ),ARM開發(fā)板,軟件配置等領(lǐng)域博主?? 快上??,一起學習,讓我們成為一個強大的攻城獅! 送給自己和讀者的一句雞湯??: 集中起來的意志可以擊穿頑石! 作者水平很有限,如果發(fā)現(xiàn)錯誤,可在評論區(qū)指正,感謝

    2024年02月12日
    瀏覽(19)
  • 【數(shù)據(jù)結(jié)構(gòu)與算法分析】使用C語言實現(xiàn)隊列的兩種(帶頭結(jié)點與不帶頭結(jié)點)鏈式存儲,并且給出一種循環(huán)隊列的設(shè)計思想

    【數(shù)據(jù)結(jié)構(gòu)與算法分析】使用C語言實現(xiàn)隊列的兩種(帶頭結(jié)點與不帶頭結(jié)點)鏈式存儲,并且給出一種循環(huán)隊列的設(shè)計思想

    ??當我們編寫程序時,經(jīng)常需要處理各種數(shù)據(jù)結(jié)構(gòu)。隊列是一種常見的數(shù)據(jù)結(jié)構(gòu),它有著廣泛的應(yīng)用場景。隊列的基本操作包括入隊和出隊,應(yīng)用于模擬等待隊列、消息隊列、計算機緩存等場合。 ??在實際編程中,我們可以用不同的數(shù)據(jù)結(jié)構(gòu)來實現(xiàn)隊列。本文主要介紹了

    2024年02月08日
    瀏覽(503)
  • 《數(shù)據(jù)結(jié)構(gòu)、算法與應(yīng)用C++語言描述》-列車車廂重排問題

    《數(shù)據(jù)結(jié)構(gòu)、算法與應(yīng)用C++語言描述》-列車車廂重排問題

    完整可編譯運行代碼見:Github::Data-Structures-Algorithms-and-Applications/_10Train_carriages_rearrangement/ 一列貨運列車有 n 節(jié)車廂,每節(jié)車廂要??吭诓煌能囌?。假設(shè) n個車站從 1 到n 編號,而且貨運列車按照從n到1的順序經(jīng)過車站。車廂的編號與它們要停靠的車站編號相同。為了便于從

    2024年04月10日
    瀏覽(30)
  • 【數(shù)據(jù)結(jié)構(gòu)和算法】--隊列的特殊結(jié)構(gòu)-循環(huán)隊列

    【數(shù)據(jù)結(jié)構(gòu)和算法】--隊列的特殊結(jié)構(gòu)-循環(huán)隊列

    循環(huán)隊列是隊列的一種特殊結(jié)構(gòu),它的 長度是固定的 k ,同樣是 先進先出 ,理論結(jié)構(gòu)是 首尾相連的環(huán)形循環(huán)結(jié)構(gòu) 。其理論結(jié)構(gòu)大致如下: 具體結(jié)構(gòu)描述可以參考 LeetCode : 622. 設(shè)計循環(huán)隊列的題目要求,大致如下: 設(shè)計你的循環(huán)隊列實現(xiàn)。 循環(huán)隊列是一種 線性數(shù)據(jù)結(jié)構(gòu) ,

    2024年02月04日
    瀏覽(16)
  • 數(shù)據(jù)結(jié)構(gòu)——隊列(C語言)

    數(shù)據(jù)結(jié)構(gòu)——隊列(C語言)

    本篇文章將解決一下幾個問題: 隊列是什么? 如何實現(xiàn)一個隊列? 什么場景下會用隊列? 隊列:一種只允許一端進行插入數(shù)據(jù)操作,在另一端進行刪除操作的特殊線性表。隊列具有先進先出(FIFO)入隊列:進行插入操作的一端稱為隊尾,出隊列的一端叫做隊頭。 ?隊列也

    2024年02月11日
    瀏覽(19)
  • 數(shù)據(jù)結(jié)構(gòu)學習記錄——圖應(yīng)用實例-拯救007(問題描述、解題思路、偽代碼解讀、C語言算法實現(xiàn))

    數(shù)據(jù)結(jié)構(gòu)學習記錄——圖應(yīng)用實例-拯救007(問題描述、解題思路、偽代碼解讀、C語言算法實現(xiàn))

    目錄 問題描述? 解題思路 偽代碼? 總體算法 DFS算法 偽代碼解讀 總體算法 DFS算法 具體實現(xiàn)(C語言) 在老電影“007之生死關(guān)頭”(Live and Let Die)中有一個情節(jié),007被毒販抓到一個鱷魚池中心的小島上,他用了一種極為大膽的方法逃脫 —— 直接踩著池子里一系列鱷魚的大腦

    2024年02月05日
    瀏覽(82)
  • 【頭歌】數(shù)據(jù)結(jié)構(gòu)-隊列的應(yīng)用

    【頭歌】數(shù)據(jù)結(jié)構(gòu)-隊列的應(yīng)用

    ? 第1關(guān):循環(huán)隊列 任務(wù)描述 本關(guān)任務(wù):編寫一個循環(huán)隊列,實現(xiàn)入隊、出隊操作,判斷隊空、隊滿等特殊情況。 相關(guān)知識 為了完成本關(guān)任務(wù),你需要掌握:1.循環(huán)隊列定義,2.入隊、出隊的定義,3.隊空、隊滿的情況。 循環(huán)隊列定義 循環(huán)隊列將數(shù)組存儲區(qū)看成是一個首尾相

    2024年02月08日
    瀏覽(62)
  • 隊列——“數(shù)據(jù)結(jié)構(gòu)與算法”

    隊列——“數(shù)據(jù)結(jié)構(gòu)與算法”

    各位CSDN的uu們你們好呀,又好久不見啦,最近有點擺爛,甚是慚愧?。。?!今天,小雅蘭的內(nèi)容是隊列,下面,讓我們進入隊列的世界吧?。?! 隊列 隊列的概念及結(jié)構(gòu) 隊列:只允許在一端進行插入數(shù)據(jù)操作,在另一端進行刪除數(shù)據(jù)操作的特殊線性表,隊列具有先進先出FIF

    2024年02月06日
    瀏覽(23)
  • 算法與數(shù)據(jù)結(jié)構(gòu)-隊列

    算法與數(shù)據(jù)結(jié)構(gòu)-隊列

    ??隊列跟棧一樣,也是一種操作受限的線性表數(shù)據(jù)結(jié)構(gòu)。不過,隊列是先進者先出。 ??棧只支持兩個基本操作:入棧 push()和出棧 pop()。隊列跟棧非常相似,支持的操作也很有限,最基本的操作也是兩個:入隊 enqueue(),放一個數(shù)據(jù)到隊列尾部;出隊 dequeue(),從隊列頭部取

    2024年02月12日
    瀏覽(21)
  • 數(shù)據(jù)結(jié)構(gòu)與算法:隊列

    數(shù)據(jù)結(jié)構(gòu)與算法:隊列

    在上篇文章講解了棧之后,本篇也對這一章進行收尾,來到隊列! 隊列(Queue)就像是排隊買票的人群。想象一下你去電影院看電影,人們在售票窗口形成一條線(隊列)等待購票。隊列遵循一個很重要的原則:先來先服務(wù)(First In, First Out,簡稱FIFO)。這意味著最先到達并

    2024年02月22日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包