C++巧克力拼图
C++ Chocolate Puzzle
假设我有N块巧克力,它们必须按照到货顺序准确地装进p个盒子。每个巧克力也有一定数量的卡路里X,每个盒子的容量K必须小于或等于3*sum(x1,x2,…,xn)+max。
在这项任务中,我得到了每种巧克力的N、p和X,我必须找出尽可能低的K。有人能帮我吗?
示例:N=8,P=3,X={1,4,5,6,3,2,5,3}
K for first three chocolates = 3*(1+4+5) + 5^2 - 1^2 = 54
K for next two chocolates = 3*(6+3) + 6^2 - 3^2 = 54
K for last three chocolates = 3*(2+5+3) + 5^2 - 2^2 = 51
Lowest possible K = 54
因此,目标是使用K 最低的p盒来找到最佳组合
谢谢!
以下是我在Java中解决此问题的方法:
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ChocolatePuzzle {
private static final Map <String, Integer> solutions =
new HashMap <String, Integer> ();
private static final Map <String, Integer> bestMoves =
new HashMap <String, Integer> ();
private static int [] x;
private static int k (int from, int to)
{
int sum = x [from];
int max = x [from];
int min = x [from];
for (int i = from + 1; i < to; i++)
{
sum += x [i];
max = Math.max (max, x [i]);
min = Math.min (min, x [i]);
}
return sum * 3 + max * max - min * min;
}
public static int solve (int n, int p)
{
String signature = n + "," + p;
Integer solution = solutions.get (signature);
if (solution == null)
{
solution = Integer.valueOf (doSolve (n, p, signature));
solutions.put (signature, solution);
}
return solution.intValue ();
}
public static int doSolve (int n, int p, String signature)
{
if (p == 1)
{
bestMoves.put (signature, Integer.valueOf (x.length - n));
return k (n, x.length);
}
else
{
int result = Integer.MAX_VALUE;
int bestMove = 0;
int maxI = x.length - n - p + 1;
for (int i = 1; i <= maxI; i++)
{
int k = Math.max (k (n, n + i), solve (n + i, p - 1));
if (k < result)
{
result = k;
bestMove = i;
}
}
bestMoves.put (signature, Integer.valueOf (bestMove));
return result;
}
}
public static void main(String[] args) {
int n = 20;
int p = 5;
x = new int [n];
Random r = new Random ();
for (int i = 0; i < n; i++)
x [i] = r.nextInt (9) + 1;
System.out.println("N: " + n);
System.out.println("P: " + p);
System.out.print("X: {");
for (int i = 0; i < n; i++)
{
if (i > 0) System.out.print (", ");
System.out.print (x [i]);
}
System.out.println("}");
System.out.println();
int k = solve (0, p);
int o = 0;
for (int i = p; i > 0; i--)
{
int m = bestMoves.get (o + "," + i);
System.out.print ("{");
for (int j = 0; j < m; j++)
{
if (j > 0)
System.out.print (", ");
System.out.print (x [o + j]);
}
System.out.print ("} (k: ");
System.out.print(k (o, o + m));
System.out.println (")");
o += m;
}
System.out.println("min(k): " + k);
}
}
也许您可以在这段代码中找到一些有用的提示。
样本输入:
N: 20
P: 5
X: {1, 7, 6, 6, 5, 5, 7, 9, 1, 3, 9, 5, 3, 7, 9, 1, 4, 2, 4, 8}
样本输出:
{1, 7, 6, 6} (k: 108)
{5, 5, 7, 9} (k: 134)
{1, 3, 9, 5} (k: 134)
{3, 7, 9} (k: 129)
{1, 4, 2, 4, 8} (k: 120)
min(k): 134
相关文章:
- 如何从文件传递2D拼图
- 了解 8 女王拼图的对角线搜索
- C Words搜索拼图网格2D数组
- 8 通过BFS拼图
- C++ suduko 拼图,初始化网格 - 新手
- 递归拼图C 代码
- 拼图+功能指针
- C/C++ 拼图:使用单个 for 循环打印 1.15 15.1 的值
- 巧克力游戏:输入循环,不插入循环或打印游戏统计数据
- 用户输入c++的8皇后拼图
- c++中八皇后拼图程序的修改
- 组装一个简化的拼图的算法,其中所有的边都被识别
- UE4.9 c++拼图块寻址
- QT Creator c++编写Kakuro拼图和不确定使用什么小部件
- Stdin在C或c++: Spotify:反向二进制拼图
- 块拼图的C++算法
- C++巧克力拼图