最短路径遍历所有的节点 Shortest Path Visiting All Nodes
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的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 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 ...
- leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...
- LeetCode 847. Shortest Path Visiting All Nodes
题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...
- [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...
- 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- ZOJ 2760 How Many Shortest Path(最短路径+最大流)
Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...
随机推荐
- c++中的header-only library
不同于在java中,虽然在java中,有些第三方库只是做了桥接的功能,比如slf4j-log4j-api,但是在运行时他们仍然是需要的,所以最多只能说是松耦合做得很好. 但是在c++中,一般我们应用第 ...
- indexOf()/equals/contains
indexOf():对大小写敏感定义:返回某个指定字符串值在字符串中首次出现位置用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判断 ...
- OO课程第四次总结
终于来到了最后一次的OO作业,以博客作业的形式来终结也是极好的,回顾一下过去十六周自己的经历,感慨颇深. 测试和正确性论证 简单来说,测试的目的是将程序的代码做到全覆盖,从而确保每个分支都运行一遍,进 ...
- bzoj 1818 [CQOI 2010] 内部白点 - 扫描线 - 树状数组
题目传送门 快速的列车 慢速的列车 题目大意 一个无限大的方格图内有$n$个黑点.问有多少个位置上下左右至少有一个黑点或本来是黑点. 扫描线是显然的. 考虑一下横着的线段,取它两个端点,横坐标小的地方 ...
- 配置vim
VundleVuldle是一个全自动的插件管理器,让我们通过维护插件列表的方式管理插件.它为安装.更新.删除插件提供了方便的命令.在安装Git的情况下(本文不赘述Git的安装),输入命令: git c ...
- (转)Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspectives
Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspecti ...
- 论文笔记:Semantic Segmentation using Adversarial Networks
Semantic Segmentation using Adversarial Networks 2018-04-27 09:36:48 Abstract: 对于产生式图像建模来说,对抗训练已经取得了 ...
- go 依赖工具glide
添加gopath/bin目录到环境变量下 安装glide $ go get github.com/Masterminds/glide $ go install github.com/Mastermin ...
- 【Net Core】DNX概述
1. 什么是.NET执行环境 ? .NET Execution Environment(DNX) 是一个SDK 和运行时环境,它包含所有的你需要创建和运行.net应用程序的组件.它提供一个主机进程,C ...
- EPPlus实战篇——Excel写入
.net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...