我正在尝试更多地了解算法,并构建了一个简单的算法,以使用蛮力查看是否可以从随机创建的数字网格中生成目标数字。我已经做了足够多的工作来检查加在一起的五个网格数字是否会创建目标,这对于我的目的来说应该足够了,但是这个过程非常慢,在 iOS 模拟器上大约需要 11 秒。我怎样才能在这里加快速度?有没有比使用我正在使用的所有循环更有效的方法?这是我的所有代码,是一个包含两个整数 GridNumber
的简单子类,并且.NSObject
number
tag
- (void)viewDidLoad
{
[super viewDidLoad];
// 0. Set up target number.
int random = arc4random() % 100 + 3;
NSNumber *target = [NSNumber numberWithInt: random];
// 1. Set up array of available numbers.
NSMutableArray *grid = [[NSMutableArray alloc] init];
for (int i = 1; i < 48; i++) {
GridNumber *number = [[GridNumber alloc] initWithRandomIntegerAndTag: i];
[grid addObject: number];
}
if ([self canTarget: target BeMadeFromGrid: grid]) NSLog(@"--- SOLVEABLE!");
else NSLog(@"--- UNSOLVEABLE!");
}
- (BOOL) canTarget: (NSNumber *) target BeMadeFromGrid: (NSArray *) grid
{
NSLog(@"TARGET NUMBER IS: %d", target.intValue);
// 2. See if the target already exists first.
for (GridNumber *firstValue in grid) {
if (firstValue.number == target.intValue) {
NSLog(@"SOLVEABLE IN 1: Grid already contains target!");
return YES;
}
}
// 3. Add elements once, see if any of those give the result.
for (GridNumber *firstValue in grid) {
for (GridNumber *secondValue in grid) {
int result = firstValue.number + secondValue.number;
if (result == target.intValue && firstValue.tag != secondValue.tag) {
NSLog(@"SOLVEABLE IN 2: Adding %d and %d creates target!", firstValue.number, secondValue.number);
return YES;
}
}
}
// 4. Add elements twice, see if any of those give the result.
for (GridNumber *firstValue in grid) {
for (GridNumber *secondValue in grid) {
for (GridNumber *thirdValue in grid) {
int result = firstValue.number + secondValue.number + thirdValue.number;
if (result == target.intValue && firstValue.tag != secondValue.tag && firstValue.tag != thirdValue.tag && secondValue.tag != thirdValue.tag) {
NSLog(@"SOLVEABLE IN 3: Adding %d, %d and %d creates target!", firstValue.number, secondValue.number, thirdValue.number);
return YES;
}
}
}
}
// 5. And three times..
for (GridNumber *firstValue in grid) {
for (GridNumber *secondValue in grid) {
for (GridNumber *thirdValue in grid) {
for (GridNumber *fourthValue in grid) {
int result = firstValue.number + secondValue.number + thirdValue.number + fourthValue.number;
if (result == target.intValue && firstValue.tag != secondValue.tag && firstValue.tag != thirdValue.tag && firstValue.tag != fourthValue.tag &&
secondValue.tag != thirdValue.tag && secondValue.tag != fourthValue.tag && thirdValue.tag != fourthValue.tag) {
NSLog(@"SOLVEABLE IN 4: Adding %d, %d, %d and %d creates target!", firstValue.number, secondValue.number, thirdValue.number, fourthValue.number);
return YES;
}
}
}
}
}
// 6. And four times..
for (GridNumber *firstValue in grid) {
for (GridNumber *secondValue in grid) {
for (GridNumber *thirdValue in grid) {
for (GridNumber *fourthValue in grid) {
for (GridNumber *fifthValue in grid) {
int result = firstValue.number + secondValue.number + thirdValue.number + fourthValue.number + fifthValue.number;
if (result == target.intValue && firstValue.tag != secondValue.tag && firstValue.tag != thirdValue.tag && firstValue.tag != fourthValue.tag &&
firstValue.tag != fifthValue.tag && secondValue.tag != thirdValue.tag && secondValue.tag != fourthValue.tag && secondValue.tag != fifthValue.tag &&
thirdValue.tag != fourthValue.tag && thirdValue.tag != fifthValue.tag && fourthValue.tag != fifthValue.tag) {
NSLog(@"SOLVEABLE IN 5: Adding %d, %d, %d, %d and %d creates target!", firstValue.number, secondValue.number, thirdValue.number, fourthValue.number, fifthValue.number);
return YES;
}
}
}
}
}
}
// 7. This is if it can't be made.
return NO;
}