为了探索一些解决方案,我需要生成所有可能性。我通过使用位掩码来实现,就像这样:
这让我有机会探索(如果NB=3):
for (long i = 0; i < 1L << NB; i++) {
System.out.println(Long.toBinaryString(i));
if(checkSolution(i)) {
this.add(i); // add i to solutions
}
}
this.getBest(); // get the solution with lowest number of 1
这让我有机会探索(如果NB=3):
000
001
010
011
100
101
110
111
我的问题是最佳解决方案是1的数量最少的解决方案。因此,为了在找到解决方案后尽快停止搜索,我希望有一个不同的顺序,并产生类似于这样的结果:
000
001
010
100
011
101
110
111
如果我能在获得第一个解决方案后停止搜索,那么搜索速度将会更快。但是我不知道如何更改循环以获得这个输出...
PS:NB未定义...
(0b011 + 1) > (1<<3)
即4 > 8
吗? - fabian1 << 3
当作100
,我的错误。 - Filipe Gonçalves