//Public Interface -- find all paths from des to des void findAllPath(Step cur, Step des) { if(!outOfBorder(cur.x, cur.y) || !outOfBorder(des.x, des.y)) return; m_path.push_back(cur); m_maze[cur.x][cur.y] = -1; if(cur.x == des.x && cur.y == des.y) //find a path { output(); m_path.erase(m_path.end() - 1); //backtraking m_maze[cur.x][cur.y] = 0; return; } //Try four different directions if(m_maze[cur.x][cur.y-1] == 0) findAllPath(Step(cur.x, cur.y-1), des); if(m_maze[cur.x+1][cur.y] == 0) findAllPath(Step(cur.x+1, cur.y), des); if(m_maze[cur.x][cur.y+1] == 0) findAllPath(Step(cur.x, cur.y+1), des); if(m_maze[cur.x-1][cur.y] == 0) findAllPath(Step(cur.x-1, cur.y), des); m_path.erase(m_path.end() - 1); m_maze[cur.x][cur.y] = 0; }
void findAllPath(tNode* root, node** nl, int sum) { if(root == NULL) { printf("node is null\n"); return ; } int* key = (int*)root->item; printf("Node of this path:"); print_list(*nl, printl); sleep(1); printf("Sum of this path:%d\n", sum + *key); if(root->lchild != NULL) { listAppend(nl, root->lchild); findAllPath(root->lchild, nl, sum + *key); listPop(nl); } if(root->rchild != NULL) { listAppend(nl, root->rchild); findAllPath(root->rchild, nl, sum + *key); listPop(nl); } }
int main() { int num = 20; int* arr = createArr(num, INT); print_arr(arr, num); tNode* root = createBSTRoot(arr, num); print_tree(root, print); printf("\n"); node* nl = createNode(root); findAllPath(root, &nl, 0); free(nl); return 0; }