迷宫

· · 个人记录

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
const int M = 15;
const int K = 1;
const int N = 2 * M + 1;
const int dx[] = {1,-1,0,0};
const int dy[] = {0,0,1,-1};
int fa[N * N + 3],box[N][N],cnt;
pair<int,int> pos[N * N];
inline int find(int x)
{
    if(fa[x] == x) return x;
    return fa[x] = find(fa[x]);
}
int step,s,prv,tar;
int dis[N + 3][N + 3];
pair<int,int> from[N + 3][N + 3];
char a[N + 3][N + 3];
random_device rd;
mt19937 rg(rd());
inline void print()
{
    system("cls");
    for(int i = 1; i <= N; i++,putchar('\n'))
        for(int j = 1; j <= N; j++)
            if(a[i][j] == ' ') printf("  ");
    else if(a[i][j] == '@') printf("ME");
    else printf("田");
    printf("time:%d\nstep:%d\ntarget:%d\n",time(0) - s,step,tar);
    prv = time(0);              
}
signed main()
{
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            if(i % 2 || j % 2) a[i][j] = '#'; 
    else a[i][j] = ' ',box[i][j] = ++cnt,pos[cnt] = {i,j};
    for(int i = 1; i <= cnt; i++) fa[i] = i;
    for(int i = 1; i < cnt; i++)
    {
        while(true)
        {
            int _ = rg() % 99824453 % cnt + 1,d = rg() % 19 % 4;
            auto& [x,y] = pos[_];
            int nx = x + dx[d] * 2,ny = y + dy[d] * 2;
            if(nx < 1 || ny < 1) continue;
            if(nx > N || ny > N) continue;
            if(find(_) == find(box[nx][ny])) continue;
            a[nx - dx[d]][ny - dy[d]] = ' ';
            fa[find(_)] = find(box[nx][ny]);
            break;
        }
    }



    for(int i = 1; i <= K; i++)
    {
        int _ = rg() % 99824453 % cnt + 1,d = rg() % 19 % 4;
        auto& [x,y] = pos[_];
        int nx = x + dx[d] * 2,ny = y + dy[d] * 2;
        if(nx < 1 || ny < 1) {i--; continue;}
        if(nx > N || ny > N) {i--; continue;}
        if(a[nx - dx[d]][ny - dy[d]] == ' ') {i--; continue;}
        a[nx - dx[d]][ny - dy[d]] = ' ';
    }   



    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            dis[i][j] = -1;
    dis[2][2] = 0;
    queue< pair<int,int> > q;
    q.push({2,2});
    while(!q.empty())
    {
        auto [x,y] = q.front(); q.pop();
        for(int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 1 || ny < 1) continue;
            if(nx > N || ny > N) continue;
            if(a[nx][ny] == '#') continue;
            if(dis[nx][ny] >= 0) continue;
            dis[nx][ny] = dis[x][y] + 1;
            q.push({nx,ny});
        }
    } tar = dis[N - 1][N - 1];

    int x = 2,y = 2; s = time(0);
    a[x][y] = '@'; print(); a[x][y] = ' ';
    while(x != N - 1 || y != N - 1)
    {
        if(!_kbhit())
        {
        //  if(prv != time(0)) a[x][y] = '@',print(),a[x][y] = ' ';
            continue;
        }
        step++;
        char c = _getch(); 
        if(c == 'a')
            if(a[x][y - 1] == ' ') y--;
        if(c == 'd')
            if(a[x][y + 1] == ' ') y++;
        if(c == 'w')
            if(a[x - 1][y] == ' ') x--;
        if(c == 's')
            if(a[x + 1][y] == ' ') x++;
        if(c == '0')
        {
            for(int i = 1; i <= N; i++)
                for(int j = 1; j <= N; j++)
                    dis[i][j] = -1;
            dis[x][y] = 0;
            queue< pair<int,int> > q;
            q.push({x,y});
            while(!q.empty())
            {
                auto [x,y] = q.front(); q.pop();
                for(int i = 0; i < 4; i++)
                {
                    int nx = x + dx[i];
                    int ny = y + dy[i];
                    if(nx < 1 || ny < 1) continue;
                    if(nx > N || ny > N) continue;
                    if(a[nx][ny] == '#') continue;
                    if(dis[nx][ny] >= 0) continue;
                    dis[nx][ny] = dis[x][y] + 1;
                    from[nx][ny] = {x,y};
                    q.push({nx,ny});
                }
            }
            int nx = N - 1,ny = N - 1;
            stack<pair<int,int>> stk;
            while(nx != x || ny != y)
            {
                stk.push({nx,ny});
                auto [X,Y] = from[nx][ny];
                nx = X,ny = Y;
            }
            while(!stk.empty())
            {
                auto [X,Y] = stk.top(); stk.pop();
                a[X][Y] = '@'; print();
                a[X][Y] = ' '; Sleep(20);
            }
        }
        a[x][y] = '@'; print(); a[x][y] = ' ';
    }
    if(step == tar){puts("Congratulations!");}
    else puts("Work hard next time!");
}