int Board::getNextCloseTo(int s, int d, bool diag, int lastIndex) { if(s == d) return(-1); // What can I say, we're here! ;o) const int firstN = diag ? 4 : 0; const int lastN = diag ? 8 : 4; Pos *root = new Pos(0, s, 0), *list = root; // List of indexes. for(; list; list = list->listNext()) { Pos *p; // Check if current list node is the destination position. if(list->index() == d) { // Find first movement after root. for(; ; list = p) { p = list->parent(); if(p == root) { // This is our move. int nextSq = list->index(); delete root; index(nextSq); return(nextSq); } } qFatal("Never here"); } // Make possible moves. for(int n = firstN; n < lastN; n ++) { int i = getNext(n, list->index()); int pri = list->price() + 1; // getNext returned valid place? if(! inBounds(i) || (! isEmpty(i) && i != d)) { // Or place is out of map or it's not empty, // so go to the next possible move. continue; } int pi = list->parent() ? list->parent()->index() : lastIndex; if(pi != -1 && direction(pi, list->index()) != direction(list->index(), i)) { pri += 10; } // Check if position wasn't processed yet. if( (p = root->searchBTree(i))) { // Position already processed. // Check price of found position with current one. if(p->price() > pri) { // We found a cheapear way to reach the same // place, so let's change the parent and price of p. p->setPrice(list->price() + 1); p->setParent(list); list->addList(p); } continue; } // Create new Pos class instance. p = new Pos(list, i, pri); // Add. list->addList(p); root->addFList(p); root->addBTree(p); } } // Solution not found. delete root; return(-1); }