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. oracle /*+ SYS_DL_CURSOR */ 这个hint

    之前一直都没使用过 /*+ SYS_DL_CURSOR */这个提示,今天下午在排查一个性能问题的时候,发现出问题的session在执行一个带了SYS_DL_CURSOR提示的语句,类似于: 经查这个 ...

  2. pprof函数名未翻译、为函数地址0x00000232382788

    这几天在分析一个性能未达预期的功能,使用gperftools cpu profiler生成后,使用pprof格式化的时候,发现pprof出的结果函数名未翻译.为函数地址,如下所示: 每个节点代表一个函 ...

  3. ZVAL——PHP源码分析

    基于 PHP 5.6.20 ZVAL——php变量实现的基础 _zval_struct 结构体的定义位于 Zend/zend.h 322 行 typedef union _zvalue_value { ...

  4. Android - Resource 之 Drawable小结

    本篇直接选择性地翻译官方开发指南 ============================= Drawable有十种类型,如下 (1) - Bitmap file:这个简单,也可以用xml来更详细的定 ...

  5. Codeforces 835F Roads in the Kingdom - 动态规划

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...

  6. [Errno 2] No such file or directory

    Centos7.5 执行ansible命令报错 问题: [root@m01 ~]# ansible servers -a "hostname|grep web" -i ./host ...

  7. Junit的套件使用

    定义一个类,在类的上方添加@RunWith(Suite.class)和@SuiteClasses({XX.class,YY.class,...}) 实例有两个类分别为:public class Log ...

  8. FJUT3574 HOME_W的附加题(带权线段树)题解

    题意: 给定n个数a1,a2,a3,……an.和m次操作. 每次操作格式如下 x y k   表示将a[x]替换为y.并求替换后,前k小的数之和 思路:我们用带权线段树维护权值,这里就是维护i的个数n ...

  9. (转)看穿机器学习(W-GAN模型)的黑箱

        本文转自:http://www.360doc.com/content/17/0212/11/35919193_628410589.shtml#   看穿机器学习(W-GAN模型)的黑箱 201 ...

  10. HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)

    题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...