#includeusing namespace std; #include #include //初始化迷宮 void InitMaze(int* maze,int row, int col) { FILE* fout = fopen("Maze.txt", "r"); assert(fout); for (int i = 0; i < row; ++i) { for (int j = 0; j < col;) { char ch = fgetc(fout); if (ch == EOF) { cout<<"Init MazeMap fail"< & path) { assert(maze); path.push(entry); maze[entry._row * col + entry._col] = 2;//將走過的路標記為2 while (!path.empty()) { Pos cur = path.top(); Pos next = cur; if (row-1 == next._row)//找到出口 { return true; } //判斷右邊是否為0 next = cur; next._col++; if (CheckIsPassWay(maze, row, col, next)) { maze[next._row * row + next._col] = 2; path.push(next); continue; } //上 next = cur; next._row--; if (CheckIsPassWay(maze, row, col, next)) { maze[next._row * row + next._col] = 2; path.push(next); continue; } //下 next = cur; next._row++; if (CheckIsPassWay(maze, row, col, next)) { maze[next._row * row + next._col] = 2; path.push(next); continue; } //左 next = cur; next._col--; if (CheckIsPassWay(maze, row, col, next)) { maze[next._row * row + next._col] = 2; path.push(next); continue; } path.pop();//四個方向都不通,返回上一步 } return false;//棧為空,沒有找到出口 } void TestMaze() { int maze[10][10] = {}; Pos entry = {1, 0}; stack path;//將走過的路徑保存在棧path中 InitMaze((int*)maze, 10, 10); PrintMaze((int*)maze, 10, 10); GetMazePath((int*)maze, 10, 10, entry, path); PrintMaze((int*)maze, 10, 10); } int main() { TestMaze(); return 0; }
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于做網(wǎng)站、網(wǎng)站制作、鄧州網(wǎng)絡推廣、微信平臺小程序開發(fā)、鄧州網(wǎng)絡營銷、鄧州企業(yè)策劃、鄧州品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供鄧州建站搭建服務,24小時服務熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com