2018-10-06 22:04:38

问题描述:

问题求解:

对于边没有权重的最短路径的求解,首选的方案是bfs。

本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢?可以使用(curNode, VisitedNode)来进行表征,如果两次的已经访问的节点相同那么就没有必要再进行访问了,最终的状态就是所有节点都访问过了。

另外,由于起点对结果是有影响的,因此在最开始需要将所有的节点都压栈。

    public int shortestPathLength(int[][] graph) {
int n = graph.length;
int target = (1 << n) - 1;
Queue<Integer> q = new LinkedList<>();
Set<Integer> seen = new HashSet<>();
for (int i = 0; i < n; i++) {
q.add(i << 16 | 1 << i);
seen.add(i << 16 | 1 << i);
}
int step = 0;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
int cur = q.poll();
int node = cur >> 16;
int state = cur & 0xffff;
if (state == target) return step;
for (int next : graph[node]) {
int newstate = state | 1 << next;
if (seen.contains(next << 16 | newstate)) continue;
q.add(next << 16 | newstate);
seen.add(next << 16 | newstate);
}
}
step += 1;
}
return -1;
}  

扩展

  • 864. Shortest Path to Get All Keys

问题描述

问题求解

给一个BFS模版。

    public int shortestPathAllKeys(String[] grid) {
int m = grid.length;
int n = grid[0].length();
Queue<Integer> q = new LinkedList<>();
HashSet<Integer> seen = new HashSet<>();
int target = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
char c = grid[i].charAt(j);
if (c == '@') {
q.add(i << 16 | j << 8);
seen.add(i << 16 | j << 8);
}
if (c >= 'a' && c <= 'f') target |= 1 << (c - 'a');
}
}
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int step = 0;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
int cur = q.poll();
int x = cur >> 16;
int y = cur >> 8 & 0xFF;
int key = cur & 0xFF;
if (key == target) return step;
for (int[] dir : dirs) {
int nx = x + dir[0];
int ny = y + dir[1];
int nkey = key;
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
char c = grid[nx].charAt(ny);
if (c == '#') continue;
if (c >= 'A' && c <= 'F' && ((key & (1 << (c - 'A'))) == 0)) continue;
if (c >= 'a' && c <= 'f') nkey = key | (1 << (c - 'a'));
int newstate = nx << 16 | ny << 8 | nkey;
if (seen.contains(newstate)) continue;
q.add(newstate);
seen.add(newstate);
}
}
step += 1;
}
return -1;
}

  

最短路径遍历所有的节点 Shortest Path Visiting All Nodes的更多相关文章

  1. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  2. [LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  3. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

  4. 847. Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  5. leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径

    设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...

  6. LeetCode 847. Shortest Path Visiting All Nodes

    题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...

  7. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  8. 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  9. ZOJ 2760 How Many Shortest Path(最短路径+最大流)

    Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...

随机推荐

  1. Zookeeper注册中心的搭建

    一.Zookeeper的介绍 Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用 ...

  2. 牛客网数据库SQL实战(1-5)

    1.查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, `f ...

  3. git将本地内容传送到远程仓库出现![rejected] master -> master (fetch first)错误

    问题:使用git push -u 远程库名 master 命令将本地提交的内容传到git远程库时出现错误: 命令: git push -u origin master 出现错误: To https:/ ...

  4. 在Ubuntu上更新Chrome到最新的版本

    本操作只限于已经安装了Chrome的Ubuntu系统: Step 1: sudo apt-get update Step 2: sudo apt-get install google-chrome-s ...

  5. Web操作web.config

    1.引用System.Configuration.DLL 2.引用命名空间System.Configuration和System.Web.Configuration 3.上代码 // 使用指定的虚拟路 ...

  6. e信与酸酸结合开wifi使用路由器上网

    关于e信"正常情况下"使用路由器网上是有方法的,入户线插上lan,电脑接lan拨号 我想要说的是连接e信后使用路由器上网,并且是绝对正常的思维 手机也是可以连接上wifi,但是手机 ...

  7. IDEA旗舰版新建web项目

    即在一个Project下(MyEclipse中为工作空间)新建一个Module. 点击,在弹出框上打一个勾,如下图: 点Next,输入项目名,如下图: 点Finish, 右键WEB-INF,新建2个D ...

  8. (zhuan) Evolution Strategies as a Scalable Alternative to Reinforcement Learning

    Evolution Strategies as a Scalable Alternative to Reinforcement Learning this blog from: https://blo ...

  9. UI之ECharts

    官网 效果图展示: 特性 ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Fir ...

  10. Kubernetes相关概念

    This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can exp ...