재귀함수? 이용해서 하노이의탑 솔루션 프린트해주는게 과제인데요
void solve::print_solution(int n, stack from, stack to, stack by, int i1, int i2)
{
if (n == 1)
{
move(from, to, i1, i2);
}
else
{
print_solution(n - 1, from, by, to, i1, i2);
print_solution(n - 1, by, to, from, i1, i2);
}
}
void solve::move(stack from, stack to, int i1, int i2)
{
if (from.data[from.top] > to.data[to.top])
{
cout << "옮기려는 원판의 크기가 더 큽니다.";
}
if (from.is_empty())
{
cout << "옮겨오려는 기둥이 비어있습니다";
}
else if (from.data[from.top] < to.data[to.top] || to.is_empty())
{
cout << "moving disk " << from.data[from.top] << " from pillar " << i1 << " to pillar " << i2 << endl;
to.push(from.pop());
}
}
요기서 프린트솔루션에서 무브함수 불러와서 무브함수에서 푸쉬,팝 해줬는데 그 결과가 반영이 안되고 원래 스택으로 돌아와요..
왜때문이죠?
일단 재귀방법이 틀렸는지는 신경쓰지 마시고 왜 초기화되는지 원인좀 알려주세요..
포인터 주소
포인터주소를 move함수 마지막 결과에 주고 프린트솔루션 맨처음에 주소달아주면 되는건가요?
스택구조체를 인자로받을때 포인터로 받으면 해결되는부분인듯