Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

하노이탑/윤성복

From ZeroWiki
#include <iostream>
using namespace std;

int Mcount = 0; //이동횟수세기 위한 전역변수

int hanoi(int disk,int start, int other, int finish){
	Mcount++; //함수가 호출될때 마다 1씩 증가

	// 마지막이거나 디스크가 1일때 start 기둥 에서 finish 기둥 으로 옮김
	if(disk == 1) 
	cout << start << "에서 " << finish << endl;

	// 디스크갯수가 1이 아니면서 마지막이 아닐때
	else
	{
		hanoi(disk-1,start,finish,other); // 큰원반을 뺀 위에 것들을 other 기둥으로 옮기는 재귀함수 호출
		cout << start << "에서 " << finish << endl;
		hanoi(disk-1,other,start,finish); // other 기둥에 있는 것을 finish 기둥으로 옮기는 재귀함수 호출
	}
	return Mcount;
}

void main(){
	int disk,MoveCount;

	cout << "디스크 갯수 : ";
	cin >> disk;

	MoveCount = hanoi(disk,1,2,3);

	cout << endl << "최소 이동횟수" << MoveCount << endl;
}

하노이탑 데블스캠프2005