#include <stdio.h>
#define MAX_DAYS 4
#define NUM_PLACES 4
typedef struct {
char name[20];
int time;
int score;
} Place;
int max(int a, int b) {
return (a > b) ? a : b;
}
void printOptimalRoute(int dp[NUM_PLACES + 1][MAX_DAYS + 1], Place places[], int numPlaces, int days) {
printf("最优路线为:\n");
int i = numPlaces;
int j = days;
while (i > 0 && j > 0) {
if (dp[i][j] != dp[i - 1][j]) {
printf("%s\n", places[i - 1].name);
j -= places[i - 1].time;
}
i--;
}
}
void optimizeRoute(Place places[], int numPlaces, int days) {
int dp[NUM_PLACES + 1][MAX_DAYS + 1];
for (int i = 0; i <= numPlaces; i++) {
for (int j = 0; j <= days; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (places[i - 1].time > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(dp[i - 1][j], places[i - 1].score + dp[i - 1][j - places[i - 1].time]);
}
}
}
printf("最大价值为: %d\n", dp[numPlaces][days]);
printOptimalRoute(dp, places, numPlaces, days);
}
int main() {
Place places[] = {{"故宫", 1, 7}, {"颐和园", 2, 8}, {"长城", 3, 9}, {"天坛", 1, 6}};
int numPlaces = sizeof(places) / sizeof(places[0]);
int days = 4;
optimizeRoute(places, numPlaces, days);
return 0;
}