我的初始算法使用了10个嵌套的for循环,并简单地检查了每个可能的10个方块的组合。当我在我的MacBook上尝试运行应用程序时,需要多次多次的计算,为了缓解无聊,我将每个测试记录到控制台中,这样我就可以观察到测试结果的增加。
问题在于,该应用程序一直运行到测试编号714,271,然后就会冻结。这个结果是重复出现的。
我认为这是内存问题,在某些计数器超过其允许的最大值,但当我谷歌这个数字时,它没有任何意义。
代码如下:
-(IBAction)findSolutions:(id)sender{
NSMutableArray* flags = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
NSMutableString* solutionsString = [[NSMutableString alloc]init];
int a,b,c,d,e,f,g,h,i,j,z,sum;
for(a=0;a<=9;a++){
for(b=0;b<=9;b++){
for(c=0;c<=9;c++){
for(d=0;d<=9;d++){
for(e=0;e<=9;e++){
for(f=0;f<=9;f++){
for(g=0;g<=9;g++){
for(h=0;h<=9;h++){
for(i=0;i<=9;i++){
for(j=0;j<=9;j++){
for(z=0;z<=9;z++){
[flags replaceObjectAtIndex:z withObject:[NSNumber numberWithInt:0]];
} //Clear the flags matrix
//Load the flags matrix
[flags replaceObjectAtIndex:a withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:b withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:c withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:d withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:e withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:f withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:g withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:h withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]];
[flags replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:1]];
sum = 0;
for(z=0;z<=9;z++){
sum = sum + [[flags objectAtIndex:z]intValue];
} // sum the flags matrix. Will = 10 if all columns are filled
if (sum == 10) {
NSLog(@"Test no. %d, sum=%d, a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d",y,sum,a,b,c,d,e,f,g,h,i,j);
[solutionsString appendString:[NSString stringWithFormat:@"Test no. %d, sum=%d, a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d",y,sum,a,b,c,d,e,f,g,h,i,j]];
[txtSolutionsFound setStringValue:solutionsString];
} // These are possible solutions
NSLog(@"a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d",a,b,c,d,e,f,g,h,i,j);
}
}
}
}
}
}
}
}
}
}
}
最终更新
我要坦白一件事。我放弃了尝试在obj-c中让代码工作,改用Python重写了它。现在已经运行了几个小时,检查了10的10次方中的12亿个组合,没有卡住系统内存,并且平均使用的CPU时间比obj-c代码少50%。我喜欢Cocoa应用程序的外观和UI的创造,但就纯工作性而言,很难超过Python。
Python代码如下:
row0 = [164,116,106,76,122,46,109,86,7,134]
row1 = [70,170,108,25,182,77,134,192,60,26]
row2 = [14,108,140,130,90,46,6,154,168,90]
row3 = [138,48,130,127,54,2,112,78,76,98]
row4 = [86,65,110,22,64,82,46,62,146,94]
row5 = [70,194,20,152,76,162,54,98,122,50]
row6 = [91,0,116,230,32,138,203,175,104,88]
row7 = [68,222,87,124,99,209,28,147,108,72]
row8 = [51,85,74,40,132,98,118,159,70,44]
row9 = [30,122,92,190,8,77,152,7,106,70]
maxvalue = 0
flagmatrix = [0,0,0,0,0,0,0,0,0,0]
solutionmatrix = [0,0,0,0,0,0,0,0,0,0]
for a in range(0,10):
for b in range(0,10):
for c in range(0,10):
for d in range(0,10):
for e in range(0,10):
for f in range(0,10):
for g in range(0,10):
for h in range(0,10):
for i in range(0,10):
for j in range(0,10):
for z in range(0,10):
flagmatrix[z]=0
#This will clear the flag matrix
#Now load the matrix
flagmatrix[a]=1
flagmatrix[b]=1
flagmatrix[c]=1
flagmatrix[d]=1
flagmatrix[e]=1
flagmatrix[f]=1
flagmatrix[g]=1
flagmatrix[h]=1
flagmatrix[i]=1
flagmatrix[j]=1
sum=0
for flag in flagmatrix:
sum = sum+flag
if sum == 10:
print "solution found at ",a,b,c,d,e,f,g,h,i,j
solution = row0[a]+row1[b]+row2[c]+row3[d]+row4[e]+row5[f]+row6[g]+row7[h]+row8[i]+row9[j]
print "Solution = ", solution
if solution > maxvalue:
maxvalue = solution
solutionmatrix[1]=b
solutionmatrix[2]=c
solutionmatrix[3]=d
solutionmatrix[4]=e
solutionmatrix[5]=f
solutionmatrix[6]=g
solutionmatrix[7]=h
solutionmatrix[8]=i
solutionmatrix[9]=j
print "Current maxvalue = ", maxvalue
print "Final max value = ", maxvalue
print "Final solution matrix = ", solutionmatrix