博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迷宫问题
阅读量:4673 次
发布时间:2019-06-09

本文共 6999 字,大约阅读时间需要 23 分钟。

1 /*  2 8 6  3 1 1 1 1 1 1 1 1  4 1 0 0 1 0 0 1 1  5 1 1 0 0 0 0 0 1  6 1 0 0 1 0 1 0 1  7 1 0 0 0 0 0 0 1  8 1 1 1 1 1 1 1 1  9 */ 10 #include 
//-static-libgcc 11 #include
12 #include
13 #include
14 #include
15 #include
//Sleep()头文件 16 #include
17 #include
//播放MP3文件的头文件,工具\编译选项\编译器\在连接器命令加入\-lwinmm 18 #include
19 using namespace std; 20 21 struct position{ 22 int row; // x 23 int col; // y 24 int option; //下一步 25 }; 26 void Pos(int x, int y) { //设置放置位置 27 COORD p; 28 p.X = y;p.Y = x; 29 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p); 30 } 31 void welcome(); //欢迎模块 32 void play_mp3(char *ps); //播放音乐 33 void MazeGenerator(char **m, int row, int col); //输入模块 34 bool findPath(stack
*&path, int **maze, int row, int col); //寻找路径 35 void outputPath(stack
*&path); //输出模块 36 37 int **NewDArray(int row, int col) //设置迷宫的大小 38 { 39 int **m = new int *[row]; 40 if (!m) { 41 cout << "Out of Memory !"; return nullptr; 42 } 43 for (int i = 0; i < col; i++) { 44 m[i] = new int [col]; 45 if (!m[i]) { 46 cout << "Out of Memory\n"; 47 for (int k = 0; k < i; k++) delete []m[i]; 48 delete []m; return nullptr; 49 } 50 } 51 52 // for (int i = 0; i < row ; i++) { 53 // for (int j = 0; j < col ; j++) 54 // m[i][j] = 0; 55 // } 56 // for (int i = 0; i < row; i++) { 57 // for (int j = 0; j < col; j++) 58 // cout << m[i][j] << " "; 59 // cout << endl; 60 // } 61 // cout << "row = " << row << ", col = " << col << endl; 62 return m; 63 } 64 65 void DeleteDArray(int **m, int row) //释放迷宫动态内存 66 { 67 for (int i = 0; i < row; i++) 68 delete []m[i]; 69 delete []m; 70 } 71 72 void MazeGenerator(int **m, int row, int col) //随机产生迷宫 73 { 74 int i, j; 75 // time_t t; 76 int code; 77 78 // srand((unsigned)time(&t)); //设置随机种子 79 // for (int i = 0; i < col; i++) { 80 // m[0][i] = 1; 81 // m[row - 1][i] = 1; 82 // } 83 // cout << "Deubg !!"; 84 // for (int j = 1; j < row - 1; j++) m[j][0] = m[j][col - 1] = 1; 85 86 for (i = 0; i < row ; i++) { 87 Pos(10 + i, 22); 88 for (j = 0; j < col ; j++) 89 { 90 cin >> code; 91 m[i][j] = code; 92 } 93 } 94 } 95 96 void MazeDisplay(int **m, int row, int col) //显示迷宫 97 { 98 system("cls"); 99 100 int i, j;101 102 HANDLE console;103 console = GetStdHandle(STD_OUTPUT_HANDLE);104 SetConsoleTextAttribute(console, 7);105 cout << endl;106 for (i = 0; i < row; i++)107 { 108 Pos(7+i, 30);109 for (j = 0; j < col; j++)110 cout << ' ' << m[i][j];111 cout << endl;112 }113 Pos(7+row, 0);114 cout << endl;115 }116 117 bool findPath(stack
*&path, int **maze, int row, int col)118 {119 //寻找一条从入口(1,1), 到达出口(size, size)的路径120 //如果找到, 返回true, 否则返回false121 122 //初始化偏移量123 position offset[8];124 125 // offset[0].row = 0; offset[0].col = 1; //右 126 // offset[1].row = 1; offset[1].col = 0; //下127 // offset[2].row = 0; offset[2].col = -1; //左128 // offset[3].row = -1;offset[3].col = 0; //上129 // 130 // offset[0].row = 1; offset[0].col = 0; //下131 // offset[1].row = 0; offset[1].col = 1; //右 132 // offset[2].row = -1;offset[2].col = 0; //上133 // offset[3].row = 0; offset[3].col = -1; //左134 135 //136 // offset[0].row = 0; offset[0].col = -1; //左 137 // offset[1].row = 1; offset[1].col = 0; //下 138 // offset[2].row = 0; offset[2].col = 1; //右 139 // offset[3].row = -1;offset[3].col = 0; //上140 141 offset[0].row = 0; offset[0].col = 1; //右142 offset[1].row = 1; offset[1].col = 1; //右下 143 offset[2].row = 1; offset[2].col = 0; //下144 offset[3].row = -1; offset[3].col = -1; //左下 145 offset[4].row = 0; offset[4].col = -1; //左146 offset[5].row = -1; offset[5].col = -1; //左上 147 offset[6].row = -1;offset[6].col = 0; //上148 offset[7].row = -1; offset[7].col = 1; //右上 149 150 151 position here;152 here.row = 1; here.col = 1; here.option = 0;153 maze[1][1] = 1; //防止回到入口154 // int option = 0; //下一步 155 int lastOption = 7; //最后一步156 157 //寻找一条路径158 while (here.row != row - 2 || here.col != col - 2)159 { //没有找到出口160 //找到要移动的相邻一步161 int r, c;162 while (here.option <= lastOption) {163 r = here.row + offset[here.option].row;164 c = here.col + offset[here.option].col;165 if (maze[r][c] == 0) break; //如果是是通路,就一直超着该通路方向. 166 here.option++; //否则,选择下一个方向 167 }168 //相邻的一步是否找到 ? 169 if (here.option <= lastOption) 170 { //移到maze[r][c] 171 path->push(here); //将(x,y)入栈 172 here.row = r; here.col = c; //求新点坐标,得新(x,y) 173 maze[r][c] = 1; //设置1,以防止重复访问174 here.option = 0; 175 }176 else { //没有邻近的一步可以走,返回 177 if (path->empty()) return false; //如果没有路径,则返回 178 // position next = path->top(); //here(当前位置) 和 next(退一步)相邻,next-->here(最后一次移动) 179 here = path->top();180 path->pop(); //往回走一步181 here.option++; //换一个方向走 182 }183 } 184 path->push(here);185 return true; //到达出口 186 187 }188 189 void outputPath(stack
*&path)190 {191 HANDLE console;192 console = GetStdHandle(STD_OUTPUT_HANDLE);193 SetConsoleTextAttribute(console, 14);194 int len = path->size(), row = 0, col = 0;195 for (int i = 1; i <= len - 1; i++) {196 row = path->top().row , col = path->top().col;197 cout << "(" << row << " , " << col << ")" << " <- " ;198 if (i % 7 == 0) cout << endl;199 path->pop();200 }201 row = path->top().row , col = path->top().col;202 cout << "(" << row << " , " << col << ")"; path->pop();203 cout << endl;204 }205 206 int main()207 {208 welcome();209 HANDLE console;210 console = GetStdHandle(STD_OUTPUT_HANDLE);211 SetConsoleTextAttribute(console, 13);212 213 stack
*path = new stack
;214 int row, col;215 Pos(7, 20);216 cout << "Enter the maze size(row and col): ";217 cin >> row >> col;218 int **m = NewDArray(row, col); //返回动态申请的二维数组219 if (!m) {220 cout << "out of Memory! \n";221 return 1;222 }223 Pos(9, 20);224 cout << "输入迷宫: \n";225 MazeGenerator(m, row, col); //随机生成迷宫 226 MazeDisplay(m,row,col); //显示迷宫 227 228 if (findPath(path, m, row, col)) {229 outputPath(path);230 // MazeDisplay(m,row,col); //显示迷宫 231 }232 else {233 cout << "No Path " << endl;234 }235 236 system("pause");237 return 0;238 }239 240 241 242 void welcome() {243 char pause;244 HANDLE console;245 console = GetStdHandle(STD_OUTPUT_HANDLE);246 char mp[] = "F:\\KuGou\\薛之谦_绅士.mp3";247 play_mp3(mp);248 Pos(7,33);249 SetConsoleTextAttribute(console, 15);250 cout << " Welcome To \n"; 251 Pos(9,31);252 cout << "↖RAT IN A MAZE↗\n";253 Pos(11,31);254 cout << "by 一念永恒, 2016\n";255 Pos(13, 0);256 SetConsoleTextAttribute(console, 11);257 cout << "加载ing...\t";258 for(int i = 0; i<101; ++i)259 {260 printf("%2.0f%%", i/100.0 * 100 ); 261 Sleep(20);262 printf("\b\b\b");263 }264 cout << "\n\t\t\t";265 for(int i = 0; i<5; ++i)266 {267 Sleep(500); cout << "@。@\t";268 }269 cout << endl;270 SetConsoleTextAttribute(console, 11);271 cout << endl; 272 cout << "===============================按回车键进入游戏=========================================\n";273 cin.get(pause);274 system("cls");275 }276 277 void play_mp3(char *ps) // 歌曲的名字 278 {279 char str[100] = "play ";280 strcat(str,ps);281 mciSendString(str,NULL,0,NULL);282 }

转载于:https://www.cnblogs.com/douzujun/p/5937015.html

你可能感兴趣的文章