Skip to content

Instantly share code, notes, and snippets.

@biscuitrainbow
Created December 12, 2017 17:55
Show Gist options
  • Save biscuitrainbow/756beb4b4729f0ee5e7b12b537e54fae to your computer and use it in GitHub Desktop.
Save biscuitrainbow/756beb4b4729f0ee5e7b12b537e54fae to your computer and use it in GitHub Desktop.
int get_tsp(int current_city, int matrix[][10], const int visited_cities[], int n_cities) {
int city, nearest_city = 999; // ให้ค่าตำแหน่งของเมืองที่ใกล้ที่สุดเริ่มต้นเป็น 999 ตำแหน่งของเมืองก็พวก 1 2 3 4
int minimum = 999; // ให้ระยะทางที่ใกล้ที่สุดของเมืองปัจบันเป็น 999 ระยะทางก็พวก 5 0 6 9
for (city = 0; city < n_cities; city++) { // สร้างลูปโดยมีรอบตามจำนวนของเมือง
if ((matrix[current_city][city] != ITSELF) && (visited_cities[city] == UNVISITED)) { // เชคว่าเมืองปัจจจุบันใช้เมืองตัวเองไหม และ เคยไปมาหรือยัง
if (matrix[current_city][city] < minimum) { // ถ้าระยะห่างของเมืองปัจจุบันน้อยกว่า minimun ตัวก่อน (ตอนแรกคือ 999)
minimum = matrix[current_city][city]; // ให้ minimum เป็นระยะห่างของเมืองนั้น (เมืองที่ใกล้ที่สุดในตอนนั้น)
nearest_city = city; // ให้ nearest_city เป็นตำแหน่งของเมืองนั้น (เมืองที่ใกล้ที่สุดในตอนนั้น)
}
}
}
return nearest_city; // ส่งตำแหน่งเมืองที่ใกล้ที่สุดออกมา ถ้าไม่มีเมืองไหนแล้วจะส่ง 999 ออกมา
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment