我正在练习解递归和回溯练习。我遇到了一个问题,要打印列表列表中的所有笛卡尔积,其中每个子列表只包含不同的字符。当然,如果任何一个子列表是空的,那么最终的产品就是一个空列表。所以我想,“基本情况是什么?”当列表列表为空时,我将打印一个空列表。如果没有,则返回第一个子列表的第一个字符,加上从下一个子列表对列表列表的递归调用。 if len(lst)==0:
我当时正在阅读,我试图在这个版本的河内塔上实现一个回溯算法,使用文章第一部分中定义的状态。基本上,它适用于相对较小的数字,但在某些情况下,程序会出现以下错误:
terminate called after throwing an instance of std::bad_alloc what(): bad_alloc This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for