中级农民
- 积分
- 133
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-3-5
- 最后登录
- 1970-1-1
|
用stack替代递归
struct Step
{
char from;
char temp;
char to;
int n;
};
void Move(char from, char temp, char to, int n)
{
stack< Step > ss;
Step s = {from, temp, to, n};
ss.push(s);
while(ss.size() > 0)
{
Step intent = ss.top();
assert(intent.n != 0);
if(intent.n == 1)
{
cout << "move " << " from " << intent.from << " to " << intent.to << endl;
ss.pop();
continue;
}
Step step1 = {intent.from, intent.to, intent.temp, intent.n - 1};
Step step2 = {intent.from, intent.temp, intent.to, 1};
Step step3 = {intent.temp, intent.from, intent.to, intent.n - 1};
ss.pop();
ss.push(step3);
ss.push(step2);
ss.push(step1);
}
} |
|