组合益智游戏

发布于 2024-11-05 13:22:45 字数 748 浏览 2 评论 0 原文

我想知道解决以下问题的最佳方法是什么:

我现在有一个 Map>。假设它是一张餐食地图,其中包含您可以在该餐食中提供的一系列饮料。例如

早餐-橙汁 早餐 - 咖啡

午餐 - 橙汁 午餐 - 苏打水 午餐 - 啤酒

晚餐 - 苏打水 晚餐 - 啤酒 晚餐 - 酒

所以我的收藏实际上是Map>

我需要做的是创建一个List>餐食中各种饮料的组合。在本例中我将有 18 种组合。例如

[

[ 早餐->橙汁, 午餐->橙汁, 晚餐->苏打 ]

[ 早餐->橙汁, 午餐->橙汁, 晚餐->啤酒 ]

[ 早餐->橙汁, 午餐->橙汁, 晚餐->葡萄酒 ]

[ 早餐->橙汁, 午餐->苏打, 晚餐->苏打, ]

ETC... ]

我有兴趣看看其他人将如何创建最终的系列。

另外,我使用的是java,所以漂亮的功能性东西是禁止的。

谢谢

编辑

膳食类型是动态的。也就是说,可以从列表中删除晚餐或添加早午餐。

I'm wondering what the best way of solving the following problem is:

I have at the moment a Map<String, Collection<String>>. Let's say its a map of meals with a collection of beverages you could serve at that meal. E.g.

Breakfast - Orange Juice
Breakfast - Coffee

Lunch - Orange Juice
Lunch - Soda
Lunch - Beer

Dinner - Soda
Dinner - Beer
Dinner - Wine

So my collection really is Map<Meal, Collection<Beverage>>

What I need to do is create a List<Map<String,String>> of all the various combinations of beverages over the meals. I will have in this case 18 combinations. E.g.

[

[
Breakfast -> Orange Juice,
Lunch -> Orange Juice,
Dinner -> Soda
]

[
Breakfast -> Orange Juice,
Lunch -> Orange Juice,
Dinner -> Beer
]

[
Breakfast -> Orange Juice,
Lunch -> Orange Juice,
Dinner -> Wine
]

[
Breakfast -> Orange Juice,
Lunch -> Soda,
Dinner -> Soda,
]

etc...
]

I'm interested in seeing how others would go about creating the final collection.

Also, I'm using java, so nifty functional stuff is off limits.

Thanks

EDIT

Meal types are dynamic. That is, dinner could be removed from the list or brunch added.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-11-12 13:22:45

如果我没猜错的话,这里没有魔法,只需在嵌套循环中迭代 3 个集合即可。

for (String breakfastBev : breakfast) {
  for (String lunchBev : lunch) {
    for (String dinnerBev : dinner) {
      System.out.println(breakfastBev + ", " + lunchBev + ", " + dinnerBev);
    }
  }
}

我确信现在您可以弄清楚如何使用 Map 和输出 List 来实现这一点。 :-)

编辑:对于更改的需求,一种方法是递归:

main() {
    List<Map<String, String>> output = new ArrayList<Map<String, String>>();
    recordBeverages(new HashMap(), beveragesByMeal, output);
}

void recordBeverages(Map visited, Map meals, List<Map> output) {
    if(meals.isEmpty()) {
        output.add(visited);
    }
    String mealType = meals.keySet().iterator().next();
    Map remainingMeals = new HashMap(visited);
    remainingMeals.keySet().remove(mealType);
    for(Beverage bev : meals.get(mealType)) {
        Map newVisited = new HashMap(visited);
        newVisited.put(mealType, bev);    

        recordBeverages(newVisited, remainingMeals, output);
    }
}

未经测试,但您明白了。

If I got it right, there's no magic here, just iterate over 3 collections in a nested loop.

for (String breakfastBev : breakfast) {
  for (String lunchBev : lunch) {
    for (String dinnerBev : dinner) {
      System.out.println(breakfastBev + ", " + lunchBev + ", " + dinnerBev);
    }
  }
}

I'm sure now you can figure out how to do it with your Map and output List. :-)

EDIT: For the changed requirements, one way to do it is recursion:

main() {
    List<Map<String, String>> output = new ArrayList<Map<String, String>>();
    recordBeverages(new HashMap(), beveragesByMeal, output);
}

void recordBeverages(Map visited, Map meals, List<Map> output) {
    if(meals.isEmpty()) {
        output.add(visited);
    }
    String mealType = meals.keySet().iterator().next();
    Map remainingMeals = new HashMap(visited);
    remainingMeals.keySet().remove(mealType);
    for(Beverage bev : meals.get(mealType)) {
        Map newVisited = new HashMap(visited);
        newVisited.put(mealType, bev);    

        recordBeverages(newVisited, remainingMeals, output);
    }
}

Not tested, but you get ths idea.

我们的影子 2024-11-12 13:22:45

使用一系列柜台,柜台数量与餐点数量相同。循环增加第一个饮料,直到达到第一顿饭的最后一杯饮料,然后增加下一个饮料(滴下)。当最后一顿饭的最后一杯饮料喝完时,你就完成了。

我将把代码留给你。

Use an array of counters with as many counters as there are meals. In a loop increase the first one until it reaches the last beverage for the first meal and then increase the next on (trickling down). When the last beverage of the last meal is reached you are done.

I'll leave the code for that up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文