import java.util.HashMap;
import java.util.Map;
public class TravelOptimization {
static class Sightseeing {
String name;
int days;
int score;
Sightseeing(String name, int days, int score) {
this.name = name;
this.days = days;
this.score = score;
}
}
public static void main(String[] args) {
Sightseeing[] sights = {
new Sightseeing("故宫", 1, 7),
new Sightseeing("颐和园", 2, 8),
new Sightseeing("长城", 3, 9),
new Sightseeing("天坛", 1, 6)
};
int totalDays = 4;
Map<String, Integer> dp = new HashMap<>();
dp.put("", 0);
for (int i = 0; i < sights.length; i++) {
Sightseeing sight = sights[i];
Map<String, Integer> newDp = new HashMap<>(dp);
for (Map.Entry<String, Integer> entry : dp.entrySet()) {
String prevKey = entry.getKey();
int prevScore = entry.getValue();
int newDays = 0;
int newScore = 0;
for (int j = 0; j < prevKey.length(); j++) {
if (prevKey.charAt(j) == '1') {
newDays++;
}
}
if (newDays + sight.days <= totalDays) {
newDays += sight.days;
newScore = prevScore + sight.score;
String newKey = prevKey + i;
newDp.put(newKey, Math.max(newDp.getOrDefault(newKey, 0), newScore));
}
}
dp = newDp;
}
int maxScore = 0;
String bestCombination = "";
for (Map.Entry<String, Integer> entry : dp.entrySet()) {
if (entry.getValue() > maxScore) {
maxScore = entry.getValue();
bestCombination = entry.getKey();
}
}
System.out.println("最大评分为:" + maxScore);
System.out.print("选择的景点为:");
for (int i = 0; i < bestCombination.length(); i++) {
if (bestCombination.charAt(i) == '0') continue;
System.out.print(sights[i].name + " ");
}
}
}