Finished backtrack algorithm