日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构课程设计,迷宫问题求解

發布時間:2023/12/9 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构课程设计,迷宫问题求解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構課程設計(C語言編寫),迷宮問題求解,要求找到出路,并要求獲取路徑和方向。

一、設計思路:
1.物理存儲結構:

① 迷宮數據存儲于一個結構體中,數據值放置于二維數組中用0表示通路,數據1表示障礙,走過的路變為-1,行列數分別放于不同的整數。
② 路徑存儲于一個棧中,最好采用順序棧存儲,采用鏈棧,隊列也可,各有好處,本人選擇鏈棧,減少產生溢出情況。
③ 將路徑的點,路徑的方向,存放于一個結構體中。

2.邏輯算法:
① 迷宮數組的遍歷操作與棧的操作相結合。
② 從入口開始,尋找下一個可同行點,只要可同行,將此刻的方向,坐標進棧,存儲信息。
③ 設計循環結構,判斷四周是否有路。
While(1){
If(){……}
else if(){……}
else if(){……}
else if(){……}
else{……}
}
先判斷四個方向是否為0(此處包括-1走過的路),若都不為零,則出棧,出棧一次后,再取棧頂元素,相當于退后了一步,記住,出棧必須判斷是否為空,不然回到原點時,若其他方向有出路,會出現報錯情況。
二、技巧:
由于數組可能會產生越界問題,故此得強行設定入口,讓編寫的迷宮比用戶輸入的多出四周圍墻,這樣對迷宮進行數組和棧的操作時,不會出現越界問題。 用戶友好型:為了給廣大用戶增加樂趣,本人設定的迷宮操作,有初始化截面,菜單選擇,并且在數字迷宮外,設定了動態迷宮的版本,根據動態迷宮,用戶可通過操作方向來確定要走的方向,大大增強了迷宮的交互性和友好性。

三、擴展之處:
設定動態迷宮,增強交互性能。拓展多個模式的用戶選擇。由于用戶按鍵的不確定性,防止用戶操作失誤,設定按鍵失誤操作,可選擇是否退出。在用戶進行游戲的同時,進行清屏,不然會造成亂糟糟一團,影響用戶體驗。

#include<stdio.h> #include<malloc.h> #include<time.h> #include<stdlib.h> #include<windows.h> #include<conio.h> typedef int datatype;//位置 typedef struct drect {int x, y;int dir; }drect;//棧 typedef struct node {struct drect data;struct node* next; }node;//初始化一個棧 node* first() {node* p = (node*)malloc(sizeof(node));p->next= NULL;return p; }//進棧 void push(node* s, int x, int y, int dir) {node* p =(node*)malloc(sizeof(node));p->data.x = x;p->data.y = y;p->data.dir = dir;p->next = s->next;s->next = p;//return s; }//出棧 node* output(node* s, int x, int y, int dir) {if (s== NULL)return 0;node* p ;p = s->next;s->next = p->next;return p; }//創建地圖 void creatmap(int a[][100], int x, int y) {int k;srand(time(0));for (int i = 0; i < x; i++) {for (int j = 0; j < y; j++) {if (i == 0 || i == x - 1) {a[i][j] = 1;}if (j == 0 || j == y - 1) {a[i][j] = 1;}else if ((i != 0 && i != x - 1) && (j != 0 && j != y - 1)) {a[i][j] = 0;}}}for (int i = 1; i < x - 1; i++) {for (int j = 1; j < y - 1; j++) {if (j < y / 3) {//規范隨機數個數產生數量k = rand() % (y - 1);a[i][k] = 1;}}} }//尋找出路 //right 1 under 2 left 3 on 4 int find(node*s , int a[][100], int w, int h, int x1, int y1, int x2, int y2, int x, int y) {node* p;int dir;// = 1;push(s, 1, 0, 1);while ((x!=x2||y!=y2)&& (x != x1 || y != y1)){if ( a[x][y + 1] == 0) {a[x][y] = -1;dir = 1;push(s, x, y, dir); y++;}else if ( a[x + 1][y] == 0) {a[x][y] = -1;dir = 2;push(s, x, y, dir);x++;}else if ( a[x][y - 1] == 0) {a[x][y] = -1;dir = 3;push(s, x, y, dir);y--;}else if ( a[x - 1][y] == 0) {a[x][y] = -1;dir = 4;push(s, x, y, dir);x--;}else{a[x][y] = -1;p = output(s, x, y, dir);if (p) {x = p->data.x;y = p->data.y;dir = p->data.dir;free(p);}}}if (x == x2 && y == y2) {push(s, x, y, 0);return 1;}return 0;}void print_map(int a[][100],int w,int h) {printf("迷宮為:\n");for (int i = 0; i < w + 2; i++) {for (int j = 0; j < h + 2; j++) {a[1][0] = 0;a[1][1] = 0;a[w + 1][h] = 0;printf("%d ", a[i][j]);}printf("\n");} }void print_foot(node* s,int i) {if (i) {printf("迷宮為:\n");while (s->next != NULL) {s = s->next;int a = s->data.x;int b = s->data.y;int c = s->data.dir;printf("(%d,%d)->%d\n", a, b, c);}}elseprintf("沒有出口!\n"); }///以下為動態迷宮游戲!!int find_run_foot(char hyb[][200]) {int x = 1, y = 1;char ch;//hyb[1]=" ";for (int i = 0; i <= 20; i++){puts(hyb[i]);}hyb[1][0] = ' ';while (1){ch = _getch();if (ch == 's') {if (hyb[x + 1][y] == ' ') {hyb[x][y] = ' ';x++;hyb[x][y] = 'o';}}else if (ch == 'w') {if (hyb[x - 1][y] == ' ') {hyb[x][y] = ' ';x--;hyb[x][y] = 'o';}}else if (ch == 'a') {if (hyb[x][y - 1] == ' ') {hyb[x][y] = ' ';y--;hyb[x][y] = 'o';}}else if (ch == 'd'){if (hyb[x][y + 1] == ' ') {hyb[x][y] = ' ';y++;hyb[x][y] = 'o';}}else {system("cls");printf("其他按鍵會退出游戲,你是否繼續?1-是,其他-否\n");int dle;scanf_s("%d", &dle);system("cls");if (dle == 1) {break;}else{system("cls");for (int i = 0; i <= 20; i++){puts(hyb[i]);}continue;}}system("cls");for (int i = 0; i <= 20; i++){puts(hyb[i]);}if (x == 19 && y == 27){break;}}system("cls");Sleep(1000);printf("游戲結束!\n");system("cls");return 0; }int run_foot(int slect_slect) {char hyb1[200][200] = {"############################","o ### ## ### ## ###########","## #### #########","### #### ## ###### ### ##","### ### ######### ### ## ##","### ###### ### ## ##","### ## #### ###### ## ##","# ## #### ########### ##","# #### ## ###### ##","### ### ## ##### #######","### ###### ### # ##","# ###### #### ###### ##","# # ###### ####### #### # ##","# # ## ## #### # ##","# ##### ## #### ## ##","# ## ## #### ###### # ##","#### ## ## ## #### # ##","# ## ## ## ## # # #","# ##### ## ##### ## # # ####","# # ###### # # ","############################",};char hyb2[200][200] = {"############################################################################","o # ## ### # #### ##### ###### #### ####### ##### #### ## ### ######","## ## #### # ### # ##### #### ## ### #######","##### ## ### ### ##### ### ## ## ### # ## ####### ##### #### ## ### ### #","## # ### ##### ### ## ## ### # ## ## #### ## ### ### ##","## ## ## # # ### ##### # #### ## ### # # ## #### ### # ## ## ### ### ###","##### ## # # ##### ### ## ## # ## ## ## ## ## ### ## ### ###","## ## ## ### # # ## # ## ### #### ## #### ### #### ## # # ### ###","## ## ## ### # # ## ## ### # #### ### ## #### ## # # ####","## ##### ### # # ## ## # # ###### #### # ### ## #### ## # #####","## ## ## ### # ## # ## ###### #### ####### # # # #######","## ## ## # ## # # ### ## ## # ## # #### ####### # ### # ## # # #####","## ##### ### ### # #### ### # ## # # #### ## ## ### ## ## # # # #####","## ## ## ### ### ##### ### # ## # # #### ### ### ##### # # # ## # # #######","## ## ## ### # ## ##### # # ## # # #### ## ## #### # ## ## ####","## ## ## ### ## ##### ## ## ## # # ### ## #### #### #### ##","## ## ## ## # ## ##### ## ## ## # # ## ## ## ##### ##### #####","## ##### ### ### ##### ## ## ## # # #### ##### # # ## ### # ##","## ## ###### ### ## ## ## ## # # #### ## ## # # ## ## # # ##","## ## ## ### ### ## ## # ## # #### #### ## ## ## # ## ## ##### ","############################################################################",};char hyb[200][200];for (int i = 0; i < 200; i++) {for (int j = 0; j < 200; j++) {if (slect_slect == 1) {hyb[i][j] = hyb1[i][j];}else if (slect_slect == 2) {hyb[i][j] = hyb2[i][j];}}}find_run_foot(hyb);return 0; }//主函數 int main() {printf("-------歡迎來到迷宮小游戲------\n");printf("------------版本1.0------------\n");printf("------------vs2019下運行---------------\n");printf("說明:本游戲要時間加載,請耐心等待!\n");Sleep(500);system("cls");printf("抵制不良游戲,做優秀的聯大學子!\n");printf("作者:黃渝斌(北京聯合大學),計算機科學與技術專業\n");printf("游戲說明:抵制不良游戲,做優秀的聯大學子!\n");printf("本游戲源代碼將上傳博客CSDN,搜索用戶“梵高的豬v”,借用原代碼請標明出處!\n");Sleep(1000);system("cls");printf("等待游戲中………………\n");printf("游戲說明:用戶請先初始化一個迷宮,然后根據自己需要是否要進行其他選擇!\n");Sleep(1000);system("cls");node* s;s = first();int a[100][100];int week;int w, h;int x1 = 1, y1 = 0,x2,y2, x = 1, y = 1;int count = 0;int slect;while (1) {printf("--------請選擇版本型號----------\n");printf("--------1-靜態迷宮1.0-----------\n");printf("--------2-動態迷宮1.0--------\n");printf("--------0-退出游戲-----------\n");printf("請輸入版本型號:\n");scanf_s("%d",&slect);system("cls");if (slect == 1) {while (1) {printf("-------1-初始化迷宮------------\n");printf("-------2-打印迷宮--------------\n");printf("-------3-出路迷宮--------------\n");printf("-------0-退出游戲--------------\n");printf("請輸入你要選擇的功能:\n");scanf_s("%d", &week);system("cls");if (week == 1) {printf("請輸入你要創建的迷宮大小:\n");scanf_s("%d%d", &w, &h);creatmap(a, w + 2, h + 2);count++;}else if (week == 2) {if (count > 0) {print_map(a, w, h);count++;}else {printf("請先初始化一個迷宮!\n");Sleep(1000);system("cls");continue;}}else if (week == 3) {if (count > 0) {x2 = w + 1;y2 = h;int i = find(s, a, w, h, x1, y1, x2, y2, x, y);print_foot(s, i);count++;}else {printf("請先初始化一個迷宮!\n");Sleep(1000);system("cls");continue;}}else if (week == 0) {system("cls");printf("---------------游戲結束!--------------\n");Sleep(1000);break;}int m;printf("請選擇是否執行程序!\n");printf("1-true,0-fasle\n");scanf_s("%d", &m);system("cls");if (m == 1) {continue;}elsesystem("cls");printf("---------------游戲結束!--------------\n");Sleep(1000);break;}}else if (slect == 2) {int slect1;printf("游戲說明:該游戲控制的方向如下:w(上)s(下)a(左)d(右)。\n若無法移動,可按住alt鍵在進行操作!\n");Sleep(1000);system("cls");printf("------------請輸入選項!----------\n");printf("------------1-簡單模式------------\n");printf("------------2-困難模式------------\n");printf(" …… \n");printf("------------0-退出游戲------------\n");scanf_s("%d",&slect1);system("cls");if (slect1 == 1) {run_foot(1);continue;}else if (slect1 == 2) {run_foot(2);continue;}else if (slect1 == 0) {continue;}else {printf("-----其他選項待開發!----");Sleep(1000);continue;}}else if(slect==0){break;}else {printf("其他大型迷宮待開發中!");continue;}}return 0; }

總結

以上是生活随笔為你收集整理的数据结构课程设计,迷宫问题求解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。