From 31d89f48107aa2df3e3775da0481fdeaa2271fd4 Mon Sep 17 00:00:00 2001 From: 2006wu Date: Sun, 20 Jul 2025 03:52:33 +0800 Subject: [PATCH 1/2] 2006wu_dijkstra_hw --- .vscode/c_cpp_properties.json | 18 ++++++++++ .vscode/launch.json | 24 +++++++++++++ .vscode/settings.json | 59 ++++++++++++++++++++++++++++++ Dijkstra.cpp | 67 ++++++++++++++++++++++++++++++----- 4 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8a55c44 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/wu/DIT_code_folder/Navigation_folder/Navigation_Tutorial", + "program": "/home/wu/DIT_code_folder/Navigation_folder/Navigation_Tutorial/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/Dijkstra.cpp b/Dijkstra.cpp index 7d39740..ff9abd5 100644 --- a/Dijkstra.cpp +++ b/Dijkstra.cpp @@ -23,9 +23,10 @@ #include #include #include -#include +#include #include // for pair #include // for reconstructing the path +#include using namespace std; @@ -48,20 +49,68 @@ class Graph { // TODO: Implement Dijkstra's algorithm void shortestPath(int src, int dest) { - // Your implementation goes here - - // 1. Create a priority queue for vertices being processed - // 2. Create arrays for distances and for tracking the path - // 3. Initialize all distances as INFINITE and src distance as 0 - // 4. Process vertices in order of their distance from src - // 5. Reconstruct and print the shortest path from src to dest + + vector dist(V, INT_MAX); + vector prev(V, -1); + priority_queue, vector>, greater<>> pq; + + dist[src] = 0; + pq.push({0, src}); // {distance, vertices} + + while (!pq.empty()){ + int u = pq.top().second; + pq.pop(); + + for (auto [v,w] : adj[u]){ + if (dist[u] + w < dist[v]){ + dist[v] = dist[u] + w; + prev[v] = u; //紀錄點 + pq.push({dist[v], v}); + } + } + } + + if (dist[dest] == INT_MAX) { + cout << "No path.\n"; + return; + } + + // 印出最短時間 + cout << "Shortest travel time: " << dist[dest] << " hours\n"; + + // 回溯路徑 + vector path; + for (int at = dest; at != -1; at = prev[at]) + path.push_back(at); + reverse(path.begin(), path.end()); + + cout << "Path: "; + for (size_t i = 0; i < path.size(); ++i) { + cout << path[i]; + if (i != path.size() - 1) cout << " -> "; + } + cout << endl; } }; +/* +void define_the_map(Graph& g){ + + cout<<"Enter the edges: (u, v, w)"; +} + */ int main() { // Create graph with 6 cities (labeled 0 to 5) - Graph g(6); + /* + int v; + cout<<"Please Enter the number of city."; + cin>>v; + Graph g(v); + define_the_map(g); */ + + Graph g(6); + // Add roads with travel times (directed edges with weights) g.addEdge(0, 1, 2); // From city 0 to city 1, travel time: 2 hours g.addEdge(0, 2, 4); // From city 0 to city 2, travel time: 4 hours From c6e95b5a25929c1d284f49d470c0836c90b106f7 Mon Sep 17 00:00:00 2001 From: 2006wu Date: Sat, 2 Aug 2025 17:55:07 +0800 Subject: [PATCH 2/2] 2006wu_Astar --- AStar.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/AStar.cpp b/AStar.cpp index 120ca76..43af104 100644 --- a/AStar.cpp +++ b/AStar.cpp @@ -28,6 +28,7 @@ #include #include // for pair #include // for reconstructing the path +#include using namespace std; @@ -58,13 +59,66 @@ class Graph { // TODO: Implement A* algorithm void aStarSearch(int src, int dest) { // Your implementation goes here - // 1. Create a priority queue for vertices being processed // 2. Create arrays for g_score (actual distance from start) // and f_score (g_score + heuristic) // 3. Initialize all g_scores as INFINITE and src g_score as 0 // 4. Process vertices in order of their f_score (not just distance) // 5. Reconstruct and print the shortest path from src to dest + + using pii = pair; // typedef pair pii; 把最小的城市放在前面 + priority_queue, greater> Set; + + vector gScore(V, INT_MAX); + vector fScore(V, INT_MAX); + vector From(V, -1); // notes every vertices where are you from + + gScore[src] = 0; + fScore[src] = heuristic[src]; + + Set.push({fScore[src], src}); + + while(!Set.empty()){ + int current = Set.top().second; + Set.pop(); + + if (current == dest){ // find destination, backtrack the path + stack path; + int temp = dest; + while (temp != -1){ + path.push(temp); + temp = From[temp]; + } + + cout << "Shortest travel time " << gScore[dest] << " hours." << endl; + cout << "Path: "; + while (!path.empty()){ + cout << path.top(); + path.pop(); + if (!path.empty()){ + cout << " -> "; + } + } + cout << endl; + return; + } + + for (auto neighborPair : adj[current]){ + int neighbor = neighborPair.first; + int weight = neighborPair.second; + + int temp_gScore = gScore[current] + weight; + + if (temp_gScore < gScore[neighbor]){ + From[neighbor] = current; + gScore[neighbor] = temp_gScore; + fScore[neighbor] = temp_gScore + heuristic[neighbor]; + Set.push({fScore[neighbor], neighbor}); + } + } + } + + cout << "No path exists from " << src << " to " << dest << "." << endl; } };