【问题标题】:Recursive division does not guarantee a path递归除法不保证路径
【发布时间】:2020-09-23 10:43:24
【问题描述】:

我从这里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, 1, 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, maxX, minY, y - 1);
      this.divide(!h, minX, maxX, 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, minX, x - 1, minY, maxY);
      this.divide(!h, x + 1, maxX, minY, maxY);
    }
  }

  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 = 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;
  }
}

【问题讨论】:

    标签: algorithm recursion graph-algorithm maze


    【解决方案1】:

    好吧,我是笨蛋

    i === 洞时我没有拆墙

    在 addHWall 和 addVWall 方法中

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

      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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 2014-03-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多