Đang tải…
Đang tải…
Đi sâu hết mức theo một nhánh rồi mới quay lui. Tìm được đường đi, nhưng không nhất thiết ngắn nhất.
Đưa điểm bắt đầu vào ngăn xếp.
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}