Loading…
Loading…
Plunges as deep as possible along one branch before backtracking. Finds a path, but not necessarily the shortest.
Push the start node.
1Deque<Integer> st = new ArrayDeque<>();2st.push(start);3while (!st.isEmpty()) {4 int node = st.pop();5 if (visited[node]) continue;6 visited[node] = true;7 if (node == goal) return buildPath();8 for (int nb : neighbors(node))9 if (!visited[nb]) { parent[nb] = node; st.push(nb); }10}