Maze generation algorithms prime, automatic Maze

He wrote one afternoon, from generation to start learning algorithm maze.

Probably style program:

 

 

 

 

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include<bits/stdc++.h>
#include <windows.h>
#include <tchar.h>
#include <SDKDDKVer.h>
#include<conio.h>
using namespace std;
#define MAZE_MAX 1000
int mz[MAZE_MAX+2][MAZE_MAX+2];
int hs[MAZE_MAX+2][MAZE_MAX+2];
int Move[MAZE_MAX+2][MAZE_MAX+2];
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int vis[MAZE_MAX];
int fa[MAZE_MAX];
int X = 12, Y = X;
void make_path();
struct node{
    int x,y;
};
struct node f[MAZE_MAX+2],ppk,endd;
int cnt=0;
void gotoxy(int x, int y){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
void hideCursor(){
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void Print_Maze()
{
    printf("使用w,s,a,d移动,Esc退出\n");
    printf("当前迷宫大小:%d\n",X );
    for(int i=0; i<=X; i++){
        for(int j=0;j<=X;j++){
            if(mz[i][j]){
                printf("%s","");
              }
              else  if(Move[i][j]==1){
                  printf("%s","");
                }
              else  if(i==1&&j==1){
                  printf("%s","");
                }
             else if(i==X-1&&j==Y-1){
                  printf("%s","");
              }
              else if(i==ppk.x&&j==ppk.y){
                 printf("");
             }
              else{
                  printf("%s","  ");
              }
        }
        printf("\n");
    }
}
void updata_Maze(){
    hideCursor();
    gotoxy(0, 7);
    Print_Maze();
}
void Init_Maze(){
    cnt=0;
    for(int i=0;i<=X;i++){
        for(int j=0;j<=Y;j++){
            Move[i][j]=0;
            if(I == 0 || J == 0 || || J == I == the Y X-) 
                HS [I] [J] = - 2 ; // wall is. 1 
            the else 
                HS [I] [J] = - 1 ; // can open the way to -1 
            IF (I == 0 || || X-J == I == 0 || || J == the Y (I% 2 == 0 ) || (J% 2 == 0 )) { 
                  MZ [I] [J] = . 1 ; //
             }
             the else { 
                Node TP; 
                tp.x = I, tp.y = J;
                hs[i][j]=cnt;
                fa[cnt]=cnt;
                f[cnt++]=tp;
            }
        }
    }
    endd=f[cnt-1];
}
int find(int x){
    while(fa[x]!=x) x=fa[x];
    return x;
}
int merge(int x,int y){
    int f1=find(x);
    int f2=find(y);
    if(f1<f2) fa[f2]=f1;
    else if(f1>f2) fa[f1]=f2;
    else return 0;
    return 1;
}
int check_wall(int x,int y){
    if(x<=0||x>=X||y>=Y||y<=0){
        return 1;
    }
    return 0;
}
void make_path(){
    srand(time(NULL));
    for(int p=0;p<cnt*8;p++){
        if(find(0)==find(cnt-1)){
            return ;
        }
        int road=vis[p%cnt];
        int next=rand()%4;
        int x=f[road].x,y=f[road].y;
        int cx=x+dir[next][0]*2,cy=y+dir[next][1]*2;
        int next_road=hs[cx][cy];
        if(!check_wall(cx,cy)){
            merge(road,next_road);
            mz[x+dir[next][0]][y+dir[next][1]]=0;
        }
    }
    ppk=f[0];
}
void Init_list(){
    srand(time(NULL));
    for(int i=0;i<cnt;i++){
        vis[i]=i;
    }
    for(int i=0;i<cnt;i++){
        int a=rand()%cnt,b=rand()%cnt;
        swap(vis[a],vis[b]);
    }
}
int check_rode(int x,int y){
    return mz[x][y]==0 ? 1: 0 ; 
}     
void p_Move () {
     int CH; PPKs = F [ 0 ];
     the while ( . 1 ) {
         IF (ppk.x == F [the CNT- . 1 ] .x && ppk.y == F [the CNT- . 1 ]. Y) { 
            the printf ( " you win \ n-! " );
             BREAK ; 
        } 
        updata_Maze (); 
        IF (_kbhit ()) { // if the key is pressed, the _kbhit () function returns true 
            ch = _getch () ; // use _getch () function to get the key pressed 
            IF (CH == 27 ) {
                 BREAK;
            }  ))// cycle when the ESC is pressed, when the key ESC key 27. A 
            the else  IF (CH == 119 ) { // W 
                IF (check_rode (ppk.x- . 1 , ppk.y)) 
                ppk.x - ; 
            } 
            the else  IF (CH == 115 ) { // S 
                IF (check_rode (+ ppk.x . 1 , ppk.y)) 
                ppk.x ++ ; 
            } 
            the else  IF (CH == 97 ) { // A 
                IF (check_rode (ppk.x, ppk.y- 1 
                ppk.y--;
            }
            else if(ch==100){//d
                if(check_rode(ppk.x,ppk.y+1))
                ppk.y++;
            }
        }
    }
}
node step[MAZE_MAX];
int flag=0,walked[MAZE_MAX][MAZE_MAX];
void dfs_find_path(int stp,node now){
    if(flag) return ;
    if(now.x==endd.x&&now.y==endd.y){
        flag=1;
        printf("%s\n","走到了" );
        for(int i=0;i<stp;i++){
            Move[step[i].x][step[i].y]=1;
            updata_Maze();
        }
        return ;
    }
    node next_road;
    for(int i=0;i<4;i++){
        int cx=now.x+dir[i][0],cy=now.y+dir[i][1];
        if(check_rode(cx,cy)&&!walked[cx][cy]){
            walked[cx][cy]=1;
            next_road.x=step[stp].x=cx;
            next_road.y=step[stp].y=cy;
            dfs_find_path(stp+1,next_road);
            walked[cx][cy]=0;
        }
    }
}
void Print_hello(){
    hideCursor();
    gotoxy(0, 0);
    printf("--------------------------\n");
    printf("1:生成迷宫\n"); 
    Printf ( " 2: their own path \ the n- " ); 
    printf ( " 3: Computer walk \ the n- " ); 
    printf ( " 4: Set the size \ the n- " ); 
    printf ( " 5: Exit \ the n- " ); 
    printf ( " -------------------------- \ n- " ); 
} 
int BEGINTIME, the endtime;
 void the MENU1 () { 
    System ( " CLS " ) ; 
    BEGINTIME = Clock ();   // timing begins 
    Init_Maze (); // initialize maze 
    Init_list (); // generates a random number sequence
    make_path (); // generated maze 
    updata_Maze (); 
    the endtime = Clock ();   // counts up 
    the printf ( " elapsed time: 8D% MS " , endtime- BEGINTIME); 
} 
void Menu2 () { 
    BEGINTIME = Clock ();   // timing begins 
    p_Move (); //
     the endtime = Clock ();   // counts up 
    the printf ( " elapsed time: 8D% MS " , endtime- BEGINTIME); 
} 

void menu3 () { 
    BEGINTIME = Clock ();   // timing begins 
    PPK f = [ 0]; 
    In Flag = 0 ; 
    dfs_find_path ( 0 , PPKs); 
    the endtime = Clock ();   // counts up 
    the printf ( " elapsed time: 8D% MS " , endtime- BEGINTIME); 
} 
void menu4 () { 
    the printf ( " \ n- enter fans size:     " ); 
    Scanf ( " % D " , & X-); X-% 2 == . 1 X-++:? . 1 ; 
    the Y = X-; 
    the MENU1 (); 
} 
void the MENU () {
     intCH;
     the while ( . 1 ) { 
        print_hello (); 
        IF (_kbhit ()) { // if the key is pressed, the _kbhit () function returns true 
            CH = _getch (); // use _getch () function to get by key in the 
            IF (CH == 49 ) { // W 
                the MENU1 (); 
            } 
            the else  IF (CH == 50 ) { 
                Menu2 (); 
            } 
            the else  IF (CH == 51 is ) { 
                menu3 (); 
            } 
            the else  IF (CH == 52 is ) { // D
                menu4();
            }
            else if(ch==53){
                break;
            }
        }
    }
}
int main(){
    system("chcp 65001");
    system("mode con cols=220 lines=100");
    system("color 9");
    MENU();
    return 0;
}

 

  

Guess you like

Origin www.cnblogs.com/37kiazz73/p/11588502.html