【发布时间】:2011-04-29 17:15:03
【问题描述】:
我只是想知道当您需要返回值并比较它们以找到最佳解决方案(如动态编程)时,是否可以用显式堆栈替换递归?
类似的东西(它什么都不做,只是一个例子):
int resursiveFunction (int state) {
if (known[state]) return cache[state];
if (state == MAX_STATE) return 0;
int max = 0;
for (int i = 0 ; i < 5; i++) {
int points = curPoints (state) + recursiveFunction (state+i);
if (points > max) max = points;
}
known[state] = true;
cache[state] = max;
return max;
}
【问题讨论】: