递归除法不能保证路径

问题描述

我从这里Maze (recursive division) algorithm design的答案中实现了算法。

但是我遇到一个问题,就是从start(+)到finish(-)并不总是一条路径 like this image

还有另一个问题,即start(+)或finish(-)节点将被墙see start node (+)包围

但是我认为它很容易修复。

移除该节点[x-1] [y],[x] [y + 1],[x + 1] [y]和[x] [y-1]周围的墙壁。

我不知道它是否正确。

那是我的迷宫课

class Maze {
  private G: INode[][];
  private rows: number;
  private cols: number;

  constructor(G: INode[][]) {
    this.G = G.map((row) =>
      row.map((node) => ({
        ...node,isWall: false,}))
    );
    this.rows = this.G.length;
    this.cols = this.G[0].length;

    // border walls
    for (let i = 0; i < this.rows; i++) {
      this.G[i][0].isWall = true;
      this.G[i][this.cols - 1].isWall = true;
    }
    for (let i = 0; i < this.cols; i++) {
      this.G[0][i].isWall = true;
      this.G[this.rows - 1][i].isWall = true;
    }
  }

  private rand(min: number,max: number) {
    return Math.floor(Math.random() * (max - min + 1) + min);
  }

  private isNodeStartOrFinish(row: number,col: number) {
    return this.G[row][col].isStart || this.G[row][col].isFinish;
  }

  // call perfectMaze to get the generate the maze 
  public perfectMaze() {
    this.divide(true,1,this.cols - 2,this.rows - 2);
  }

  private divide(
    h: boolean,minX: number,maxX: number,minY: number,maxY: number
  ) {
    if (h) {
      if (maxX - minX < 2) return;
      const y = Math.floor(this.rand(minY,maxY) / 2) * 2;
      this.addHWall(minX,maxX,y);
      this.divide(!h,minX,minY,y - 1);
      this.divide(!h,y + 1,maxY);
    } else {
      if (maxY - minY < 2) return;
      const x = Math.floor(this.rand(minX,maxX) / 2) * 2;
      this.addVWall(minY,maxY,x);
      this.divide(!h,x - 1,maxY);
      this.divide(!h,x + 1,maxY);
    }
  }

  private addHWall(minX: number,y: number) {
    const hole = Math.floor(this.rand(minX,maxX) / 2) * 2 + 1;
    for (let i = minX; i <= maxX; i++) {
      if (i !== hole && !this.isNodeStartOrFinish(y,i)) {
        this.G[y][i].isWall = true;
      }
    }
  }

  private addVWall(minY: number,maxY: number,x: number) {
    const hole = Math.floor(this.rand(minY,maxY) / 2) * 2 + 1;
    for (let i = minY; i <= maxY; i++) {
      if (i !== hole && !this.isNodeStartOrFinish(i,x)) {
        this.G[i][x].isWall = true;
      }
    }
  }

  public getMaze() {
    return this.G;
  }
}

解决方法

好吧,我很愚蠢

当我===孔时,我没有移除墙壁

在addHWall和addVWall方法中

如果我===孔,则必须删除墙,否则将再次添加墙

  private addHWall(minX: number,maxX: number,y: number) {
    const hole = Math.floor(this.rand(minX,maxX) / 2) * 2 + 1;
    for (let i = minX; i <= maxX; i++) {
      if (i === hole || this.isNodeStartOrFinish(y,i)) {
        this.G[y][i].isWall = false;
      } else {
        this.G[y][i].isWall = true;
      }
    }
  }

与addVWall相同

if (i === hole || this.isNodeStartOrFinish(i,x)) {
   this.G[y][i].isWall = false;
} else {
   this.G[i][x].isWall = true;
}

我最终删除了起点和终点节点周围的墙,仍然不知道这是否是正确的解决方案

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...