Updating problem 2
This commit is contained in:
parent
3f2da26b0e
commit
57f68bf930
1 changed files with 36 additions and 2 deletions
38
lab10/2.c
38
lab10/2.c
|
@ -16,6 +16,39 @@ void load_matrix(FILE * file) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void do_all_routes2() {
|
||||||
|
int distance;
|
||||||
|
int routes = 0;
|
||||||
|
int min_distance = 0;
|
||||||
|
int min_route = -1;
|
||||||
|
|
||||||
|
for (int a = 0; a < 5; a++) {
|
||||||
|
if ( a == 1 ) { continue; }
|
||||||
|
for (int b = 0; b < 5; b++) {
|
||||||
|
if ( b == 1 || b == a) { continue; }
|
||||||
|
for (int c = 0; c < 5; c++) {
|
||||||
|
if ( c == 1 || c == b || c == a) { continue; }
|
||||||
|
for (int d = 0; d < 5; d++) {
|
||||||
|
if ( d == 1 || d == c || d == b || d == a) { continue; }
|
||||||
|
distance = city_distance[1][a] + city_distance[a][b] + city_distance[b][c] + city_distance[c][d];
|
||||||
|
routes += 1;
|
||||||
|
if (!min_distance) {
|
||||||
|
min_distance = distance;
|
||||||
|
min_route = routes;
|
||||||
|
} else if ( distance < min_distance) {
|
||||||
|
min_distance = distance;
|
||||||
|
min_route = routes;
|
||||||
|
}
|
||||||
|
printf("Route %02d, [%s - %s - %s - %s - %s] Total distance: %d\n",
|
||||||
|
routes, cities[1], cities[a], cities[b], cities[c], cities[d], distance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Minimum route: %d with the minimum distance %d\n", min_route, min_distance);
|
||||||
|
}
|
||||||
|
|
||||||
void do_all_routes() {
|
void do_all_routes() {
|
||||||
FILE * file;
|
FILE * file;
|
||||||
if ( !(file = fopen("routes.txt", "r")) ) { exit(0); }
|
if ( !(file = fopen("routes.txt", "r")) ) { exit(0); }
|
||||||
|
@ -32,7 +65,7 @@ void do_all_routes() {
|
||||||
min_distance = distance;
|
min_distance = distance;
|
||||||
min_route = i;
|
min_route = i;
|
||||||
}
|
}
|
||||||
printf("Route %d, [%s - %s - %s - %s - %s] Total distance: %d\n",
|
printf("Route %2d, [%s - %s - %s - %s - %s] Total distance: %d\n",
|
||||||
i, cities[1], cities[a], cities[b], cities[c], cities[d], distance);
|
i, cities[1], cities[a], cities[b], cities[c], cities[d], distance);
|
||||||
}
|
}
|
||||||
printf("Minimum route: %d with the minimum distance %d\n", min_route, min_distance);
|
printf("Minimum route: %d with the minimum distance %d\n", min_route, min_distance);
|
||||||
|
@ -50,7 +83,8 @@ int main(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
load_matrix(file);
|
load_matrix(file);
|
||||||
do_all_routes();
|
// do_all_routes();
|
||||||
|
do_all_routes2();
|
||||||
|
|
||||||
if ( fclose(file) ) {
|
if ( fclose(file) ) {
|
||||||
perror("File can't be closed!");
|
perror("File can't be closed!");
|
||||||
|
|
Loading…
Reference in a new issue