正文
增加 295 行删除 983 行
1234567891011121314151617181920212223242526272 行未改动30313233343536373839405 行未改动464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431有bug或建议私信我## Windows```cpp//https://www.luogu.com.cn/article/qqm2loyo//https://www.luogu.com.cn/user/92860/article#include <bits/stdc++.h>#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)return;CONSOLE_SCREEN_BUFFER_INFO 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}; //这行指令就是反的SetConsoleCursorPosition(hConsole, coordScreen);}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])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){ //upif (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){ //downif (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){ //leftif (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){ //rightif (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// cls();// 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;}elsenxt.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){color(10);printf("[]");color(7);}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'){printf(" ");}else{color(8);printf("##");color(7);}// 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){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]){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'){printf(" ");}else{color(8);printf("##");color(7);}keep_g1[i][j] = 1;g1[i][j] = 1;}else{g1[i][j] = 0;printf("..");}}// else cout<<mp[i][j]<<" ";printf("\n");}};}int min_y[2005], max_y[2005];void yunxing(){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);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){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){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){shiyex = min(shiyex, i);max_x = max(max_x, i);}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]){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]){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){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])){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){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]){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]){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]){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){color(10);printf("[]");color(7);}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'){printf(" ");}else{color(8);printf("##");color(7);}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){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){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]){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'){printf(" ");}else{color(8);printf("##");color(7);}keep_g1[i][j] = 1;g1[i][j] = 1;}else{g1[i][j] = 0;printf("..");}}// else cout<<mp[i][j]<<" ";// cout<<i<<" "<<min_y[i]<<" "<<max_y[i]<<endl;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]");elseprintf(" [视野模式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不确定按任意健)");c = _getch();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){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;}}elsewhile (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;}cls();break;}}if (flag)break;if (x == 2 * n - 1 && y == 2 * m - 1){cls();printf("╔════════════════════════════════╗\n");printf("║ 恭喜!到达终点! ║\n");printf("╠════════════════════════════════╣\n");printf("║ 总共走了 %5d 步 ║\n", sum);printf("╚════════════════════════════════╝\n");printf("按任意键继续...");_getch();break;}}cursorInfo.bVisible = TRUE;SetConsoleCursorInfo(hConsole, &cursorInfo);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){color(9);if (lu[i][j] < 10)printf("%d ", lu[i][j]);else if (lu[i][j] < 100)printf("%d", lu[i][j]);elseprintf("99");color(7);}else if (show_foot == FOOTPRINT){color(9);printf("--");color(7);}else{printf(" ");}}else if (keep_g1[i][j] || show_all){if (mp[i][j] == 'o'){printf(" ");}else{color(8);printf("##");color(7);}}else{printf("..");}}printf("\n");}printf("你走了%d步\n", sum);printf("按q或b退出,按a或n有不同迷宫视角\n");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')){c = _getch();if (c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b'){flag = 1;break;}else if (c == 'n' || c == 'N'){show_foot++;show_foot %= 3;break;}else if (c == 'a' || c == 'A'){show_all = !show_all;break;}}if (flag)break;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的迷宫游戏");color(7);while (1){srand(time(NULL));// 显示特殊模式设置菜单bool setting = true;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 ? "开启" : "关闭");printf("───────────────────────────────────\n");printf(" 按数字键切换对应模式,按 [Y] 开始输入边数列数\n");printf("═══════════════════════════════════\n");printf("请选择: ");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请输入迷宫大小 (n m): ");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;}```## 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){ //upif (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){ //downif (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){ //leftif (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){ //rightif (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;}elsenxt.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("\033[1;33m");printf("##");printf("\033[0m");}// 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("\033[1;33m");printf("##");printf("\033[0m");}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("\033[1;33m");printf("##");printf("\033[0m");}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("\033[1;33m");printf("##");printf("\033[0m");}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]");elseprintf(" [视野模式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]);}}c = _getch();if (c == 'b'){printf("确定结束吗(确定按y不确定按任意健)");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;}}elsewhile (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;32m");if (lu[i][j] < 10)printf("%d ", lu[i][j]);else if (lu[i][j] < 100)printf("%d", lu[i][j]);elseprintf("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("\033[1;33m");printf("##");printf("\033[0m");}}else{printf("..");}}printf("\n");}printf("你走了%d步", sum);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(){while (1){srand(time(NULL));printf("请输入迷宫大小 (n m): ");string input_n, input_m;cin >> input_n >> input_m;while ((input_n == "123" && input_m == "321") ||(input_n == "321" && input_m == "123") ||(input_n == "135" && input_m == "531") ||(input_n == "531" && input_m == "135") ||(input_n == "111" && input_m == "111") ||(input_n == "114" && input_m == "514")){// 处理特殊指令if (input_n == "123" && input_m == "321"){gua1 = 1;printf("【特殊模式激活】视野遗留模式 ON\n");}else if (input_n == "321" && input_m == "123"){gua1 = 0;printf("【特殊模式激活】视野遗留模式 OFF\n");}else if (input_n == "135" && input_m == "531"){gua2 = 1;printf("【特殊模式激活】脚步留痕模式 ON\n");}else if (input_n == "531" && input_m == "135"){gua2 = 0;printf("【特殊模式激活】脚步留痕模式 OFF");}else if (input_n == "111" && input_m == "111"){shiyexingshi = shiyexingshi ^ 1;if (shiyeyunxing){printf("【特殊模式激活】视野模式切换为: 模式1(精确视野)\n");}else{printf("【特殊模式激活】视野模式切换为: 模式2(距离视野)\n");}}else if (input_n == "114" && input_m == "514"){zidong = zidong ^ 1;printf("【特殊模式激活】人机接管:%d\n", int(zidong));}cout.flush();printf("继续输入迷宫大小 (n m): ");cin >> input_n >> input_m;}n = stoi(input_n);m = stoi(input_m);if (n < 2)n = 2;if (m < 2)m = 2;// if (n > 50) n = 50;// if (m > 50) m = 50;printf("\n当前游戏设置:\n");printf("地图大小: %d*%d\n", n, m);printf("正在生成迷宫");shengcheng();printf("迷宫生成完成!按任意键开始游戏...");_getch();yunxing();printf("\n游戏结束!是否继续?(y/n): ");cout.flush();char c = _getch();while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'){c = _getch();}if (c == 'n' || c == 'N'){break;}printf("\n");}return 0;}```更新日志:$2026.4.2.12:00$ 经历不知道多少调试,正式入驻洛谷。$2026.4.2.16:40$ 修复了无法显示颜色的问题。$2026.4.2.20:00$ 修复了生成地图错误并优化了bfs函数的寻路逻辑(试用优先队列)。$2026.4.2.22:30$ 加速了生成(输出)的速度(真的很快!)(只更新要更新的几行)$2026.4.3.7:30$ 修复了数组未清空等问题$2026.4.3.11:00$ 优化了人机对畸形图的应对能力$2026.4.3.15:00$ 修复了数组未清空的问题,新增在人机自动寻路时退出的功能$2026.4.12.13:30$ 新建Linux版本,但有时会很卡,bug可能会有,不知道怎么调,知道的告诉我$2026.4.14.21:30$ 对Windows进行了优化:加快输出,添加开始界面。补充:请确保您的终端能完整显示所有内容,否则一定会出现bug下面(如果有的话)为临时快照版本,bug多多,快照版的bug不要私信我1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253542 行未改动575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431445 行未改动150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743有bug或建议私信我## 更新日志&&bug:$2026.4.2.12:00$ 经历不知道多少调试,正式入驻洛谷。$2026.4.2.16:40$ 修复了无法显示颜色的问题。$2026.4.2.20:00$ 修复了生成地图错误并优化了bfs函数的寻路逻辑(试用优先队列)。$2026.4.2.22:30$ 加速了生成(输出)的速度(真的很快!)(只更新要更新的几行)$2026.4.3.7:30$ 修复了数组未清空等问题$2026.4.3.11:00$ 优化了人机对畸形图的应对能力$2026.4.3.15:00$ 修复了数组未清空的问题,新增在人机自动寻路时退出的功能$2026.4.12.13:30$ 新建Linux版本,但有时会很卡,bug可能会有,不知道怎么调,知道的告诉我$2026.4.14.21:30$ 对Windows进行了优化:加快输出,添加开始界面。$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## 融合版```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>#define color(a) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a)void cls(){HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);if(hStdOut == INVALID_HANDLE_VALUE)return;CONSOLE_SCREEN_BUFFER_INFO 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}; //这行指令就是反的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])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;}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){ //upif(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){ //downif(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){ //leftif(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){ //rightif(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;}}}int 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 pre[4005][4005][2];int lst[2005][2005][2]; //指此节点第一次被走是哪个节点走的(方向)bool bfsb[2005][2005];priority_queue<Node> q;int bfs(int x, int y){ //return way// cls();// 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};for(int i = 1; i <= 4; ++i){int tx = now.x + xy[i][0], ty = now.y + xy[i][1];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;}elsenxt.way = now.way;nxt.dis = now.dis + 1;nxt.vis =(lu[tx][ty]) ? 2 : 1;if(lu[tx][ty])++nxt.dis;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);}}return 0;}int last_shiye[4005][4005];void out_map(int x, int y){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]){color(9);printf("--");color(7);}else if(shiye[i][j] ||(gua1 && g1[i][j])){if(mp[i][j] == 'o'){printf(" ");}else{color(8);printf("##");color(7);}keep_g1[i][j] = 1;g1[i][j] = 1;}else{g1[i][j] = 0;printf("..");}}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]){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'){printf(" ");}else{color(8);printf("##");color(7);}keep_g1[i][j] = 1;g1[i][j] = 1;}else{g1[i][j] = 0;printf("..");}}}printf("\n");}cout.flush();}int min_y[2005], max_y[2005];void yunxing(){#ifdef _WIN32HANDLE 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);#endifint x = 1, y = 1, sum = 0;lu[1][1] = 1;cls();mp[0][0] = '#';g1[1][1] = 1;out_map(x, y);while(1){int shiyex = x - 1, max_x = x + 1;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);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]){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]){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]){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){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])){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{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]){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]){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]){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];}}}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]){color(9);printf("--");color(7);}else if(shiye[i][j] ||(gua1 && g1[i][j])){if(mp[i][j] == 'o'){printf(" ");}else{color(8);printf("##");color(7);}keep_g1[i][j] = 1;g1[i][j] = 1;}else{g1[i][j] = 0;printf("..");}}printf("\n");}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]){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'){printf(" ");}else{color(8);printf("##");color(7);}keep_g1[i][j] = 1;g1[i][j] = 1;}else{g1[i][j] = 0;printf("..");}}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(" [视野遗留模式]");if(gua2)printf(" [脚步留痕模式]");if(shiyexingshi)printf(" [视野模式1]");elseprintf(" [视野模式2]");printf("\n");printf("使用 WASD 移动,B 退出\n");cout.flush();char c;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() || zidong == 1){c = _getch();if(c == 'b'){printf("确定结束吗(确定按y不确定按任意健)"); cout.flush();c = _getch();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){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不确定按任意健)"); cout.flush();c = _getch();if(c == 'y'){flag = 1;break;}cls();break;}}if(flag)break;if(x == 2 * n - 1 && y == 2 * m - 1){cls();printf("╔════════════════════════════════╗\n");printf("║ 恭喜!到达终点! ║\n");printf("╠════════════════════════════════╣\n");printf("║ 总共走了 %5d 步 ║\n", sum);printf("╚════════════════════════════════╝\n");printf("按任意键继续..."); cout.flush();_getch();break;}}#ifdef _WIN32cursorInfo.bVisible = TRUE;SetConsoleCursorInfo(hConsole, &cursorInfo);#endifconst 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){color(9);if(lu[i][j] < 10)printf("%d ", lu[i][j]);else if(lu[i][j] < 100)printf("%d", lu[i][j]);elseprintf("99");color(7);}else if(show_foot == FOOTPRINT){color(9);printf("--");color(7);}else{printf(" ");}}else if(keep_g1[i][j] || show_all){if(mp[i][j] == 'o'){printf(" ");}else{color(8);printf("##");color(7);}}else{printf("..");}}printf("\n");}printf("你走了%d步\n", sum);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')){c = _getch();if(c == ESC || c == 'q' || c == 'Q' || c == 'B' || c == 'b'){flag = 1;break;}else if(c == 'n' || c == 'N'){show_foot++;show_foot %= 3;break;}else if(c == 'a' || c == 'A'){show_all = !show_all;break;}}if(flag)break;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的迷宫游戏");color(7);while(1){srand(time(NULL));// 显示特殊模式设置菜单bool setting = true;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) ? "半自动" : "全自动(很快)"));printf("───────────────────────────────────\n");printf(" 按数字键切换对应模式,按 [Y] 开始输入边数列数\n");printf("═══════════════════════════════════\n");printf("请选择: "); 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 %= 3;}else if(opt == 'y' || opt == 'Y'){setting = false; // 开始游戏}// 忽略其他按键}printf("\n请输入迷宫大小(n m): ");cout.flush();cin >> n >> m;if(n < 2)n = 2;if(m < 2)m = 2;printf("\n正在生成迷宫...");cout.flush();shengcheng();printf("迷宫生成完成!按任意键开始游戏...");cout.flush();_getch();yunxing();printf("\n游戏结束!是否继续?(y/n): ");cout.flush();char c = _getch();while(c != 'y' && c != 'Y' && c != 'n' && c != 'N'){c = _getch();}if(c == 'n' || c == 'N'){break;}printf("\n");}printf("欢迎下次再来! made by derekyang");cout.flush();return 0;}```下面(如果有的话)为临时快照版本,bug多多,快照版的bug不要私信我## 快照(烂摊子)