迷宫生成

2017-12-09 16:16:27


#include<conio.h>
#include<bits/stdc++.h>
#include <windows.h>
using namespace std;

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define br BACKGROUND_RED
#define bg BACKGROUND_GREEN
#define bb BACKGROUND_BLUE
#define bi BACKGROUND_INTENSITY
#define fr FOREGROUND_RED
#define fg FOREGROUND_GREEN
#define fb FOREGROUND_BLUE
#define fi FOREGROUND_INTENSITY
#define MAXM 4005
#define MAXN 1000005
struct dian
{
    int x,y;
}d[MAXN];
struct bian
{
    int x,y;
}b[MAXN];
int f[MAXN];
int n,m,nn,mm;
char a[MAXM][MAXM];

void col(int co)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),co);
}

void prin(int num)
{
    col(fr+fg+fb);
    switch (num)
    {
        case 0:
        {
            col(fr+fg+fb);
            break;
        }
        case 1:
        {
            col(br+bi);
            break;
        }
        case 2:
        {
            col(bg+bi);
            break;
        }
        case 3:
        {
            col(bb+bi);
            break;
        }
    }
   // if (choice) cout<<"**";
    cout<<"  ";
}

void rd()
{
    scanf("%d",&n);
    for(int i = 1; i <= n*2-1; i ++)
        for(int j = 1; j <= n*2-1; j ++)
        if(i%2 == 1 && j%2 == 1)
        {
            a[i][j] = ' ';    
            nn ++;
            d[nn].x = i;
            d[nn].y = j;
            if(i != 1)
            {
                mm ++;
                b[mm].x = nn;
                b[mm].y = nn-n;
            }
            if(j != 1)
            {
                mm ++;
                b[mm].x = nn;
                b[mm].y = nn-1;
            }

        }
        else
            a[i][j] = '#';
}

void wt(int x,int y)
{
    int sy = 10;
    //for(int i = 1; i <= nn*2+1; i ++) cout<<"_";
    //cout<<"\n";
    for(int i = max(x-sy,1); i <= min(x+sy,nn*2-1); i ++)
    {
       // cout<<"|";
        for(int j = max(1,y-sy); j <= min(y+sy,nn*2-1); j ++)
            if(a[i][j] == ' ') prin(1);
            else if(a[i][j] == 'o')prin(2);
            else prin(3);
        cout<<"\n";
    }
    prin(5);
   // for(int i = 1; i <= nn*2+1; i ++) cout<<"-";
    //cout<<"\n";

}

void csh()
{
    srand(time(0));
    for(int i = 1; i <= n; i ++)
        f[i] = i;
}

int zhao(int x)
{
    return f[x] == x ? x : zhao(f[x]); 
}

void he(int x,int y)
{
    f[zhao(y)] = zhao(x);
}

void zou()
{
    int x,y,lx,ly,n = nn * 2 - 1;
    char ch;
    x = 1;y = 1;
    while(1)
    {
        lx = x;
        ly = y;
        a[x][y] = 'o';
        system("cls");
        wt(x,y);
        do
        {
            ch = getch();
        }while(ch != 'w' && ch != 's' && ch != 'd' && ch != 'a');
        a[x][y] = ' ';
        if(ch == 'w') x --;
        if(ch == 's') x ++;
        if(ch == 'a') y --;
        if(ch == 'd') y ++;
        if(a[x][y] != ' ') {x = lx; y = ly;}
        while(x > n) x --;
        while(y > n) y --;
        while(x < 1) x ++;
        while(y < 1) y ++;

        if(x == n && y == n)
        {
            cout<<"You Win";
            break;
        }
    }
}

int main()
{
    //freopen("1.txt","w",stdout);
    rd();
    csh();
    swap(n,nn);
    m = mm;
    csh();
    int o;
    for(int  i = 1; i <= n-1 ; i ++)
    {
        do
        {
            o = rand()%m+1;
        }while(zhao(b[o].x) == zhao(b[o].y));
        he(b[o].x,b[o].y);
        a[(d[b[o].x].x + d[b[o].y].x)/2][(d[b[o].x].y + d[b[o].y].y)/2] = ' '; 
    }
    wt(1,1);
    zou();
    return 0;
}