algoalgo-world
algoalgo-world/maze/aldous-broder
maze/aldous-broder

奥尔德斯-布罗德

选一种语言就能看到代码

def aldous_broder_maze(w, h, rng):
    links = [0] * (w * h)
    seen = [False] * (w * h)
    room = rng.below(w * h)
    seen[room] = True
    left = w * h - 1
    while left > 0:
        d = rng.below(4)
        other = neighbor(room, d)
        if other < 0:
            continue
        if not seen[other]:
            carve(links, room, other, d)
            seen[other] = True
            left -= 1
        room = other
    return links
function aldousBroderMaze(w, h, rng) {
  const links = new Array(w * h).fill(0);
  const seen = new Array(w * h).fill(false);
  let room = rng.below(w * h);
  let left = w * h - 1;
  seen[room] = true;
  while (left > 0) {
    const d = rng.below(4);
    const other = neighbor(room, d);
    if (other < 0) continue;
    if (!seen[other]) {
      carve(links, room, other, d);
      seen[other] = true;
      left--;
    }
    room = other;
  }
  return links;
}
void aldous_broder_maze(int links[], int w, int h) {
    int n = w * h;
    char* seen = calloc(n, 1);
    int room = rand() % n;
    int left = n - 1;
    seen[room] = 1;
    while (left > 0) {
        int d = rand() % 4;
        int other = neighbor(room, d);
        if (other < 0) continue;
        if (!seen[other]) {
            carve(links, room, other, d);
            seen[other] = 1;
            left--;
        }
        room = other;
    }
    free(seen);
}
void aldous_broder_maze(std::vector<int>& links, int w, int h,
                        std::mt19937& rng) {
    int n = w * h;
    std::vector<char> seen(n, 0);
    std::uniform_int_distribution<int> any_room(0, n - 1);
    std::uniform_int_distribution<int> any_dir(0, 3);
    int room = any_room(rng);
    int left = n - 1;
    seen[room] = 1;
    while (left > 0) {
        int d = any_dir(rng);
        int other = neighbor(room, d);
        if (other < 0) continue;
        if (!seen[other]) {
            carve(links, room, other, d);
            seen[other] = 1;
            left--;
        }
        room = other;
    }
}
static int[] AldousBroderMaze(int w, int h, Random rng) {
    int n = w * h;
    int[] links = new int[n];
    bool[] seen = new bool[n];
    int room = rng.Next(n);
    int left = n - 1;
    seen[room] = true;
    while (left > 0) {
        int dir = rng.Next(4);
        int next = Neighbor(room, dir);
        if (next < 0) continue;
        if (!seen[next]) {
            Carve(links, room, next, dir);
            seen[next] = true;
            left--;
        }
        room = next;
    }
    return links;
}
static int[] aldousBroderMaze(int w, int h, Random rng) {
    int n = w * h;
    int[] links = new int[n];
    boolean[] seen = new boolean[n];
    int room = rng.nextInt(n);
    int left = n - 1;
    seen[room] = true;
    while (left > 0) {
        int dir = rng.nextInt(4);
        int next = neighbor(room, dir);
        if (next < 0) continue;
        if (!seen[next]) {
            carve(links, room, next, dir);
            seen[next] = true;
            left--;
        }
        room = next;
    }
    return links;
}
先跑一遍,再读源码