1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| vector<vector<int>> *map = nullptr; vector<vector<bool>> *visit = nullptr; const int LocX[4] = { 0, 1, 0, -1 }; const int LocY[4] = { 1, 0, -1, 0 }; enum Direction { UP, RIGHT, DOWN, LEFT }; struct Point { int x, y; }; void RamdomMap(int x, int y) { system("cls"); if (map != nullptr) delete map; map = new vector<vector<int>>(y*2 + 2, vector<int>(x*2 + 2, '0')); vector<vector<int>> &map = *::map; UnionSet us; int maxm = x*y; while (us.Find(1)!=us.Find(maxm)) { int rx = (rand() % x)+1; int ry = (rand() % y)+1; rx = rx * 2 - 1; ry = ry * 2 - 1; int loc = rand() % 4; if ((ry + LocY[loc]<1 || ry + LocY[loc] > y*2-1) || (rx + LocX[loc]<1 || rx + LocX[loc] > x*2-1)) continue; rx += LocX[loc]; ry += LocY[loc]; if (ry % 2 == 0) { int tx1 = rx / 2 + 1, tx2 = tx1; int ty1 = ry / 2, ty2 = ry / 2 + 1; if (us.Find((ty1 - 1)*x + tx1) == us.Find((ty2 - 1)*x + tx2)) continue; map[ry][rx] = '1'; map[ry - 1][rx] = '1'; map[ry + 1][rx] = '1';
us.Merge((ty1 - 1)*x + tx1, (ty2 - 1)*x + tx2); } else { int ty1 = (ry + 1) / 2, ty2 = ty1; int tx1 = rx / 2, tx2 = rx / 2 + 1; if (us.Find((ty1 - 1)*x + tx1) == us.Find((ty2 - 1)*x + tx2)) continue; map[ry][rx] = '1'; map[ry][rx - 1] = '1'; map[ry][rx + 1] = '1';
us.Merge((ty1 - 1)*x + tx1, (ty2 - 1)*x + tx2); } COORD pos = { 0, 0 }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); for (int j = 1; j < y*2 ; ++j) { for (int i = 1; i < x*2; ++i) { printf("%c", map[j][i]); } printf("n"); }
} FILE *fp = fopen("maze.map", "w"); fprintf(fp,"%d %dn", x * 2 - 1, y * 2 - 1); for (int j = 1; j < y*2; ++j) { for (int i = 1; i < x*2; ++i) { fprintf(fp,"%c ", map[j][i]); } fprintf(fp, "n"); } fclose(fp); delete ::map; ::map = nullptr; }
|