solution.json 4.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "49a32f00b67543ec98399a8e3266b20e",
  "keywords": "数组,动态规划,矩阵",
  "title": "不同路径 II",
  "desc": [
    {
      "content": "\n<p>一个机器人位于一个 <em>m x n </em>网格的左上角 (起始点在下图中标记为“Start” )。</p>\n<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。</p>\n<p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?</p>\n<p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot_maze.png\"\nstyle=\"height: 183px; width: 400px;\" /></p>\n<p>网格中的障碍物和空位置分别用 <code>1</code> 和 <code>0</code> 来表示。</p>\n<p> </p>\n<p><strong>示例 1:</strong></p><img alt=\"\"\nsrc=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot1.jpg\"\nstyle=\"width: 242px; height: 242px;\" />\n<pre><strong>输入:</strong>obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]<strong><br />输出:</strong>2<strong><br />解释:</strong>3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:<br />1. 向右 -> 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右 -> 向右</pre>\n<p><strong>示例 2:</strong></p><img alt=\"\"\nsrc=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot2.jpg\"\nstyle=\"width: 162px; height: 162px;\" />\n<pre><strong>输入:</strong>obstacleGrid = [[0,1],[0,0]]<strong><br />输出:</strong>1</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n<li><code>m == obstacleGrid.length</code></li>\n<li><code>n == obstacleGrid[i].length</code></li>\n<li><code>1 <= m, n <= 100</code></li>\n<li><code>obstacleGrid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>\n</ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\nstatic int uniquePathsWithObstacles(int **obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize)\n{\n\tint row, col;\n\tint reset = 0;\n\tfor (row = 0; row < obstacleGridRowSize; row++)\n\t{\n\t\tif (reset)\n\t\t{\n\t\t\tobstacleGrid[row][0] = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (obstacleGrid[row][0] == 1)\n\t\t\t{\n\t\t\t\treset = 1;\n\t\t\t}\n\t\t}\n\t}\n\treset = 0;\n\tfor (col = 0; col < obstacleGridColSize; col++)\n\t{\n\t\tif (reset)\n\t\t{\n\t\t\tobstacleGrid[0][col] = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (obstacleGrid[0][col] == 1)\n\t\t\t{\n\t\t\t\treset = 1;\n\t\t\t}\n\t\t}\n\t}\n\tfor (row = 0; row < obstacleGridRowSize; row++)\n\t{\n\t\tint *line = obstacleGrid[row];\n\t\tfor (col = 0; col < obstacleGridColSize; col++)\n\t\t{\n\t\t\tline[col] ^= 1;\n\t\t}\n\t}\n\tfor (row = 1; row < obstacleGridRowSize; row++)\n\t{\n\t\tint *last_line = obstacleGrid[row - 1];\n\t\tint *line = obstacleGrid[row];\n\t\tfor (col = 1; col < obstacleGridColSize; col++)\n\t\t{\n\t\t\tif (line[col] != 0)\n\t\t\t{\n\t\t\t\tline[col] = line[col - 1] + last_line[col];\n\t\t\t}\n\t\t}\n\t}\n\treturn obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1];\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test m n\\n\");\n\t\texit(-1);\n\t}\n\tint i, j, k = 3;\n\tint row_size = atoi(argv[1]);\n\tint col_size = atoi(argv[2]);\n\tint **grids = malloc(row_size * sizeof(int *));\n\tfor (i = 0; i < row_size; i++)\n\t{\n\t\tgrids[i] = malloc(col_size * sizeof(int));\n\t\tint *line = grids[i];\n\t\tfor (j = 0; j < col_size; j++)\n\t\t{\n\t\t\tline[j] = atoi(argv[k++]);\n\t\t\tprintf(\"%d \", line[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tprintf(\"%d\\n\", uniquePathsWithObstacles(grids, row_size, col_size));\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-4636efcd81dd4e2a9f8f83ab23b80ffe",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470123"
}