On a 2-dimensional grid, there are 4 types of squares:

  • 1 represents the starting square.  There is exactly one starting square.
  • 2 represents the ending square.  There is exactly one ending square.
  • 0 represents empty squares we can walk over.
  • -1 represents obstacles that we cannot walk over.

Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.

Example 1:

Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)

Example 2:

Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)

Example 3:

Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.

Note:

  1. 1 <= grid.length * grid[0].length <= 20

思路:深搜, 终止条件到达目标位置,以及可到达的位置全部走了一遍,算一条路径。

 class Solution {
int dx[] = {, -, , };
int dy[] = {, , -, };
public:
int uniquePathsIII(vector<vector<int>>& grid) {
int m = grid.size();
if (m == )
return ;
int n = grid[].size();
int todo = ;
int start_x, start_y, end_x, end_y;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (grid[i][j] != -) { //记录要走的总的位置数
todo++;
if (grid[i][j] == ) { //记录起始位置
start_x = i;
start_y = j;
} else if (grid[i][j] == ) { //记录终点
end_x = i;
end_y = j;
}
}
}
}
int ans = ;
dfs(grid, start_x, start_y, end_x, end_y, todo, ans, m, n);
return ans;
}
void dfs(vector<vector<int> > &grid, int sx, int sy, const int ex, const int ey, int todo, int &ans, int row, int col) {
todo--;
if (todo < )
return ;
if (sx == ex && sy == ey) {
if (todo == ) ans++;
return;
}
//上下左右四个方向
for (int k = ; k < ; k++) {
int new_x = sx + dx[k];
int new_y = sy + dy[k];
if (new_x >= && new_x < row && new_y >= && new_y < col) {
if (grid[new_x][new_y] == || grid[new_x][new_y] == ) {
grid[new_x][new_y] = -;
dfs(grid, new_x, new_y, ex, ey, todo, ans, row, col);
grid[new_x][new_y] = ;
}
}
}
}
};

leetcode 980. Unique Paths III的更多相关文章

  1. LC 980. Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...

  2. 原题链接在这里:980. Unique Paths III

    原题链接在这里:https://leetcode.com/problems/unique-paths-iii/ 题目: On a 2-dimensional grid, there are 4 typ ...

  3. 【LeetCode】980. Unique Paths III解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  4. 【leetcode】980. Unique Paths III

    题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  Ther ...

  5. 980. Unique Paths III

    题目来源: https://leetcode.com/problems/unique-paths-iii/ 自我感觉难度/真实难度: 题意: 分析: 回溯法,直接DFS就可以了 自己的代码: clas ...

  6. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...

  7. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  9. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

随机推荐

  1. JavaWeb_利用Servlet处理注册登录请求

    利用Servlet处理注册登录请求 程序结构 <%@page import="com.Gary.model.User"%> <%@ page language=& ...

  2. Confluence备份,数据迁移

    一.Confluence的备份.恢复1)Confluence的备份 管理员账号登录Confluence,点击右上角的"一般配置"-"每日备份管理",如下图(默认 ...

  3. 分布式-信息方式-JMS Topic示例

                                                      Topic消息 非持久的 Topic消息示例对于非持久的 Topic消息的发送       基本跟前 ...

  4. Linux高级调试与优化——同时抓取coredump和maps文件

    Linux内核源码 Documentation/sysctl/kernel.txt core_pattern: core_pattern: core_pattern is used to specif ...

  5. 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_06.入门案例的流程总结

    配置了load-on-startup等于1 表示启动了服务器就会去创建DispatcherServlet 如果不配置load-on-startup为1 那么第一次发送请求才会去创建Dispatcher ...

  6. 三十八:数据库之ORM层面删除数据的注意事项

    准备工作 from sqlalchemy import create_engine, Column, Integer, String, Float, Text, ForeignKeyfrom sqla ...

  7. selenium之css selector定位

    什么是CSS Selector? Css Selector定位实际就是HTML的Css选择器的标签定位 工具 Css Selector的练习建议大家安装火狐浏览器(49及以下版本)后,下载插件Fire ...

  8. Mybatis面试题合集及答案

    Mybatis面试题合集及答案 1.#{}和${}的区别是什么? 答:${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替换,比如${driver}会被静态 ...

  9. word2vec高效训练方法

    在word2vec原理中讲到如果每个词向量由300个元素组成,并且一个单词表中包含了10000个单词.回想神经网络中有两个权重矩阵——一个在隐藏层,一个在输出层.这两层都具有300 x 10000 = ...

  10. python3 selenuim PC端使用chrome模拟手机进行H5自动化

    情况说明:初次在做PC端使用chrome进行H5自动化测试时,以为和app端自动化一样使用click()就可以对按钮进行点击,找了好几天也没有找到解决方法,有些人说是工程问题,有些人是使用微信进行H5 ...