정리필요2

알고리즘 HW01 :: Hanoi Tower (Recursive)

ShineWithMe 2007. 9. 7. 00:45

첫번째 숙제

워밍업 정도.

어설픈 문서화는 어쩔거냐...

코드만 살짝 올려본다.

부록. 소스원본

#include <stdio.h>

#include <time.h>

#include <windows.h>

void Hanoi(char from, char to, char temp, int n);

int cnt; // disk movement count

void main(void){

LARGE_INTEGER Timefeq, Beg, End;

double dwtime; //runtime value

int n = 15; // disk number

QueryPerformanceFrequency(&Timefeq); // CPU ClockTick/second

QueryPerformanceCounter(&Beg); // start clock tick

Hanoi('1','2','3',n);

QueryPerformanceCounter(&End); // end clock tick

dwtime = (double)(End.QuadPart-Beg.QuadPart)/Timefeq.QuadPart; // (second)

// (end cpu clock tick - start cpu click tick) / CPU hz

// Show Disk movements and Runtime

printf("\n--- %d disks and 3 Poles Hanoi Tower --- \n", n);

printf("Number of Disc Movements: %d\n", cnt);

printf("Runtime : %.20lf second.\n",dwtime);

}

void Hanoi(char from, char to, char temp, int n){

if(n==1){

printf("Disk %d : Pole %c --> Pole %c \n", n, from, to);

cnt++;

}

else {

Hanoi(from,temp,to,n-1);

printf("Disk %d : Pole %c --> Pole %c \n", n, from, to);

cnt++;

Hanoi(temp,to,from,n-1);

}

}