{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "3eb2527a26be4b9e8b24ade9a1e4b9f6",
  "keywords": "图算法,算法高阶,图的表示,基本的图算法",
  "title": "迷宫问题，需要用递归",
  "desc": [
    {
      "content": "\n<p>问题描述&#xff1a;一只老鼠在一个n×n迷宫的入口处&#xff0c;它想要吃迷宫出口处放着奶酪&#xff0c;问这只老鼠能否吃到奶酪&#xff1f;如果可以吃到&#xff0c;请给出一条从入口到奶酪的路径。\n思考&#xff1a;解决问题之前&#xff0c;我们首先要做的就是仔细研究问题&#xff0c;找出问题的已知条件和要得到的是什么。和解数学问题、物理问题一样要先弄懂问题。那么&#xff0c;老鼠走迷宫问题的已知条件有什么呢&#xff1f;\n数学模型重新定义问题&#xff1a;\n问题&#xff1a;问老鼠能否吃到奶酪就是问能否找到一条从迷宫入口到出口的路径。如果不能找到&#xff0c;那么老鼠就吃不到奶酪&#xff1b;如果能够找到&#xff0c;那么就给出这条路径。\n观察10×10的迷宫。这个迷宫其实是由10×10&#61;100个格子组成的&#xff0c;其中绿色格子代表墙&#xff0c;白色格子代表路&#xff0c;如图&#xff08;1&#xff09;所示。“绿色格子代表墙&#xff0c;白色格子代表路”是用语言形式描述的&#xff0c;需要转换成数学的形式。用1和0分别定义绿色格子和白色格子&#xff0c;可以得到如图&#xff08;2&#xff09;的迷宫。\n将上面10×10的迷宫定义为如下的二维数组&#xff0c;即\nm[10][10]&#61;[1,1,1,0,1,1,1,1,1,1,\n1,0,0,0,0,0,0,0,1,1,\n1,0,1,1,1,1,1,0,0,1,\n1,0,1,0,0,0,0,1,0,1,\n1,0,1,0,1,1,0,0,0,1,\n1,0,0,1,1,0,1,0,1,1,\n1,1,1,1,0,0,0,0,1,1,\n1,0,0,0,0,1,1,1,0,0,\n1,0,1,1,0,0,0,0,0,1,\n1,1,1,1,1,1,1,1,1,1]\n有了对迷宫的数学定义&#xff0c;就可以很简单的定义迷宫的入口和出口了。迷宫的入口是m[0][3]&#xff0c;出口是m[7][9]。老鼠走迷宫问题就是要找一条从入口到出口的路径&#xff0c;如果存在就返回这条路径&#xff1b;如果不存在&#xff0c;就返回不存在这种路径。也就是说&#xff0c;要在二维数组m中找一条从m[0][3]到m[7][9]全部为0的路径。\n请使用递归解决迷宫路径查找问题。\n</p>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "def maze(m, n, route, pos, export):\n\t\"\"\"走迷宫\n\tm\t   - 迷宫数组，列表\n\tn\t   - 迷宫阶数\n\troute   - 可能的路线，列表\n\tpos\t - 当前位置，元组\n\texport  - 出口位置，元组\n\t\"\"\"\n\troute.append(pos)\n\tif pos == export:\n\t\tprint(route)\n\tif pos[0] > 0 and m[pos[0]-1][pos[1]] == 0 and (pos[0]-1,pos[1]) not in route:\n\t\tmaze(m, n, route[:], (pos[0]-1,pos[1]), export)\n\tif pos[0] < n-1 and m[pos[0]+1][pos[1]] == 0 and (pos[0]+1,pos[1]) not in route:\n\t\tmaze(m, n, route[:], (pos[0]+1,pos[1]), export)\n\tif pos[1] > 0 and m[pos[0]][pos[1]-1] == 0 and (pos[0],pos[1]-1) not in route:\n\t\tmaze(m, n, route[:], (pos[0],pos[1]-1), export)\n\tif pos[1] < n-1 and m[pos[0]][pos[1]+1] == 0 and (pos[0],pos[1]+1) not in route:\n\t\tmaze(m, n, route[:], (pos[0],pos[1]+1), export)\nm = [\n\t[1,1,1,0,1,1,1,1,1,1], \n\t[1,0,0,0,0,0,0,0,1,1], \n\t[1,0,1,1,1,1,1,0,0,1], \n\t[1,0,1,0,0,0,0,1,0,1], \n\t[1,0,1,0,1,1,0,0,0,1], \n\t[1,0,0,1,1,0,1,0,1,1], \n\t[1,1,1,1,0,0,0,0,1,1], \n\t[1,0,0,0,0,1,1,1,0,0], \n\t[1,0,1,1,0,0,0,0,0,1], \n\t[1,1,1,1,1,1,1,1,1,1] \n]\nmaze(m, len(m), list(), (0,3), (7,9))",
    "language": "python"
  },
  "node_id": "dailycode-140f8cff6c77471a9b56168e6219d7e3",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470402"
}