【问题标题】:Pseudocode -> C++, any tips?伪代码-> C++,有什么提示吗?
【发布时间】:2022-01-04 11:08:18
【问题描述】:

所以我得到了这个伪代码,但我不明白它的含义。我们必须制作一个递归回溯随机迷宫生成器。谁能解释一下?

【问题讨论】:

    标签: pseudocode


    【解决方案1】:

    你从迷宫中的某种牢房开始。此外,如果您还没有访问过它,您会立即从它移动到所有相邻的单元格并从头开始。即首先您在一个牢房中,然后例如在三个牢房中。由于实际上递归调用是按顺序执行的,因此旅行者似乎是依次进入这些路径的。

    async function findPath(from, to){ 
      /* 1 */ visited[from.y][from.x]=true;
    
              drawMaze()
              await sleep(150)
    
      /* 2 */ if (from.x === to.x && from.y === to.y) return true;
      /* 3 */ let neighbours = getNeighbours(from.x, from.y);
      /* 4 */ for (let n in neighbours) {
          /* 1 */   if (visited[neighbours[n].y][neighbours[n].x] === 0){
                /* 1 */ if (await findPath(neighbours[n], to)) return true  
                    } 
              }
      /* 5 */ visited[from.y][from.x] = false;
      /* 6 */ return false; 
    }
    
    const maze = [
      [0,1,1,1,1,1,1,1,1,1,1,1],
      [0,0,0,0,0,1,0,1,1,1,1,1],
      [1,1,0,1,0,1,0,1,1,1,1,1],
      [1,1,0,1,0,0,0,0,1,1,1,1],
      [1,1,0,1,1,0,1,1,1,1,1,1],
      [1,1,0,1,1,0,1,1,1,1,0,1],
      [1,1,0,0,1,1,1,1,1,0,0,1],
      [1,1,0,1,1,0,0,0,1,0,1,1],
      [1,0,0,0,1,0,1,1,1,0,0,1],
      [1,0,1,0,0,0,0,0,0,0,1,1],
      [1,0,0,0,1,1,1,1,1,0,1,1],
      [1,1,1,1,1,1,1,1,1,0,0,0]]
    
    const visited = [
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0]]
    
    const c = document.querySelector("canvas");
    const h2 = document.querySelector("h2");
    const ctx = c.getContext("2d");
    const f = {x: 0, y: 0 }  // from (red)
    const t = {x: 11, y: 11} // to (green)
    const CANVAS_SIZE = 120;
    const sz = CANVAS_SIZE / maze.length; 
    
    function drawMaze() {
      for (let y = 0; y < maze.length; y++ ){
        for (let x = 0; x < maze[0].length; x++ ){
              ctx.fillStyle = maze[y][x] === 0 ? "white": "black";  
              ctx.fillRect(x * sz, y * sz, sz, sz);
              if (visited[y][x]){
                ctx.fillStyle = "lightblue"
                ctx.fillRect(x * sz, y * sz, sz, sz);
              } 
          }
       }
       // draw start point
       ctx.fillStyle = "red" 
       ctx.fillRect(f.x * sz, f.y * sz, sz, sz);
       // draw finish point
       ctx.fillStyle = "green"
       ctx.fillRect(t.x * sz, t.y * sz, sz, sz);
    } 
    
    function isValidStep({x, y}){
      if (x < 0 
        || x >= maze[0].length 
        || y < 0 
        || y >= maze.length
      ) return false
      return true  
    }
    
    function getNeighbours(x, y){
      let neighbours = {}
      if (isValidStep({x: x - 1, y}) 
        && maze[y][x - 1] === 0) {
        neighbours.left = { x: x - 1, y}
      }  
      if (isValidStep({x, y: y - 1}) 
        && maze[y - 1][x] === 0) {
        neighbours.top ={x, y: y - 1}
      }  
      if (isValidStep({x: x + 1, y}) 
        && maze[y][x + 1] === 0) {
        neighbours.right = {x: x + 1, y }
      }  
      if (isValidStep({x, y: y + 1}) 
        && maze[y + 1][x] === 0) {
        neighbours.bottom = {x, y: y + 1}
      }  
      return neighbours
    }
    
    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    
    async function test(){
      h2.textContent="?"
      const result = await findPath(f, t) ? "TRUE" : "FALSE"
      h2.textContent=result
    }
    
    test();
    <html>
      <head>
        <title></title>
        <meta content="">
        <style>
           canvas {
             border: 1px solid black;
             height: 120px;
             width:  120px;
           }
        </style>
      </head>
      <body>
        <canvas height="120" width="120"></canvas>
        <h2></h2>
      </body>
    </html>

    你的任务是生成算法并以这种方式检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多