Java 找到组合,给定n个盒子和x个球
我正在做一个项目,其中我有三个盒子(截至目前),每个盒子都有一些球的颜色 因此,我将它们存储在字符串映射和字符串列表中,如下所示Java 找到组合,给定n个盒子和x个球,java,algorithm,list,map,combinations,Java,Algorithm,List,Map,Combinations,我正在做一个项目,其中我有三个盒子(截至目前),每个盒子都有一些球的颜色 因此,我将它们存储在字符串映射和字符串列表中,如下所示 Map<String, List<String>> boxBallMap = new LinkedHashMap<String, List<String>>(); 因此,盒子中球的可能组合可以是- (A点)::所有箱子的球数相同- {box1=[blue, red, orange]} {box2=[blue, red,
Map<String, List<String>> boxBallMap = new LinkedHashMap<String, List<String>>();
因此,盒子中球的可能组合可以是-
(A点)::所有箱子的球数相同-
{box1=[blue, red, orange]}
{box2=[blue, red, orange]}
{box3=[blue, red, orange]}
or
(B点):任何盒子都没有球。假设box3没有球-
{box1=[blue, red, orange]}
{box2=[blue, red, orange]}
{box3=[]}
or
(C点):有些盒子的球数较少。假设box2只有两个球-
{box1=[blue, red, orange]}
{box2=[blue, red]}
{box3=[blue, red, orange]}
or
(D点)::任何盒子都没有球。假设box3和box2没有球-
{box1=[blue, red, orange]}
{box2=[]}
{box3=[]}
问题陈述:-
基于上述输入,我需要返回一个映射,该映射将是列表
,例如对于(点a),下面的映射将作为输出返回-
[{box1=blue, box2=red, box3=orange},
{box1=red, box2=orange, box3=blue},
{box1=orange, box2=blue, box3=red}]
在这里,如果您看到,每一行的每个框都有不同颜色的球-表示框1为蓝色,框2为红色,框3为橙色。我不能在每一排都有相同颜色的球。所以这种组合是不可能的,因为它有两个盒子的相同颜色的球
{box1=blue, box2=blue, box3=orange}
还有,在第二排,我不会用第一排的球来做那个盒子
输出组合是根据传递的输入生成的,如(点A)所示
现在,让我们假设(B点)作为一个输入,其中框3
没有任何球,我将返回另一个映射,如下所示,它也将是列表
[{box1=blue, box2=red},
{box1=red, box2=orange},
{box1=orange, box2=blue}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
List<String> balls1 = Arrays.asList("red", "blue", "orange");
List<String> balls2 = Arrays.asList("red", "blue", "orange");
List<String> balls3 = Arrays.asList("red", "blue", "orange", "purple", "pink");
在上面的输出中,您可以看到没有box3
,因为它没有输入,但每行中的box1和box2具有交替的球颜色
现在,让我们假设(点C)作为一个输入,其中框2
只有两种颜色的球,我将返回另一个映射,如下所示,它也将是列表
[{box1=blue, box2=red},
{box1=red, box2=orange},
{box1=orange, box2=blue}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
List<String> balls1 = Arrays.asList("red", "blue", "orange");
List<String> balls2 = Arrays.asList("red", "blue", "orange");
List<String> balls3 = Arrays.asList("red", "blue", "orange", "purple", "pink");
在上面的输出中,您可以看到在第二行中没有box2
,因为box2
只有红色和蓝色的球的颜色,为了使组合正确,box2位于第一行和第三行只是为了保持规则,即每行都有球的交替颜色
现在我无法理解我将如何编写这样的方法,它可以根据我为这个问题传递的输入返回映射
注意:-
这里的框现在总是三个,但是球可以根据上面输入中显示的不同而变化
在这方面,任何建议都会有很大帮助。谢谢
更新:-
我的基本问题是如上所示的球和盒子的输入-我如何返回映射,以便在每一行中,盒子使用交替/不同颜色的球,并且它们需要确保在前一行中,这些球的颜色没有被同一个盒子使用
对于(点C)作为一个输入,其中框2
只有两种颜色的球,我想返回如下所示的映射,该映射也将是列表
-
[{box1=blue, box2=red},
{box1=red, box2=orange},
{box1=orange, box2=blue}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
List<String> balls1 = Arrays.asList("red", "blue", "orange");
List<String> balls2 = Arrays.asList("red", "blue", "orange");
List<String> balls3 = Arrays.asList("red", "blue", "orange", "purple", "pink");
- 在第一行中,
box1为蓝色
,box2为红色
,box3为橙色
,具有球的交替颜色李>
- 在第二行,
框1有红色
为什么?bcozblue
已在第1行中用于第1行,第3行为蓝色,第2行中无box2
- 第三排也是如此
我之前有一个解决方案,但假设每个盒子中的球数始终相同-
public List<Map<String, String>> createMappings(List<String> boxes, List<String> balls) {
List<Map<String, String>> result = new ArrayList<Map<String, String>>();
for(int i = 0; i < balls.size(); i++) {
Map<String, String> row = new HashMap<String,String>();
for(int j = 0; j < boxes.size(); j++) {
String box = boxes.get(j);
int ballIndex = (j + i) % balls.size();
String ball = balls.get(ballIndex);
row.put(box, ball);
}
result.add(row);
}
return result;
}
而且,它对以下输入也不起作用-
[{box1=blue, box2=red},
{box1=red, box2=orange},
{box1=orange, box2=blue}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]
List<String> balls1 = Arrays.asList("red", "blue", "orange");
List<String> balls2 = Arrays.asList("red", "blue", "orange");
List<String> balls3 = Arrays.asList("red", "blue", "orange", "purple", "pink");
你可以考虑下面的策略(代码没有给出,因为这是“家庭作业”):
创建一个具有颜色的Ball
类
创建一个Box
类,该类具有球数计数方法和addBall方法
在框中创建一个“choose”方法,你给它一个球数组,它将从框中拉出一个与球数组输入或null上的任何颜色都不匹配的球
通过创建列(框中最大球数的大小)开始处理输出,然后对于第1行,从框1中拉出一个球,该球没有先前拉出的球的颜色)
对于第2行,从框2中拉出一个列中没有颜色的球)
如果我正确理解了这个问题,你可以做以下几点:
根据盒子中的球数对盒子进行排序(从最小的盒子到最大的盒子,按升序排列)李>
虽然还有颜色
循环遍历已排序的框列表
在每次迭代中,从框中选择一种颜色(如果还有),该颜色在当前迭代(while循环)中尚未选择
希望这有帮助。没有密码,必须睡觉
编辑:以下是伪代码:
arranged_colors = [] // empty list, this is you desired output
sort_the_boxes(boxes) // ascending, by the number of colors in it
while( there_are_more_colors_left() ) { // a method that is easy to implement
current_list = [] // empty list
for( box in boxes ) {
for( color in box ) {
if( not color in current_list ) {
current_list.add(color)
box.remove(color)
break
}
}
}
aranged_colors.add(current_colors)
}
基本上,你将不得不结合所有可能的颜色框。在每一个新行中,一个框都会获得它在前一行中的下一个颜色。如果你写下所有可能的方框/颜色组合并写下所有的索引,它会变得更加清晰PointA就是一个完美的例子:
输入
{box1=[blue, red, orange]}
{box2=[blue, red, orange]}
{box3=[blue, red, orange]}
上述输入的所有组合为(前面有boxIndex、colorIndex):
您正在查找以下输出:
{box1=blue, box2=red, box3=orange}
{box1=red, box2=orange, box3=blue}
{box1=orange, box2=blue, box3=red}
因此,您要查找的索引如下:
row1 0,0 1,1 2,2
row2 0,1 1,2 2,0
row3 0,2 1,0 2,1
现在,当您知道自己在寻找什么时,编写一些循环就变得很容易了(免责声明:就我正确理解您的问题/未完全测试!!!):
输出b
@Test
public void createFromPointB() {
// {box1=[blue, red, orange]}
// {box2=[blue, red, orange]}
// {box3=[]}
// [{box1=blue, box2=red},
// {box1=red, box2=orange},
// {box1=orange, box2=blue}]
// 0,0 {box1=blue}
// 0,1 {box1=red}
// 0,2 {box1=orange}
// 1,0 {box2=blue}
// 1,1 {box2=red}
// 1,2 {box2=orange}
// 2,x {box3=blue}
// 2,x {box3=red}
// 2,X {box3=orange}
// 0,0 1,1 2,x
// 0,1 1,1 2,x
// 0,2 1,0 2,x
Map<String, List<String>> input = new LinkedHashMap<>();
input.put("box1", Arrays.asList("blue", "red", "orange"));
input.put("box2", Arrays.asList("blue", "red", "orange"));
input.put("box3", Collections.<String>emptyList());
List<Map<String, String>> output = create(input);
for(Map<String, String> e : output) {
System.out.println(e);
}
}
{box1=blue, box2=red}
{box1=red, box2=orange}
{box1=orange, box2=blue}
输出UTC
@Test
public void createFromPointC() {
// {box1=[blue, red, orange]}
// {box2=[blue, red]}
// {box3=[blue, red, orange]}
// [{box1=blue, box2=red, box3=orange},
// {box1=red, box3=blue},
// {box1=orange, box2=blue, box3=red}]
// 0,0 {box1=blue}
// 0,1 {box1=red}
// 0,2 {box1=orange}
// 1,0 {box2=blue}
// 1,1 {box2=red}
// 1,x {box2=orange}
// 2,0 {box3=blue}
// 2,1 {box3=red}
// 2,2 {box3=orange}
// 0,0 1,1 2,2
// 0,1 1,x 2,0
// 0,2 1,0 2,1
Map<String, List<String>> input = new LinkedHashMap<>();
input.put("box1", Arrays.asList("blue", "red", "orange"));
input.put("box2", Arrays.asList("blue", "red"));
input.put("box3", Arrays.asList("blue", "red", "orange"));
List<Map<String, String>> output = create(input);
for(Map<String, String> e : output) {
System.out.println(e);
}
}
{box1=blue, box2=red, box3=orange}
{box1=red, box3=blue}
{box1=orange, box2=blue, box3=red}
希望这有助于或至少给你一些提示,帮助你找到解决方案<
Set<String> generationHistory = new LinkedHashSet<>();
int colorIndex = 0;
int index = boxes.size() > colors.size() ? boxes.size() : colors.size();
for(int i = 0; i < index; i++) {
Map<String, String> row = new LinkedHashMap<>();
output.add(row);
colorIndex = i;
for(int j = 0; j < index; j++) {
int boxIndex = j;
if(boxIndex >= boxes.size()) {
boxIndex = 0;
}
String box = boxes.get(boxIndex);
List<String> boxColors = input.get(box);
if(colorIndex >= colors.size()) {
colorIndex = 0;
}
String color = colors.get(colorIndex++);
// a combination is generated only if the actual
// colors does exist in the actual box
// and it has not already been generated i all previous rows
if(boxColors.contains(color) && isNotYetGenerated(box, color, generationHistory)) {
row.put(box, color);
}
}
}
@Test
public void createFromPointF() {
// {box1=red, box2=blue, box3=orange}
// {box1=blue, box2=orange, box3=purple}
// {box1=red, box3=pink}
// {box3=red, box1=orange}
// {box3=blue}
// 0,0 {box1=red}
// 0,1 {box1=blue}
// 0,2 {box1=orange}
// 0,x {box1=purple}
// 0,x {box1=pink}
//
// 1,0 {box2=red}
// 1,1 {box2=blue}
// 1,2 {box2=orange}
// 1,x {box2=purple}
// 1,x {box2=pink}
//
// 2,0 {box3=red}
// 2,1 {box3=blue}
// 2,2 {box3=orange}
// 2,3 {box3=purple}
// 2,4 {box3=pink}
// 0,0 1,1 2,2
// 0,1 1,2 2,3
// 0,x 1,x 2,0
// 0,x 1,0 2,1
Map<String, List<String>> input = new LinkedHashMap<>();
input.put("box1", Arrays.asList("red", "blue", "orange"));
input.put("box2", Arrays.asList("red", "blue", "orange"));
input.put("box3", Arrays.asList("red", "blue", "orange", "purple", "pink"));
List<Map<String, String>> output = create(input);
Assert.assertEquals(
"{box1=red, box2=blue, box3=orange}\r\n" +
"{box1=blue, box2=orange, box3=purple}\r\n" +
"{box1=orange, box3=pink}\r\n" +
"{box3=red}\r\n" +
"{box2=red, box3=blue}\r\n", toString(output));
}
private String toString(List<Map<String, String>> output) {
StringWriter sw = new StringWriter();
for(Map<String, String> e : output) {
sw.write(e.toString());
sw.write("\r\n");
}
return sw.toString();
}
{box1=red, box2=blue, box3=orange}
{box1=blue, box2=orange, box3=purple}
{box1=orange, box3=pink}
{box3=red}
{box2=red, box3=blue}