版本比较
derekyang的迷宫(自己手搓)
正文
增加 261 行删除 1135 行
21 行未改动2223242526272829303130323233343536373839404142434142444445464546494748495250515253542 行未改动57585960636162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431445 行未改动1501511521537915415515682157158159160861611621631649016516616716816917017117298991731741751761771781791051061801811821831841101111851861871131881891151161901911921181931941201951221961971981992002012022032041321332052062071351362082092102111401412122132141431442152162172181481492192202211511522222232242251561572262272281591602292302312322337 行未改动24124224324417617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133224524624724824933825025125225334234325425525625725834725926026126235135226326435435526526626726835926927036136227127227336536627427527636836927727837127928028128228337628428528628738138238338428828929029129229329438838929529629729839339439529930030139739830230330430530640330730840640730931031131241141241341441541641741841931331431531631742342431831932032142842943032232332443243332532632732832933033144044133233333433533644644733744933833934034134234345334434534634734834935035135235335435535635735846735936046947047136136236336436536636736836947948048148248337037137237337437548648748837637737837938038138238338449649749838538638738838939039139239350639439539639739839951240040140240340440551840640740840941041141252552652752852953053141341441541653453553641741841942042142242342442554454554642642742842943043143243343455443543643743843944044144244344456456556656756856957057157257357457544544644744844945045145245345445557958045645745845958458558646046146258858946346446546646746846959659747047147247347460247560460560660760860961061161247647747847948048161661748248348448562162262348648748862562648949049149249349449563363449649749849950063964050150264350350450550664664750750850964951051165151251351451551651765765851851966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669752052152252352452570170270352652752852953053170853253371071153453553671353753853954054154254354454554672354754872572654955072855155255355455555655773555855973756056156256356474256556656756856974757057157257357475257557657757857975775858058158276058358458558658758858959076859159277059359459559659759859977760060160260360460560660760860961061161261361479079179279379461561661761861962079679779879980062162262362462580380462662762862980880963063163281281381463363463581681763663763863964082282364164264364464564664783064864965083365165283565365465583883965665765865984384466066166266366484966566666766885385466967067167267367485986086167567667767867986486568068168268368487068568668768868969069169287869369469569688269769869988570070188788870270389089170470589389470670770889789870971071171271390371471590571671790771871972091072172291272372472572691672772891872973073192173273373473573673773873974092993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161474174274321 行未改动$2026.4.19.12:00$ Linux版追上Windows版所有进度
$2026.4.19.22:30$ Windows版新增人机模式:topo(由不学C++的LHY同学提供)
$2026.7.14.19:00$ 突然想起来,优化了Windows的石山代码,删除了啥也不是的topo,增加了“融合版”(Win Lin 双版,but Lin版本没有测试过,不确定行不行)
补充:请确保您的终端能完整显示所有内容,否则一定会出现bug,Linux版若输出过快终端会有轻微问题,这是终端的问题,不要找我。
### 目前bug
topo现在有无穷个bug,待调整。喜欢欣赏bug可以使用该模式,否则不建议尝试(LHY正在调)
## Windows
## 融合版
```cpp//https://www.luogu.com.cn/article/qqm2loyo//https://www.luogu.com.cn/user/92860/article#include <bits/stdc++.h>#ifdef _WIN32#include <Windows.h>#include <conio.h>#include <time.h>using namespace std;#define ESC '\033'#define color(a) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a)int shiyexingshi;char mp[2005][2005];int n, m;void cls(){ HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if (hStdOut == INVALID_HANDLE_VALUE) if(hStdOut == INVALID_HANDLE_VALUE) return; CONSOLE_SCREEN_BUFFER_INFO csbi; if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) if(!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return; DWORD count; DWORD cellCount = csbi.dwSize.X * csbi.dwSize.Y; COORD homeCoords = {0, 0};2 行未改动 SetConsoleCursorPosition(hStdOut, homeCoords);}void move_print(int x, int y){ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD coordScreen = {(short)y, (short)x}; //这行指令就是反的 COORD coordScreen = {(short)y,(short)x}; //这行指令就是反的 SetConsoleCursorPosition(hConsole, coordScreen);}#else#include <unistd.h>#include <cstdlib>#include <termios.h>#include <fcntl.h>#define cls() system("clear");static char peek_char = 0;int kbhit(){ struct termios oldt, newt; int oldf; int bytes; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); char c; bytes = read(STDIN_FILENO, &c, 1); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (bytes > 0){ peek_char = c; return 1; } return 0;}char _getch(){ if (peek_char){ char c = peek_char; peek_char = 0; return c; } struct termios old_termios, new_termios; char c; tcgetattr(STDIN_FILENO, &old_termios); new_termios = old_termios; new_termios.c_lflag &= ~(ICANON | ECHO); new_termios.c_cc[VMIN] = 1; new_termios.c_cc[VTIME] = 0; tcsetattr(STDIN_FILENO, TCSANOW, &new_termios); read(STDIN_FILENO, &c, 1); tcsetattr(STDIN_FILENO, TCSANOW, &old_termios); return c;}void clear_input_buffer(){ char c; while ((c = getchar()) != '\n' && c != EOF);}void press_any_key_to_continue(){ fflush(stdout); tcflush(STDIN_FILENO, TCIFLUSH); struct termios old_tio, new_tio; tcgetattr(STDIN_FILENO, &old_tio); new_tio = old_tio; new_tio.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);}void move_print(int x, int y){ printf("\033[%d;%dH", x, y);}bool color(int x){ if(x == 10) printf("\033[1;32m"); else if(x == 7) printf("\033[0m"); else if(x == 9) printf("\033[1;36m"); else if(x == 8) return 0; else return 0; return 1;}#endif#define ESC '\033'using namespace std;int shiyexingshi;char mp[2005][2005];int n, m;int b[1005][1005];bool in(int x, int y){ return x >= 1 && x <= 2 * n - 1 && y >= 1 && y <= 2 * m - 1;}5 行未改动int yidong[5][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int lu[2005][2005]; //走过几次void dfs(int x, int y){ // cout<<x<<"ovo"<<y<<endl; if (b[x][y]) if(b[x][y]) return; b[x][y] = 1; if (x == n && y == m) if(x == n && y == m) return; int tmp = rand() % 4; int sum = 0; while ((!in_dfs(x + yidong[tmp][0], y + yidong[tmp][1]) || b[x + yidong[tmp][0]][y + yidong[tmp][1]]) && sum < 40){ while((!in_dfs(x + yidong[tmp][0], y + yidong[tmp][1]) || b[x + yidong[tmp][0]][y + yidong[tmp][1]]) && sum < 40){ tmp = rand() % 4, ++sum; // cout<<x+yidong[tmp][0]<<" "<<y+yidong[tmp][1]<<"qwq\n"; } if (sum == 40) if(sum == 40) return; dfs(x + yidong[tmp][0], y + yidong[tmp][1]); mp[x * 2 - 1 + yidong[tmp][0]][y * 2 - 1 + yidong[tmp][1]] = 'o';}bool g1[2005][2005];int keep_g1[4005][4005], last_g1[4005][4005]; //last:上一个位置void shengcheng(){ for (int i = 1; i <= n; ++i){ for (int j = 1; j <= m; ++j){ for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ mp[i * 2 - 1][j * 2 - 1] = 'o'; mp[i * 2][j * 2 - 1] = mp[i * 2 - 1][j * 2] = mp[i * 2][j * 2] = '#'; b[i][j] = 0; } } for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ for(int i = 0; i <= 2 * n; ++i){ for(int j = 0; j <= 2 * m; ++j){ g1[i][j] = keep_g1[i][j] = last_g1[i][j] = lu[i][j] = 0; } } for (int len = 2; len <= m + n; ++len){ for (int i = 1; i <= min(len - 1, n); ++i){ for(int len = 2; len <= m + n; ++len){ for(int i = 1; i <= min(len - 1, n); ++i){ int j = len - i; if (j > m) if(j > m) continue; if (!b[i][j]){ if (i != 1 || j != 1){ if(!b[i][j]){ if(i != 1 || j != 1){ int tmp = rand() % 4; while (!in_dfs(i + yidong[tmp][0], j + yidong[tmp][1]) || !b[i + yidong[tmp][0]][j + yidong[tmp][1]] || ((i == n - 1 || j == n - 1) && (i == n || j == n) && i + yidong[tmp][0] == n && j + yidong[tmp][1] == m)){ while(!in_dfs(i + yidong[tmp][0], j + yidong[tmp][1]) || !b[i + yidong[tmp][0]][j + yidong[tmp][1]] ||((i == n - 1 || j == n - 1) &&(i == n || j == n) && i + yidong[tmp][0] == n && j + yidong[tmp][1] == m)){ tmp = rand() % 4; // cout<<x+yidong[tmp][0]<<" "<<y+yidong[tmp][1]<<"qwq\n"; } // cout<<i<<" "<<j<<" "<<i+yidong[tmp][0]<<" "<<j+yidong[tmp][1]<<endl; mp[i * 2 - 1 + yidong[tmp][0]][j * 2 - 1 + yidong[tmp][1]] = 'o'; } dfs(i, j); } } }}bool shiye[4005][4005];void shiyeyunxing(int x, int y){ for (int i = x; i >= max(1, x - 2); --i){ //up if (mp[i][y] == '#'){ for(int i = x; i >= max(1, x - 2); --i){ //up if(mp[i][y] == '#'){ break; } else{ }else{ shiye[i][y] = shiye[i - 1][y] = shiye[i + 1][y] = shiye[i][y - 1] = shiye[i][y + 1] = shiye[i + 1][y + 1] = shiye[i - 1][y - 1] = shiye[i - 1][y + 1] = shiye[i + 1][y - 1] = 1; } } for (int i = x; i < min(2 * n, x + 3); ++i){ //down if (mp[i][y] == '#'){ for(int i = x; i < min(2 * n, x + 3); ++i){ //down if(mp[i][y] == '#'){ break; } else{ }else{ shiye[i][y] = shiye[i - 1][y] = shiye[i + 1][y] = shiye[i][y - 1] = shiye[i][y + 1] = shiye[i + 1][y + 1] = shiye[i - 1][y - 1] = shiye[i - 1][y + 1] = shiye[i + 1][y - 1] = 1; } } for (int i = y; i >= max(1, y - 2); --i){ //left if (mp[x][i] == '#'){ for(int i = y; i >= max(1, y - 2); --i){ //left if(mp[x][i] == '#'){ break; } else{ }else{ shiye[x][i] = shiye[x - 1][i] = shiye[x + 1][i] = shiye[x][i - 1] = shiye[x][i + 1] = shiye[x + 1][i + 1] = shiye[x - 1][i - 1] = shiye[x - 1][i + 1] = shiye[x + 1][i - 1] = 1; } } for (int i = y; i < min(2 * m, y + 3); ++i){ //right if (mp[x][i] == '#'){ for(int i = y; i < min(2 * m, y + 3); ++i){ //right if(mp[x][i] == '#'){ break; } else{ }else{ shiye[x][i] = shiye[x - 1][i] = shiye[x + 1][i] = shiye[x][i - 1] = shiye[x][i + 1] = shiye[x + 1][i + 1] = shiye[x - 1][i - 1] = shiye[x - 1][i + 1] = shiye[x + 1][i - 1] = 1; } }}7 行未改动 }};int pre[4005][4005][2]; int lst[2005][2005][2]; //指此节点第一次被走是哪个节点走的(方向)// 新增:道路类,存储一条路径上的所有节点(按顺序)struct Path { vector<pair<int,int>> nodes;};set<pair<int,int>> getConvergentPoints(int sx, int sy, int tx, int ty) { set<pair<int,int>> convergent; // 使用全局 pre 数组,重置为 -1(只重置可能用到的范围,提高效率) // 注意:pre 的大小是 4005x4005x2,全部 memset 约 128MB,可接受 memset(pre, -1, sizeof(pre)); queue<pair<int,int>> q; q.push({sx, sy}); pre[sx][sy][0] = sx; pre[sx][sy][1] = sy; bool found = false; while (!q.empty() && !found) { int x = q.front().first, y = q.front().second; q.pop(); for (int d = 0; d < 4; ++d) { int nx = x + yidong[d][0]; int ny = y + yidong[d][1]; if (!in(nx, ny)) continue; if (!keep_g1[nx][ny] || mp[nx][ny] != 'o') continue; if (pre[nx][ny][0] != -1) continue; // 已访问 pre[nx][ny][0] = x; pre[nx][ny][1] = y; if (nx == tx && ny == ty) { found = true; break; } q.push({nx, ny}); } } if (!found) return convergent; // 终点未探索,返回空集 // 回溯收集路径上的所有点(从终点到起点) int x = tx, y = ty; while (!(x == sx && y == sy)) { convergent.insert({x, y}); int px = pre[x][y][0]; int py = pre[x][y][1]; x = px; y = py; } convergent.insert({sx, sy}); return convergent;}pair<int,int> autoMove(int x, int y, int tx, int ty) { // 辅助函数:BFS 从 (sx,sy) 到 (ex,ey) 返回第一步 auto bfsStep = [&](int sx, int sy, int ex, int ey) -> pair<int,int> { memset(pre, -1, sizeof(pre)); queue<pair<int,int>> q; q.push({sx, sy}); pre[sx][sy][0] = sx; pre[sx][sy][1] = sy; bool found = false; while (!q.empty() && !found) { int cx = q.front().first, cy = q.front().second; q.pop(); for (int d = 0; d < 4; ++d) { int nx = cx + yidong[d][0]; int ny = cy + yidong[d][1]; if (!in(nx, ny)) continue; if (!keep_g1[nx][ny] || mp[nx][ny] != 'o') continue; if (pre[nx][ny][0] != -1) continue; pre[nx][ny][0] = cx; pre[nx][ny][1] = cy; if (nx == ex && ny == ey) { found = true; break; } q.push({nx, ny}); } } if (!found) return {sx, sy}; // 回溯第一步 int cx = ex, cy = ey; while (!(pre[cx][cy][0] == sx && pre[cx][cy][1] == sy)) { int px = pre[cx][cy][0]; int py = pre[cx][cy][1]; cx = px; cy = py; if (cx == sx && cy == sy) break; } return {cx, cy}; }; // 1. 终点已探索:直接 BFS 到终点 if (keep_g1[tx][ty] && mp[tx][ty] == 'o') { return bfsStep(x, y, tx, ty); } // 2. 终点未探索:分析已探索区域的拓扑结构 // 先计算每个格子的度数(已探索邻居数)和是否有未探索可走邻居 vector<pair<int,int>> endpoints; // 有未探索邻居的端点 vector<pair<int,int>> branchPoints; // 分支点(度数>=3) for (int i = 1; i <= 2*n-1; ++i) { for (int j = 1; j <= 2*m-1; ++j) { if (!keep_g1[i][j] || mp[i][j] != 'o') continue; int deg = 0; bool hasUnexplored = false; for (int d = 0; d < 4; ++d) { int ni = i + yidong[d][0]; int nj = j + yidong[d][1]; if (!in(ni, nj)) continue; if (keep_g1[ni][nj] && mp[ni][nj] == 'o') deg++; else if (!keep_g1[ni][nj] && mp[ni][nj] == 'o') hasUnexplored = true; } if (deg == 1 && hasUnexplored && (i != 1 || j != 1)) { endpoints.push_back({i, j}); } if (deg >= 3) { branchPoints.push_back({i, j}); } } } // 如果有有效端点,选择曼哈顿距离终点最近的 if (!endpoints.empty()) { sort(endpoints.begin(), endpoints.end(), [&](auto &a, auto &b) { int da = abs(a.first - tx) + abs(a.second - ty); int db = abs(b.first - tx) + abs(b.second - ty); return da < db; }); return bfsStep(x, y, endpoints[0].first, endpoints[0].second); } // 没有有效端点:说明所有分支都已走到死胡同末端,需要回溯到最近的分支点 if (!branchPoints.empty()) { // 选择离当前位置最近的分支点(BFS 距离最小) sort(branchPoints.begin(), branchPoints.end(), [&](auto &a, auto &b) { int da = abs(a.first - x) + abs(a.second - y); int db = abs(b.first - x) + abs(b.second - y); return da < db; }); return bfsStep(x, y, branchPoints[0].first, branchPoints[0].second); } // 保底:向任意未走过的邻居移动一步 for (int d = 0; d < 4; ++d) { int nx = x + yidong[d][0]; int ny = y + yidong[d][1]; if (in(nx, ny) && keep_g1[nx][ny] && mp[nx][ny] == 'o' && lu[nx][ny] == 0) { return {nx, ny}; } } // 实在无路,随便走一步 for (int d = 0; d < 4; ++d) { int nx = x + yidong[d][0]; int ny = y + yidong[d][1]; if (in(nx, ny) && keep_g1[nx][ny] && mp[nx][ny] == 'o') { return {nx, ny}; } } return {x, y};}bool bfsb[2005][2005];priority_queue<Node> q;int bfs(int x, int y){ //return way // cls(); // cout<<x<<"---"<<y<<"---\n"; while (!q.empty()) while(!q.empty()) q.pop(); Node tmp = {x, y, 0, 0, 0}; q.push(tmp); for (int i = 1; i <= 2 * n; ++i){ for (int j = 1; j <= 2 * m; ++j){ for(int i = 1; i <= 2 * n; ++i){ for(int j = 1; j <= 2 * m; ++j){ bfsb[i][j] = 0; } } while (!q.empty()){ while(!q.empty()){ Node now = q.top(); q.pop(); Node nxt = {0, 0, 0, 0, 0}; // cout<<now.x<<" "<<now.y<<endl; for (int i = 1; i <= 4; ++i){ for(int i = 1; i <= 4; ++i){ int tx = now.x + xy[i][0], ty = now.y + xy[i][1]; // if(!bfsb[tx][ty]&&in(tx,ty))cout<<tx<<" "<<ty<<" "<<(g1[tx][ty]&&mp[tx][ty]=='#')<<" "<<q.empty()<<" "; if (!in(tx, ty) || (keep_g1[tx][ty] && mp[tx][ty] == '#') || bfsb[tx][ty]) if(!in(tx, ty) ||(keep_g1[tx][ty] && mp[tx][ty] == '#') || bfsb[tx][ty]) continue; nxt.x = tx, nxt.y = ty; bfsb[tx][ty] = 1; if (now.way == 0){ if(now.way == 0){ nxt.way = i; } else }else nxt.way = now.way; nxt.dis = now.dis + 1; nxt.vis = (lu[tx][ty]) ? 2 : 1; if (lu[tx][ty]) nxt.vis =(lu[tx][ty]) ? 2 : 1; if(lu[tx][ty]) ++nxt.dis; // cout<<tx<<" "<<ty<<" "<<nxt.way<<" "<<q.size()<<" "; if (tx == 2 * n - 1 && ty == 2 * m - 1) if(tx == 2 * n - 1 && ty == 2 * m - 1) return nxt.way; if (/*lu[tx][ty]||*/ (now.vis == 1 && nxt.vis == 2)) if(/*lu[tx][ty]||*/(now.vis == 1 && nxt.vis == 2)) continue; q.push(nxt); } } // cout<<"出现bug\n"; return 0;}int last_shiye[4005][4005];void out_map(int x, int y){ if (shiyexingshi){ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ for(int i = 0; i <= 2 * n; ++i){ for(int j = 0; j <= 2 * m; ++j){ if(shiyexingshi){ if(x == i && y == j){ color(10); printf("[]"); color(7); } else if ((shiye[i][j] || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ }else if((shiye[i][j] ||(gua1 && g1[i][j])) && gua2 && lu[i][j]){ color(9); printf("--"); color(7); } else if (shiye[i][j] || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ }else if(shiye[i][j] ||(gua1 && g1[i][j])){ if(mp[i][j] == 'o'){ printf(" "); } else{ }else{ color(8); printf("##"); color(7); } // cout<<i<<" "<<j<<" "<<g1[i][j]<<endl;_getch(); keep_g1[i][j] = 1; g1[i][j] = 1; } else{ }else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } } else{ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ }else{ if(x == i && y == j){ color(10); printf("[]"); color(7); } else if ((abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ }else if((abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) ||(gua1 && g1[i][j])) && gua2 && lu[i][j]){ color(9); printf("--"); color(7); } else if (abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ }else if(abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) ||(gua1 && g1[i][j])){ if(mp[i][j] == 'o'){ printf(" "); } else{ }else{ color(8); printf("##"); color(7); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ }else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } }; printf("\n"); } cout.flush();}int min_y[2005], max_y[2005];void yunxing(){
#ifdef _WIN32 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(hConsole, &cursorInfo); COORD bufferSize = {120, 300}; SetConsoleScreenBufferSize(hConsole, bufferSize);#endif int x = 1, y = 1, sum = 0; lu[1][1] = 1; cls(); mp[0][0] = '#'; g1[1][1] = 1; out_map(x, y); while (1){ while(1){ int shiyex = x - 1, max_x = x + 1; // cls(); for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ for(int i = 0; i <= 2 * n; ++i){ for(int j = 0; j <= 2 * m; ++j){ shiye[i][j] = 0; } min_y[i] = y - 1; max_y[i] = y + 1; } shiyeyunxing(x, y);
// cout<<g1[20][20]<<" "<<g1[19][19]<<endl; if (shiyexingshi){ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ for(int i = 0; i <= 2 * n; ++i){ for(int j = 0; j <= 2 * m; ++j){ if(shiyexingshi){ if(x == i && y == j){ shiyex = min(shiyex, i); max_x = max(max_x, i); } else if (shiye[i][j] || (gua1 && g1[i][j])){ if (!g1[i][j]){ }else if(shiye[i][j] ||(gua1 && g1[i][j])){ if(!g1[i][j]){ min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); shiyex = min(shiyex, i); max_x = max(max_x, i); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ if (last_g1[i][j] != g1[i][j]){ }else{ if(last_g1[i][j] != g1[i][j]){ min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); shiyex = min(shiyex, i); max_x = max(max_x, i); } g1[i][j] = 0; } if (last_g1[i][j] != g1[i][j]){ if(last_g1[i][j] != g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); } if (shiye[i][j] != g1[i][j] && !gua1){ if(shiye[i][j] != g1[i][j] && !gua1){ min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); shiyex = min(shiyex, i); max_x = max(max_x, i); } if (gua1 && (shiye[i][j] && !g1[i][j])){ if(gua1 &&(shiye[i][j] && !g1[i][j])){ min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); shiyex = min(shiyex, i); max_x = max(max_x, i); } last_g1[i][j] = g1[i][j]; } } } else{ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ }else{ if(x == i && y == j){ shiyex = min(shiyex, i); max_x = max(max_x, i); } else if (abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])){ if (!g1[i][j]){ }else if(abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) ||(gua1 && g1[i][j])){ if(!g1[i][j]){ min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); shiyex = min(shiyex, i); max_x = max(max_x, i); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ if (last_g1[i][j] != g1[i][j]){ }else{ if(last_g1[i][j] != g1[i][j]){ min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); shiyex = min(shiyex, i); max_x = max(max_x, i); } g1[i][j] = 0; } if (last_g1[i][j] != g1[i][j]){ if(last_g1[i][j] != g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); min_y[i] = min(min_y[i], j); max_y[i] = max(max_y[i], j); } last_g1[i][j] = g1[i][j]; } } } // shiyex--; // shiyex=max(shiyex,0); // max_x++; // max_x=min(max_x,2*n); if (shiyexingshi){ for (int i = shiyex; i <= max_x; ++i){ min_y[i] = max(min_y[i], 0); max_y[i] = min(max_y[i], 2 * m); // cout<<i<<" "<<min_y[i]<<" "<<max_y[i]<<endl; move_print(i, 2 * min_y[i]); for (int j = min_y[i]; j <= max_y[i]; ++j){ if (x == i && y == j){
for(int i = shiyex; i <= max_x; ++i){ min_y[i] = max(min_y[i], 0); max_y[i] = min(max_y[i], 2 * m); move_print(i, 2 * min_y[i]); if(shiyexingshi){ for(int j = min_y[i]; j <= max_y[i]; ++j){ if(x == i && y == j){ color(10); printf("[]"); color(7); } else if ((shiye[i][j] || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ }else if((shiye[i][j] ||(gua1 && g1[i][j])) && gua2 && lu[i][j]){ color(9); printf("--"); color(7); } else if (shiye[i][j] || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ }else if(shiye[i][j] ||(gua1 && g1[i][j])){ if(mp[i][j] == 'o'){ printf(" "); } else{ }else{ color(8); printf("##"); color(7); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ }else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } } else{ for (int i = shiyex; i <= max_x; ++i){ min_y[i] = max(min_y[i], 0); max_y[i] = min(max_y[i], 2 * m); move_print(i, 2 * min_y[i]); for (int j = min_y[i]; j <= max_y[i]; ++j){ if (x == i && y == j){ }else{ for(int j = min_y[i]; j <= max_y[i]; ++j){ if(x == i && y == j){ color(10); printf("[]"); color(7); } else if ((abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ }else if((abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) ||(gua1 && g1[i][j])) && gua2 && lu[i][j]){ color(9); printf("--"); color(7); } else if (abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ }else if(abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) ||(gua1 && g1[i][j])){ if(mp[i][j] == 'o'){ printf(" "); } else{ }else{ color(8); printf("##"); color(7); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ }else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; // cout<<i<<" "<<min_y[i]<<" "<<max_y[i]<<endl; printf("\n"); } }; } cout.flush(); move_print(2 * n + 1, 0); bool flag = 0; printf("步数: %d 位置: (%d,%d)", sum, (x + 1) / 2, (y + 1) / 2); if (gua1) printf("步数: %d 位置:(%d,%d)", sum,(x + 1) / 2,(y + 1) / 2); if(gua1) printf(" [视野遗留模式]"); if (gua2) if(gua2) printf(" [脚步留痕模式]"); if (shiyexingshi) if(shiyexingshi) printf(" [视野模式1]"); else printf(" [视野模式2]"); printf("\n"); printf("使用 WASD 移动,B 退出\n"); // printf("%d %d",shiyex,max_x); //cout<<g1[20][20]<<" "<<g1[19][19]<<endl; cout.flush(); char c; // c=_getch(); if(zidong == 1){ // 自动移动(基于收敛点) if(kbhit()){ char c = _getch(); if(c=='b'){ printf("确定结束吗(确定按y不确定按任意健)"); c=_getch(); if(c=='y'){flag=1;break;} cls(); out_map(x,y); } } pair<int,int> nxt = autoMove(x, y, 2*n-1, 2*m-1); // 防止卡死:若无法移动,尝试随机走一步 if (nxt.first == x && nxt.second == y) { for (int d = 0; d < 4; ++d) { int nx = x + yidong[d][0]; int ny = y + yidong[d][1]; if (in(nx, ny) && keep_g1[nx][ny] && mp[nx][ny] == 'o') { nxt = {nx, ny}; break; } } if (nxt.first == x && nxt.second == y) { // 完全无路可走,结束自动模式(或跳出) break; } } x = nxt.first; y = nxt.second; ++lu[x][y]; ++sum; // 可选:记录父节点(用于回溯) // lst[x][y][0] = x - (nxt.first - x); // lst[x][y][1] = y - (nxt.second - y); }else if(zidong == 2){ if (xy[1][0] == 1){ //目前优先向下 if (rand() % (2 * n - x + 2 * m - y) < 2 * m - y){ if(zidong){ if(xy[1][0] == 1){ //目前优先向下 if(rand() %(2 * n - x + 2 * m - y) < 2 * m - y){ swap(xy[1][0], xy[2][0]); swap(xy[1][1], xy[2][1]); } } else{ //优先向右 if (rand() % (2 * n - x + 2 * m - y) < 2 * n - x){ }else{ //优先向右 if(rand() %(2 * n - x + 2 * m - y) < 2 * n - x){ swap(xy[1][0], xy[2][0]); swap(xy[1][1], xy[2][1]); } } if (kbhit()){ if(kbhit() || zidong == 1){ c = _getch(); if (c == 'b'){ printf("确定结束吗(确定按y不确定按任意健)"); if(c == 'b'){ printf("确定结束吗(确定按y不确定按任意健)"); cout.flush(); c = _getch(); if (c == 'y'){ if(c == 'y'){ flag = 1; break; } cls(); out_map(x, y); } } // cout<<xy[1][1]<<" "<<xy[1][0]<<endl; int way = bfs(x, y); if (way == 0){ if(way == 0){ x = lst[x][y][0], y = lst[x][y][1]; } else{ }else{ x += xy[way][0], y += xy[way][1]; if (!lu[x][y]){ if(!lu[x][y]){ lst[x][y][0] = x - xy[way][0]; lst[x][y][1] = y - xy[way][1]; } ++lu[x][y]; ++sum; } }else while (1){ }else while(1){ c = _getch(); if (c == 'w' && x > 0 && mp[x - 1][y] == 'o'){ if(c == 'w' && x > 0 && mp[x - 1][y] == 'o'){ --x, ++sum; ++lu[x][y]; break; } if (c == 'a' && y > 0 && mp[x][y - 1] == 'o'){ if(c == 'a' && y > 0 && mp[x][y - 1] == 'o'){ --y, ++sum; ++lu[x][y]; break; } if (c == 's' && x < 2 * n && mp[x + 1][y] == 'o'){ if(c == 's' && x < 2 * n && mp[x + 1][y] == 'o'){ ++x, ++sum; ++lu[x][y]; break; } if (c == 'd' && y < 2 * m && mp[x][y + 1] == 'o'){ if(c == 'd' && y < 2 * m && mp[x][y + 1] == 'o'){ ++y, ++sum; ++lu[x][y]; break; } if (c == 'b'){ printf("确定结束吗(确定按y不确定按任意健)"); if(c == 'b'){ printf("确定结束吗(确定按y不确定按任意健)"); cout.flush(); c = _getch(); if (c == 'y'){ if(c == 'y'){ flag = 1; break; } cls(); break; } } if (flag) if(flag) break; if (x == 2 * n - 1 && y == 2 * m - 1){ if(x == 2 * n - 1 && y == 2 * m - 1){ cls(); printf("╔════════════════════════════════╗\n"); printf("║ 恭喜!到达终点! ║\n"); printf("╠════════════════════════════════╣\n"); printf("║ 总共走了 %5d 步 ║\n", sum); printf("╚════════════════════════════════╝\n"); printf("按任意键继续..."); printf("按任意键继续..."); cout.flush(); _getch(); break; } }#ifdef _WIN32 cursorInfo.bVisible = TRUE; SetConsoleCursorInfo(hConsole, &cursorInfo);#endif const int FOOTNUMBER = 1; const int FOOTPRINT = 2; const int DONTSHOW = 0; int show_foot = 1; bool show_all = 0; cls(); while (1){ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (lu[i][j]){ if (show_foot == FOOTNUMBER){ while(1){ for(int i = 0; i <= 2 * n; ++i){ for(int j = 0; j <= 2 * m; ++j){ if(lu[i][j]){ if(show_foot == FOOTNUMBER){ color(9); if (lu[i][j] < 10) printf("%d ", lu[i][j]); else if (lu[i][j] < 100) printf("%d", lu[i][j]); else if(lu[i][j] < 10) printf("%d ", lu[i][j]);else if(lu[i][j] < 100) printf("%d", lu[i][j]);else printf("99"); color(7); } else if (show_foot == FOOTPRINT){ }else if(show_foot == FOOTPRINT){ color(9); printf("--"); color(7); } else{ }else{ printf(" "); } } else if (keep_g1[i][j] || show_all){ if (mp[i][j] == 'o'){ }else if(keep_g1[i][j] || show_all){ if(mp[i][j] == 'o'){ printf(" "); } else{ }else{ color(8); printf("##"); color(7); } } else{ }else{ printf(".."); } } printf("\n"); } printf("你走了%d步\n", sum); printf("按q或b退出,按a或n有不同迷宫视角\n"); printf("按q或b退出,按a或n有不同迷宫视角\n"); cout.flush(); char c = 0; bool flag = 0; while (!(c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b' || c == 'n' || c == 'N' || c == 'a' || c == 'A')){ while(!(c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b' || c == 'n' || c == 'N' || c == 'a' || c == 'A')){ c = _getch(); if (c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b'){ if(c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b'){ flag = 1; break; } else if (c == 'n' || c == 'N'){ }else if(c == 'n' || c == 'N'){ show_foot++; show_foot %= 3; break; } else if (c == 'a' || c == 'A'){ }else if(c == 'a' || c == 'A'){ show_all = !show_all; break; } } if (flag) if(flag) break; move_print(0, 0); } for (int i = 0; i <= 2 * n; ++i) for (int j = 0; j <= 2 * m; ++j){ for(int i = 0; i <= 2 * n; ++i) for(int j = 0; j <= 2 * m; ++j){ g1[i][j] = keep_g1[i][j] = last_g1[i][j] = lu[i][j] = shiye[i][j] = 0; }}int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // ios::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); system("title derekyang的迷宫游戏"); color(7);
while (1){ while(1){ srand(time(NULL));
// 显示特殊模式设置菜单 bool setting = true; while (setting){ while(setting){ cls(); printf("\n═══════════════════════════════════\n"); printf(" 当前特殊模式设置\n"); printf("───────────────────────────────────\n"); printf(" 1. 视野遗留模式 : %s\n", gua1 ? "开启" : "关闭"); printf(" 2. 脚步留痕模式 : %s\n", gua2 ? "开启" : "关闭"); printf(" 3. 视野模式 : %s\n", shiyexingshi ? "模式1(精确视野)" : "模式2(距离视野)"); printf(" 4. 人机接管模式 : %s\n", (zidong == 0) ? "关闭" : ((zidong == 1) ? "topo" : "A*")); printf(" 4. 人机接管模式 : %s\n",(zidong == 0) ? "关闭" :((zidong == 1) ? "半自动" : "全自动(很快)")); printf("───────────────────────────────────\n"); printf(" 按数字键切换对应模式,按 [Y] 开始输入边数列数\n"); printf("═══════════════════════════════════\n"); printf("请选择: "); printf("请选择: "); cout.flush();
char opt = _getch(); if (opt == '1'){ if(opt == '1'){ gua1 = !gua1; } else if (opt == '2'){ }else if(opt == '2'){ gua2 = !gua2; } else if (opt == '3'){ }else if(opt == '3'){ shiyexingshi = !shiyexingshi; } else if (opt == '4'){ }else if(opt == '4'){ ++zidong; zidong %= 3; } else if (opt == 'y' || opt == 'Y'){ }else if(opt == 'y' || opt == 'Y'){ setting = false; // 开始游戏 } // 忽略其他按键 } printf("\n请输入迷宫大小 (n m): "); printf("\n请输入迷宫大小(n m): ");cout.flush(); cin >> n >> m; if (n < 2) if(n < 2) n = 2; if (m < 2) if(m < 2) m = 2;
printf("\n正在生成迷宫..."); printf("\n正在生成迷宫...");cout.flush(); shengcheng(); printf("迷宫生成完成!按任意键开始游戏..."); printf("迷宫生成完成!按任意键开始游戏...");cout.flush(); _getch(); yunxing();
printf("\n游戏结束!是否继续?(y/n): "); printf("\n游戏结束!是否继续?(y/n): ");cout.flush(); char c = _getch(); while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'){ while(c != 'y' && c != 'Y' && c != 'n' && c != 'N'){ c = _getch(); } if (c == 'n' || c == 'N'){ if(c == 'n' || c == 'N'){ break; } printf("\n"); } printf("欢迎下次再来! made by derekyang");cout.flush(); return 0;}```
## Linux
```cpp//https://www.luogu.com.cn/article/qqm2loyo//https://www.luogu.com.cn/user/92860/article#include <bits/stdc++.h>#include <unistd.h>#include <cstdlib>#include <termios.h>#include <fcntl.h>using namespace std;#define ESC '\033'int shiyexingshi;char mp[2005][2005];int n, m;static char peek_char = 0;int kbhit(){ struct termios oldt, newt; int oldf; int bytes; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); char c; bytes = read(STDIN_FILENO, &c, 1); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if (bytes > 0){ peek_char = c; return 1; } return 0;}char _getch(){ if (peek_char){ char c = peek_char; peek_char = 0; return c; } struct termios old_termios, new_termios; char c; tcgetattr(STDIN_FILENO, &old_termios); new_termios = old_termios; new_termios.c_lflag &= ~(ICANON | ECHO); new_termios.c_cc[VMIN] = 1; new_termios.c_cc[VTIME] = 0; tcsetattr(STDIN_FILENO, TCSANOW, &new_termios); read(STDIN_FILENO, &c, 1); tcsetattr(STDIN_FILENO, TCSANOW, &old_termios); return c;}void clear_input_buffer(){ char c; while ((c = getchar()) != '\n' && c != EOF) ;}void press_any_key_to_continue(){ fflush(stdout); tcflush(STDIN_FILENO, TCIFLUSH); struct termios old_tio, new_tio; tcgetattr(STDIN_FILENO, &old_tio); new_tio = old_tio; new_tio.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);}void move_print(int x, int y){ printf("\033[%d;%dH", x, y);}int b[1005][1005];bool in(int x, int y){ return x >= 1 && x <= 2 * n - 1 && y >= 1 && y <= 2 * m - 1;}bool in_dfs(int x, int y){ return 1 <= x && x <= n && 1 <= y && y <= m;}bool gua1; //视野遗留bool gua2; //脚步留痕int yidong[5][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int lu[2005][2005]; //走过几次void dfs(int x, int y){ // cout<<x<<"ovo"<<y<<endl; if (b[x][y]) return; b[x][y] = 1; if (x == n && y == m) return; int tmp = rand() % 4; int sum = 0; while ((!in_dfs(x + yidong[tmp][0], y + yidong[tmp][1]) || b[x + yidong[tmp][0]][y + yidong[tmp][1]]) && sum < 40){ tmp = rand() % 4, ++sum; // cout<<x+yidong[tmp][0]<<" "<<y+yidong[tmp][1]<<"qwq\n"; } if (sum == 40) return; dfs(x + yidong[tmp][0], y + yidong[tmp][1]); mp[x * 2 - 1 + yidong[tmp][0]][y * 2 - 1 + yidong[tmp][1]] = 'o';}bool g1[2005][2005];int keep_g1[4005][4005], last_g1[4005][4005]; //last:上一个位置void shengcheng(){ for (int i = 1; i <= n; ++i){ for (int j = 1; j <= m; ++j){ mp[i * 2 - 1][j * 2 - 1] = 'o'; mp[i * 2][j * 2 - 1] = mp[i * 2 - 1][j * 2] = mp[i * 2][j * 2] = '#'; b[i][j] = 0; } } for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ g1[i][j] = keep_g1[i][j] = last_g1[i][j] = lu[i][j] = 0; } } for (int len = 2; len <= m + n; ++len){ for (int i = 1; i <= min(len - 1, n); ++i){ int j = len - i; if (j > m) continue; if (!b[i][j]){ if (i != 1 || j != 1){ int tmp = rand() % 4; while (!in_dfs(i + yidong[tmp][0], j + yidong[tmp][1]) || !b[i + yidong[tmp][0]][j + yidong[tmp][1]] || ((i == n - 1 || j == n - 1) && (i == n || j == n) && i + yidong[tmp][0] == n && j + yidong[tmp][1] == m)){ tmp = rand() % 4; // cout<<x+yidong[tmp][0]<<" "<<y+yidong[tmp][1]<<"qwq\n"; } // cout<<i<<" "<<j<<" "<<i+yidong[tmp][0]<<" "<<j+yidong[tmp][1]<<endl; mp[i * 2 - 1 + yidong[tmp][0]][j * 2 - 1 + yidong[tmp][1]] = 'o'; } dfs(i, j); } } }}bool shiye[4005][4005];void shiyeyunxing(int x, int y){ for (int i = x; i >= max(1, x - 2); --i){ //up if (mp[i][y] == '#'){ break; } else{ shiye[i][y] = shiye[i - 1][y] = shiye[i + 1][y] = shiye[i][y - 1] = shiye[i][y + 1] = shiye[i + 1][y + 1] = shiye[i - 1][y - 1] = shiye[i - 1][y + 1] = shiye[i + 1][y - 1] = 1; } } for (int i = x; i < min(2 * n, x + 3); ++i){ //down if (mp[i][y] == '#'){ break; } else{ shiye[i][y] = shiye[i - 1][y] = shiye[i + 1][y] = shiye[i][y - 1] = shiye[i][y + 1] = shiye[i + 1][y + 1] = shiye[i - 1][y - 1] = shiye[i - 1][y + 1] = shiye[i + 1][y - 1] = 1; } } for (int i = y; i >= max(1, y - 2); --i){ //left if (mp[x][i] == '#'){ break; } else{ shiye[x][i] = shiye[x - 1][i] = shiye[x + 1][i] = shiye[x][i - 1] = shiye[x][i + 1] = shiye[x + 1][i + 1] = shiye[x - 1][i - 1] = shiye[x - 1][i + 1] = shiye[x + 1][i - 1] = 1; } } for (int i = y; i < min(2 * m, y + 3); ++i){ //right if (mp[x][i] == '#'){ break; } else{ shiye[x][i] = shiye[x - 1][i] = shiye[x + 1][i] = shiye[x][i - 1] = shiye[x][i + 1] = shiye[x + 1][i + 1] = shiye[x - 1][i - 1] = shiye[x - 1][i + 1] = shiye[x + 1][i - 1] = 1; } }}bool zidong;int xy[5][2] = {{0, 0}, {0, 1}, {1, 0}, {-1, 0}, {0, -1}};struct Node{ int x, y, way, dis; //way表示第一步向哪里走 int vis; //1:未经过节点 2:经过已经过节点 bool operator<(const Node &a) const{ return vis > a.vis; }};int lst[2005][2005][2]; //指此节点第一次被走是哪个节点走的(方向)bool bfsb[2005][2005];priority_queue<Node> q;int bfs(int x, int y){ //return way // system("clear"); // cout<<x<<"---"<<y<<"---\n"; while (!q.empty()) q.pop(); Node tmp = {x, y, 0, 0, 0}; q.push(tmp); for (int i = 1; i <= 2 * n; ++i){ for (int j = 1; j <= 2 * m; ++j){ bfsb[i][j] = 0; } } while (!q.empty()){ Node now = q.top(); q.pop(); Node nxt = {0, 0, 0, 0, 0}; // cout<<now.x<<" "<<now.y<<endl; for (int i = 1; i <= 4; ++i){ int tx = now.x + xy[i][0], ty = now.y + xy[i][1]; // if(!bfsb[tx][ty]&&in(tx,ty))cout<<tx<<" "<<ty<<" "<<(g1[tx][ty]&&mp[tx][ty]=='#')<<" "<<q.empty()<<" "; if (!in(tx, ty) || (keep_g1[tx][ty] && mp[tx][ty] == '#') || bfsb[tx][ty]) continue; nxt.x = tx, nxt.y = ty; bfsb[tx][ty] = 1; if (now.way == 0){ nxt.way = i; } else nxt.way = now.way; nxt.dis = now.dis + 1; nxt.vis = (lu[tx][ty]) ? 2 : 1; if (lu[tx][ty]) ++nxt.dis; // cout<<tx<<" "<<ty<<" "<<nxt.way<<" "<<q.size()<<" "; if (tx == 2 * n - 1 && ty == 2 * m - 1) return nxt.way; if (/*lu[tx][ty]||*/ (now.vis == 1 && nxt.vis == 2)) continue; q.push(nxt); } } // cout<<"出现bug\n"; return 0;}int last_shiye[4005][4005];void out_map(int x, int y){ if (shiyexingshi){ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ printf("\033[1;32m"); printf("[]"); printf("\033[0m"); } else if ((shiye[i][j] || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ printf("\033[1;36m"); printf("--"); printf("\033[0m"); } else if (shiye[i][j] || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ printf(" "); } else{ printf("██"); } // cout<<i<<" "<<j<<" "<<g1[i][j]<<endl;_getch(); keep_g1[i][j] = 1; g1[i][j] = 1; } else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } } else{ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ printf("\033[1;32m"); printf("[]"); printf("\033[0m"); } else if ((abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ printf("\033[1;36m"); printf("--"); printf("\033[0m"); } else if (abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ printf(" "); } else{ printf("██"); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } };}void yunxing(){ int x = 1, y = 1, sum = 0; lu[1][1] = 1; system("clear"); mp[0][0] = '#'; g1[1][1] = 1; out_map(x, y); while (1){ int shiyex = x - 1, max_x = x + 1; // system("clear"); for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ shiye[i][j] = 0; } } shiyeyunxing(x, y);
// cout<<g1[20][20]<<" "<<g1[19][19]<<endl; if (shiyexingshi){ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ shiyex = min(shiyex, i); max_x = max(max_x, i); } else if (shiye[i][j] || (gua1 && g1[i][j])){ if (!g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ if (last_g1[i][j] != g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); } g1[i][j] = 0; } if (last_g1[i][j] != g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); } last_g1[i][j] = g1[i][j]; } } } else{ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ shiyex = min(shiyex, i); max_x = max(max_x, i); } else if (abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])){ if (!g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ if (last_g1[i][j] != g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); } g1[i][j] = 0; } if (last_g1[i][j] != g1[i][j]){ shiyex = min(shiyex, i); max_x = max(max_x, i); } last_g1[i][j] = g1[i][j]; } } } // shiyex--; // shiyex=max(shiyex,0); // max_x++; // max_x=min(max_x,2*n); move_print(shiyex + 1, 0); if (shiyexingshi){ for (int i = shiyex; i <= max_x; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ printf("\033[1;32m"); printf("[]"); printf("\033[0m"); } else if ((shiye[i][j] || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ printf("\033[1;36m"); printf("--"); printf("\033[0m"); } else if (shiye[i][j] || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ printf(" "); } else{ printf("██"); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } } else{ for (int i = shiyex; i <= max_x; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (x == i && y == j){ printf("\033[1;32m"); printf("[]"); printf("\033[0m"); } else if ((abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])) && gua2 && lu[i][j]){ printf("\033[1;36m"); printf("--"); printf("\033[0m"); } else if (abs(i - x) + abs(j - y) <= max(2, 2 * n / 10) || (gua1 && g1[i][j])){ if (mp[i][j] == 'o'){ printf(" "); } else{ printf("██"); } keep_g1[i][j] = 1; g1[i][j] = 1; } else{ g1[i][j] = 0; printf(".."); } } // else cout<<mp[i][j]<<" "; printf("\n"); } }; move_print(2 * n + 1, 0); bool flag = 0; printf("步数: %d 位置: (%d,%d)", sum, (x + 1) / 2, (y + 1) / 2); if (gua1) printf(" [视野遗留模式]"); if (gua2) printf(" [脚步留痕模式]"); if (shiyexingshi) printf(" [视野模式1]"); else printf(" [视野模式2]"); printf("\n"); printf("使用 WASD 移动,B 退出\n"); // printf("%d %d",shiyex,max_x); //cout<<g1[20][20]<<" "<<g1[19][19]<<endl; char c; // c=_getch(); if (zidong){ if (xy[1][0] == 1){ //目前优先向下 if (rand() % (2 * n - x + 2 * m - y) < 2 * m - y){ swap(xy[1][0], xy[2][0]); swap(xy[1][1], xy[2][1]); } } else{ //优先向右 if (rand() % (2 * n - x + 2 * m - y) < 2 * n - x){ swap(xy[1][0], xy[2][0]); swap(xy[1][1], xy[2][1]); } } if (kbhit()){ c=_getch(); if(c=='b'){ printf("确定结束吗(确定按y不确定按任意健)\n"); c = _getch(); if (c == 'y'){ flag = 1; break; } system("clear"); out_map(x, y); } } // cout<<xy[1][1]<<" "<<xy[1][0]<<endl; int way = bfs(x, y); if (way == 0){ x = lst[x][y][0], y = lst[x][y][1]; } else{ x += xy[way][0], y += xy[way][1]; if (!lu[x][y]){ lst[x][y][0] = x - xy[way][0]; lst[x][y][1] = y - xy[way][1]; } ++lu[x][y]; ++sum; } } else while (1){ c = _getch(); if (c == 'w' && x > 0 && mp[x - 1][y] == 'o'){ --x, ++sum; ++lu[x][y]; break; } if (c == 'a' && y > 0 && mp[x][y - 1] == 'o'){ --y, ++sum; ++lu[x][y]; break; } if (c == 's' && x < 2 * n && mp[x + 1][y] == 'o'){ ++x, ++sum; ++lu[x][y]; break; } if (c == 'd' && y < 2 * m && mp[x][y + 1] == 'o'){ ++y, ++sum; ++lu[x][y]; break; } if (c == 'b'){ printf("确定结束吗(确定按y不确定按任意健)"); c = _getch(); if (c == 'y'){ flag = 1; break; } system("clear"); break; } } if (flag) break; if (x == 2 * n - 1 && y == 2 * m - 1){ system("clear"); printf("╔════════════════════════════════╗\n"); printf("║ 恭喜!到达终点! ║\n"); printf("╠════════════════════════════════╣\n"); printf("║ 总共走了 %5d 步 ║\n", sum); printf("╚════════════════════════════════╝\n"); printf("按任意键继续..."); press_any_key_to_continue(); break; } } const int FOOTNUMBER = 1; const int FOOTPRINT = 2; const int DONTSHOW = 0; int show_foot = 1; bool show_all = 0; system("clear"); while (1){ for (int i = 0; i <= 2 * n; ++i){ for (int j = 0; j <= 2 * m; ++j){ if (lu[i][j]){ if (show_foot == FOOTNUMBER){ printf("\033[1;36m"); if (lu[i][j] < 10) printf("%d ", lu[i][j]); else if (lu[i][j] < 100) printf("%d", lu[i][j]); else printf("99"); printf("\033[0m"); } else if (show_foot == FOOTPRINT){ printf("\033[1;36m"); printf("--"); printf("\033[0m"); } else{ printf(" "); } } else if (keep_g1[i][j] || show_all){ if (mp[i][j] == 'o'){ printf(" "); } else{ printf("██"); } } else{ printf(".."); } } printf("\n"); } printf("你走了%d步\n", sum); printf("按q或b退出,按a或n有不同迷宫视角\n"); char c = _getch(); while (!(c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b' || c == 'n' || c == 'N' || c == 'a' || c == 'A')){ c = _getch(); } if (c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b'){ break; } else if (c == 'n' || c == 'N'){ show_foot++; show_foot %= 3; } else if (c == 'a' || c == 'A'){ show_all = !show_all; } move_print(0, 0); } for (int i = 0; i <= 2 * n; ++i) for (int j = 0; j <= 2 * m; ++j){ g1[i][j] = keep_g1[i][j] = last_g1[i][j] = lu[i][j] = shiye[i][j] = 0; }}int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); system("title derekyang的迷宫游戏"); while (1){ srand(time(NULL));
// 显示特殊模式设置菜单 bool setting = true; while (setting){ system("clear"); printf("\n═══════════════════════════════════\n"); printf(" 当前特殊模式设置\n"); printf("───────────────────────────────────\n"); printf(" 1. 视野遗留模式 : %s\n", gua1 ? "开启" : "关闭"); printf(" 2. 脚步留痕模式 : %s\n", gua2 ? "开启" : "关闭"); printf(" 3. 视野模式 : %s\n", shiyexingshi ? "模式1(精确视野)" : "模式2(距离视野)"); printf(" 4. 人机接管模式 : %s\n", zidong ? "开启" : "关闭"); printf("───────────────────────────────────\n"); printf(" 按数字键切换对应模式,按 [Y] 开始输入边数列数\n"); printf("═══════════════════════════════════\n"); printf("请选择: \n");cout.flush();
char opt = _getch(); if (opt == '1'){ gua1 = !gua1; } else if (opt == '2'){ gua2 = !gua2; } else if (opt == '3'){ shiyexingshi = !shiyexingshi; } else if (opt == '4'){ zidong = !zidong; } else if (opt == 'y' || opt == 'Y'){ setting = false; // 开始游戏 } // 忽略其他按键 } printf("请输入迷宫大小 (n m): \n");cout.flush(); cin >> n >> m; if (n < 2) n = 2; if (m < 2) m = 2;
printf("\n正在生成迷宫..."); shengcheng(); printf("迷宫生成完成!按任意键开始游戏..."); _getch(); yunxing();
printf("\n游戏结束!是否继续?(y/n): "); char c = _getch(); while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'){ c = _getch(); } if (c == 'n' || c == 'N'){ break; } printf("\n"); } return 0;}```下面(如果有的话)为临时快照版本,bug多多,快照版的bug不要私信我
## 快照(烂摊子)