您好,登錄后才能下訂單哦!
新建一個.txt文檔來存儲迷宮,輸入n*n的迷宮矩陣并保存起來,如下圖
//Stack.h
#pragma once template <class T> class stack { public: stack() //構造函數 :_arr(NULL) , _top(0) , _capacity(0) {} ~stack() //析構函數 { if (_arr) { delete[] _arr; } } public: void Push(const T& x) //插入 { _CheckCapacity(); _arr[_top++] = x; } void Pop() //刪除 { assert(_top > 0); --_top; } size_t Size() //大小 { return _top; } bool Empty() //判斷棧是否為空 { //return _top == 0; if (_top <= 0) { return true; } else return false; } T& Top() //獲取棧頂元素 { return _arr[_top - 1]; } protected: void _CheckCapacity() { if (_arr == NULL) { _capacity = 5; _arr = new T[_capacity]; return; } else if (_top == _capacity) { _capacity *= 2; T* tmp = new T[_capacity]; for (size_t i = 0;i < _top;++i) { tmp[i] = _arr[i]; } delete[] _arr; _arr = tmp; } } protected: T* _arr; size_t _top; size_t _capacity; };
//Maze.cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> using namespace std; #include <assert.h> #include "Stack.h" struct Pos { int _row; int _col; }; //函數聲明 void GetMaze(int* a, int n); bool SearchMazePath(int *a, int n, Pos entrance, stack<Pos>& paths); bool CheckIsAccess(int* a, int n, const Pos& next); void GetMaze(int* a, int n) //將迷宮存到一個一維數組a里 { assert(a); FILE* fout = fopen("E:\\MazeMap.txt", "r"); //以讀的方式讀取迷宮矩陣 assert(fout); for (int i = 0;i < n;++i) { for (int j = 0;j < n;) { char ch = fgetc(fout); if (ch == '1' || ch == '0') //矩陣中‘0’為通路 { a[i*n + j] = ch - '0'; cout << a[i*n + j]<<" "; ++j; } } cout << endl; } cout << endl; } bool CheckIsAccess(int* a, int n, const Pos& next) { int row = next._row; int col = next._col; if (row >= 0 && row < n && col >= 0 && col < n && a[row*n + col] == 0) { return true; } else { return false; } } //尋找通路,并用棧來存儲 bool SearchMazePath(int *a, int n, Pos entrance, stack<Pos>& paths) { assert(a); paths.Push(entrance); bool isfirst = true; while (!paths.Empty()) { Pos cur = paths.Top(); a[cur._row*n + cur._col] = 7; //將通路標記為7 if (isfirst == false || cur._row == n - 1 || cur._col == n - 1 || cur._row == 0 || cur._row == 0 ) //如果找到矩陣的四個邊就表示找到通路 { return true; } Pos next = cur; //判斷當前元素的上路是否為通路,即是否為‘0’ next._row--; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } //判斷當前元素的下路是否為通路 next = cur; next._row++; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } //判斷當前元素的左路是否為通路 next = cur; next._col--; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } //判斷當前元素的右路是否為通路 next = cur; next._col++; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } paths.Pop(); isfirst = false; } return false; } //打印迷宮矩陣 void PrintMaze(int* a, int n) { for (int i = 0;i < n;++i) { for (int j = 0;j < n;++j) { cout << a[i*n + j] << " "; } cout << endl; } cout << endl; } int main() { int size = 5; int mazemap[25] = { 0 }; Pos entrance; entrance._row = 1; entrance._col = 0; stack<Pos> path; GetMaze(mazemap, size); if (SearchMazePath(mazemap, size, entrance, path)) { PrintMaze(mazemap, size); } system("pause"); return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。