zoukankan      html  css  js  c++  java
  • 迷宫问题

      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 <iostream>           //////////-static-libgcc
     11 #include <iomanip>
     12 #include <ctime>
     13 #include <stack>
     14 #include<cstdio>
     15 #include<windows.h>          //Sleep()头文件 
     16 #include<cstring>
     17 #include<mmsystem.h>         //播放MP3文件的头文件,工具编译选项编译器在连接器命令加入-lwinmm  
     18 #include<conio.h>
     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<position> *&path, int **maze, int row, int col);       //寻找路径 
     35 void outputPath(stack<position> *&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
    ";
     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<position> *&path, int **maze, int row, int col)
    118 {
    119     //寻找一条从入口(1,1), 到达出口(size, size)的路径
    120     //如果找到, 返回true, 否则返回false
    121     
    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<position> *&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<position> *path = new stack<position>;
    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! 
    ";
    221         return 1;
    222     }
    223     Pos(9, 20);
    224     cout << "输入迷宫: 
    ";
    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 
    "; 
    251     Pos(9,31);
    252     cout << "↖RAT IN A MAZE↗
    ";
    253     Pos(11,31);
    254     cout << "by 一念永恒, 2016
    ";
    255     Pos(13, 0);
    256     SetConsoleTextAttribute(console, 11);
    257     cout << "加载ing...	";
    258     for(int i = 0; i<101; ++i)
    259     {
    260         printf("%2.0f%%", i/100.0 * 100 ); 
    261         Sleep(20);
    262         printf("");
    263     }
    264     cout << "
    			";
    265     for(int i = 0; i<5; ++i)
    266     {
    267         Sleep(500); cout << "@。@	";
    268     }
    269     cout << endl;
    270     SetConsoleTextAttribute(console, 11);
    271     cout << endl; 
    272     cout << "===============================按回车键进入游戏=========================================
    ";
    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 }

  • 相关阅读:
    Lodash 严重安全漏洞背后 你不得不知道的 JavaScript 知识
    SQL Server和C#中无法将小数字符串直接转换为整数类型
    Is default(CancellationToken) equivalent to CancellationToken.None?(转载)
    Stored Procedures: OUTPUT vs OUT?(转载)
    关于TransactionScope.Complete方法(链接)
    Do I need to dispose of Tasks?(链接)
    EF Core 3.0 Keyless Entity Types (链接)
    为什么C#接口中不能声明async异步函数(转载)
    How does SqlDataReader handle really large queries?(转载)
    ASP.NET Core MVC中的Filter如果不实现IFilterFactory接口,那么Filter默认情况下在ASP.NET Core生命周期内是单例的,会被重用
  • 原文地址:https://www.cnblogs.com/douzujun/p/5937015.html
Copyright © 2011-2022 走看看