C++解迷宫问题
#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; }
创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站制作、邓州网络推广、微信平台小程序开发、邓州网络营销、邓州企业策划、邓州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供邓州建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
标题名称:C++解迷宫问题
标题网址:http://cdiso.cn/article/ijjoeo.html