We have a list of bus routes. Each routes[i]
is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7]
, this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.
We start at bus stop S
(initially not on a bus), and we want to go to bus stop T
. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.
Example: Input: routes = [[1, 2, 7], [3, 6, 7]] S = 1 T = 6 Output: 2 Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
Note:
1 <= routes.length <= 500
.1 <= routes[i].length <= 500
.0 <= routes[i][j] < 10 ^ 6
.
分析:有点类似于求最短路径,那么马上联想到图的BFS的DFS。因为求的是最短所以优先采用BFS。对于每个bus route,因为他是循环发车的,即从头能到尾,所以一条线上的站肯定是互通的。遍历所有bus route,得到每个站在哪些route上,如果两个站所在的route相同则一定可达。再从起始点S出发,找到所有与其处在相同route上的station,如果有目标站T则停止遍历,否则继续以这些station为开始点继续广度遍历。
class Solution {
public int numBusesToDestination(int[][] routes, int S, int T) {
HashSet<Integer> visited = new HashSet<>(); // 访问标记,防止BFS时重复遍历
Queue<Integer> q = new LinkedList<>(); // BFS借助队列实现,DFS借助栈来实现
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>(); // station——station所在的所有route
int ret = 0;
if (S==T) return 0;
// 统计每个station和其所对在的bus route
for(int i = 0; i < routes.length; i++){
for(int j = 0; j < routes[i].length; j++){
ArrayList<Integer> buses = map.getOrDefault(routes[i][j], new ArrayList<>());
buses.add(i);
map.put(routes[i][j], buses);
}
}
q.offer(S); // 从初始站开始遍历
while (!q.isEmpty()) {
int len = q.size();
ret++;
for (int i = 0; i < len; i++) {
int cur = q.poll();
ArrayList<Integer> buses = map.get(cur);
for (int bus: buses) {
if (visited.contains(bus)) continue;
visited.add(bus);
for (int j = 0; j < routes[bus].length; j++) {
if (routes[bus][j] == T) return ret;
q.offer(routes[bus][j]);
}
}
}
}
return -1;
}
}