您好,登錄后才能下訂單哦!
新建一個(gè).txt文檔來(lái)存儲(chǔ)迷宮,輸入n*n的迷宮矩陣并保存起來(lái),如下圖
//Stack.h
#pragma once template <class T> class stack { public: stack() //構(gòu)造函數(shù) :_arr(NULL) , _top(0) , _capacity(0) {} ~stack() //析構(gòu)函數(shù) { 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; }; //函數(shù)聲明 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) //將迷宮存到一個(gè)一維數(shù)組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; } } //尋找通路,并用棧來(lái)存儲(chǔ) 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; //將通路標(biāo)記為7 if (isfirst == false || cur._row == n - 1 || cur._col == n - 1 || cur._row == 0 || cur._row == 0 ) //如果找到矩陣的四個(gè)邊就表示找到通路 { return true; } Pos next = cur; //判斷當(dāng)前元素的上路是否為通路,即是否為‘0’ next._row--; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } //判斷當(dāng)前元素的下路是否為通路 next = cur; next._row++; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } //判斷當(dāng)前元素的左路是否為通路 next = cur; next._col--; if (CheckIsAccess(a, n, next)) { paths.Push(next); continue; } //判斷當(dāng)前元素的右路是否為通路 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; }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。