最短路径遍历所有的节点 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 ...
随机推荐
- 记一次gitlab添加用户收不到邮件的解决办法
之前再gitlab服务器上创建账号可以正常收到邮件,最近就收不到,查了gitlab的配置以及postfix服务都没有问题,后来查看了发信25端口,发现该25端口并没有开启(postfix已经开启),提 ...
- 头像上传uploadPreview插件
原文链接:https://blog.csdn.net/Alisa_L/article/details/52923953 uploadPreview 今天写头像上传,使用到uploadPreview插件 ...
- Java.util.properties读取配置文件分析
Java.util.properties API链接: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html Clas ...
- POJ 1486 Sorting Slides(二分图完全匹配必须边)题解
题意:给你n张照片的范围,n个点的坐标,问你能唯一确定那几个点属于那几张照片,例如样例中4唯一属于A,2唯一属于C,1唯一属于B,3唯一属于C 思路:进行二分图完全匹配,怎么判断唯一属于?匹配完之后删 ...
- Dubbo集群配置和官方文档
集群配置: https://blog.csdn.net/zh520qx/article/details/63679908 https://www.cnblogs.com/hd3013779515/p/ ...
- 论文笔记之:Optical Flow Estimation using a Spatial Pyramid Network
Optical Flow Estimation using a Spatial Pyramid Network spynet 本文将经典的 spatial-pyramid formulation ...
- HDU 5976 Detachment(拆分)
HDU 5976 Detachment(拆分) 00 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- Aviutl 视频处理软件
素材类:No.009 倒放(Video) http://www.bilibili.com/video/av3078207/ No.010 倒放(Object) http ...
- 2、ansilbe常用模块详解及inventory介绍
Ansible ansible格式: ansible <host-pattern> [-f forks] [-m module_name] [-a args] args: 用法 key=v ...
- Leetcode167-Two Sum II Input array is sorted-Easy
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...