iphone - how to fast and safely create pattern database for 6 tiles for the 663 approach to solve 15 puzzle? -
i creating pattern databases 6-6-3 pattern database approach solve 15 puzzle, code working generate possible patterns 3 tiles taking time 6 tiles crashes in between after generating fewer node. code far below-
-(void)createandinsertpatterns{ fifteenpuzzle *startingpuzzle=[[fifteenpuzzle alloc]init]; startingpuzzle.pattern=[self getpatternstring:startingpattern]; startingpuzzle.cost=0; [self enqueue:startingpuzzle]; //[self insertintodb:startingpuzzle.pattern:0]; fifteenpuzzle* currentpuzzle; while((currentpuzzle =[self dequeue])!=nil){ if(![self isalreadyadded:currentpuzzle.pattern]){ nsstring *patternstring=[nsstring stringwithstring:currentpuzzle.pattern]; nsinteger cost=currentpuzzle.cost; [self insertintodb:currentpuzzle.pattern:cost]; nsmutablearray *allpatterns = [self allneighborforpattern:[self getarrayfromstring:patternstring]]; (nsmutablearray *obj in allpatterns) { fifteenpuzzle *p=[[fifteenpuzzle alloc]init]; p.pattern=[self getpatternstring:obj]; p.cost=cost+1; [self enqueue:p]; } }else{ //nslog(@"pattern added"); } } } -(void)insertintodb :(nsstring*)patternstring :(int)no_of_moves{ [patternqueue setobject:@"yes" forkey:patternstring]; sqlite3_stmt *statement; nsstring *querysql = [nsstring stringwithformat:@"insert data values (\"%@\",\"%d\")",patternstring,no_of_moves]; if(sqlite3_prepare_v2(database, [querysql utf8string], -1, &statement, null) == sqlite_ok) { } if(sqlite3_step(statement) != sqlite_done ) { nslog( @"error: %s", sqlite3_errmsg(database) ); sqlite3_finalize(statement); }else { //nslog(@"query executed");; } sqlite3_reset(statement); } -(bool)isalreadyadded:(nsstring*)patternstring{ if ([[patternqueue objectforkey:patternstring]isequaltostring:@"yes"]) return yes; else return no; } -(void)enqueue:(fifteenpuzzle*)puzzle { if(![self isalreadyadded:puzzle.pattern]) [queue addobject:puzzle]; } -(fifteenpuzzle*)dequeue{ if (queue.count==0) { return nil; } fifteenpuzzle *puzzle=[queue objectatindex:0]; [queue removeobjectatindex:0]; return puzzle; } -(nsmutablearray*)allneighborforpattern :(nsmutablearray*)pattern{ nsmutablearray *allneighborpatterns=[[nsmutablearray alloc]init]; (int v=0; v<16;v++) { int value=[[pattern objectatindex:v]intvalue]; if (value==0) continue; int row=v/4; int col=v%4; if (col-1>=0) {//left if ([[pattern objectatindex:v-1]intvalue]==0) { nsmutablearray *left=[[nsmutablearray alloc]initwitharray:pattern]; [left exchangeobjectatindex:v withobjectatindex:v-1]; if([patternqueue objectforkey:[self getpatternstring:left]]==nil) [allneighborpatterns addobject:left]; } } if (col+1<4) {//right if ([[pattern objectatindex:v+1]intvalue]==0) { nsmutablearray *right=[[nsmutablearray alloc]initwitharray:pattern]; [right exchangeobjectatindex:v withobjectatindex:v+1]; if([patternqueue objectforkey:[self getpatternstring:right]]==nil) [allneighborpatterns addobject:right]; } } if (row-1>=0) {//top if ([[pattern objectatindex:v-4]intvalue]==0) { nsmutablearray *top=[[nsmutablearray alloc]initwitharray:pattern]; [top exchangeobjectatindex:v withobjectatindex:v-4]; if([patternqueue objectforkey:[self getpatternstring:top]]==nil) [allneighborpatterns addobject:top]; } } if (row+1<4) {//bottom if ([[pattern objectatindex:v+4]intvalue]==0) { nsmutablearray *bottom=[[nsmutablearray alloc]initwitharray:pattern]; [bottom exchangeobjectatindex:v withobjectatindex:v+4]; if([patternqueue objectforkey:[self getpatternstring:bottom]]==nil) [allneighborpatterns addobject:bottom]; } } } return allneighborpatterns; }
where patternqueue
used save patterns been added database , queue holds patterns expanded. using breadth first search expand nodes. 6 tiles combination database hold 16!/(16-10)!=5765760 patterns (nodes) code crashes after generating fewer nodes approximately 200000 in 5 minutes.
please suggest changes can increase speed or avoid chances of crash.
Comments
Post a Comment