我正试图书写一种算法,确定从10x10电网中选择10个数值的可能解决办法。 两个数值不能相同。 共有10万个组合(超过3 600 000个)。
我最初的算法使用10套nes,简单地检查了10平方米的可能组合。 当我试图在我的MacBook上接手时,需要许多分钟的时间才能免除每只 log一只 log子对ole子的检测,这样我就可以看看这些检测。
问题在于,该表一直持续到测试编号714271,然后冻结。 这一结果可以重复。
我认为,它是一种记忆,有些反面的东西超出了其最大允许的价值,但从我的角度来说,这个数字没有任何意义。
法典认为:
-(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);
}
}
}
}
}
}
}
}
}
}
}
FINAL UPDATE I have a confession to make. I gave up trying to get the code to work in obj-c and rewrote it in Python. It has now been running for a couple of hours, checked 1.2 billion of the 10^10 combinations, has not clagged up the system memory and on average is using 50% less CPU time than the obj-c code. I love the look of Cocoa apps and the creation of the UI is brilliant but for sheer workability, Python is hard to beat.
灰色成像:
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