Last active
August 29, 2015 13:57
-
-
Save kamiyaowl/9911656 to your computer and use it in GitHub Desktop.
ハノイの塔
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#define PORL_COUNT 3 | |
#define TARGET_COUNT 5 | |
void print_hanoi(int (*porl)[TARGET_COUNT]) | |
{ | |
int i,j; | |
for(j = 0 ; j < PORL_COUNT ; ++j) | |
{ | |
printf("%d : ", j); | |
for(i = 0 ; i < TARGET_COUNT ; ++i) | |
{ | |
printf("%2d",porl[j][i]); | |
} | |
printf("\n"); | |
} | |
} | |
void move(int (*porl)[TARGET_COUNT],int from,int dest) | |
{ | |
int data = porl[from][0]; | |
int i; | |
//from shift | |
for(i = 0 ; i < TARGET_COUNT - 1 ; ++i) | |
porl[from][i] = porl[from][i + 1]; | |
porl[from][TARGET_COUNT - 1] = 0; | |
//dest shift | |
for(i = TARGET_COUNT - 1 ; i > 0 ; --i) | |
porl[dest][i] = porl[dest][i - 1]; | |
porl[dest][0] = data; | |
} | |
void hanoi(int (*porl)[TARGET_COUNT],int* point,int from,int dest,int swap,int count) | |
{ | |
//other up target moving to swap from from (dest is swap) | |
if(count > 1) hanoi(porl,point,from,swap,dest,count - 1); | |
//moving | |
printf("#%d %d -> %d\n",++*point,from,dest); | |
move(porl,from,dest); | |
print_hanoi(porl); | |
//other targets moveing to dest from swap (from is swap) | |
if(count > 1) hanoi(porl,point,swap,dest,from,count - 1); | |
} | |
int main() | |
{ | |
int porl[PORL_COUNT][TARGET_COUNT]; | |
int point = 0,i,j;//point;move count | |
//init | |
for(j = 0 ; j < PORL_COUNT ; ++j) | |
for(i = 0 ; i < TARGET_COUNT ; ++i) | |
porl[j][i] = 0; | |
//default targets | |
for(i = 0 ; i < TARGET_COUNT ; ++i) | |
porl[0][i] = i + 1; | |
//from 0 | |
//swap 1 | |
//dest 2 | |
print_hanoi(porl); | |
hanoi(porl,&point,0,2,1,TARGET_COUNT); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 : 1 2 3 4 5 | |
1 : 0 0 0 0 0 | |
2 : 0 0 0 0 0 | |
#1 0 -> 2 | |
0 : 2 3 4 5 0 | |
1 : 0 0 0 0 0 | |
2 : 1 0 0 0 0 | |
#2 0 -> 1 | |
0 : 3 4 5 0 0 | |
1 : 2 0 0 0 0 | |
2 : 1 0 0 0 0 | |
#3 2 -> 1 | |
0 : 3 4 5 0 0 | |
1 : 1 2 0 0 0 | |
2 : 0 0 0 0 0 | |
#4 0 -> 2 | |
0 : 4 5 0 0 0 | |
1 : 1 2 0 0 0 | |
2 : 3 0 0 0 0 | |
#5 1 -> 0 | |
0 : 1 4 5 0 0 | |
1 : 2 0 0 0 0 | |
2 : 3 0 0 0 0 | |
#6 1 -> 2 | |
0 : 1 4 5 0 0 | |
1 : 0 0 0 0 0 | |
2 : 2 3 0 0 0 | |
#7 0 -> 2 | |
0 : 4 5 0 0 0 | |
1 : 0 0 0 0 0 | |
2 : 1 2 3 0 0 | |
#8 0 -> 1 | |
0 : 5 0 0 0 0 | |
1 : 4 0 0 0 0 | |
2 : 1 2 3 0 0 | |
#9 2 -> 1 | |
0 : 5 0 0 0 0 | |
1 : 1 4 0 0 0 | |
2 : 2 3 0 0 0 | |
#10 2 -> 0 | |
0 : 2 5 0 0 0 | |
1 : 1 4 0 0 0 | |
2 : 3 0 0 0 0 | |
#11 1 -> 0 | |
0 : 1 2 5 0 0 | |
1 : 4 0 0 0 0 | |
2 : 3 0 0 0 0 | |
#12 2 -> 1 | |
0 : 1 2 5 0 0 | |
1 : 3 4 0 0 0 | |
2 : 0 0 0 0 0 | |
#13 0 -> 2 | |
0 : 2 5 0 0 0 | |
1 : 3 4 0 0 0 | |
2 : 1 0 0 0 0 | |
#14 0 -> 1 | |
0 : 5 0 0 0 0 | |
1 : 2 3 4 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0 0 | |
2 : 2 3 4 5 0 | |
#31 0 -> 2 | |
0 : 0 0 0 0 0 | |
1 : 0 0 0 0 0 | |
2 : 1 2 3 4 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment