c_汉诺塔真相
来源:互联网 发布:ckplayer视频解析源码 编辑:程序博客网 时间:2024/06/10 18:30
输出图形的汉诺塔,蛮有意思。
其中的list.h是我从linux中提取的,参见:c_提取linux链表
#include "stdio.h"#include "stdlib.h"#include "list.h"////////////////////////int high;int interval=5;LIST_HEAD(head1);LIST_HEAD(head2);LIST_HEAD(head3);////////////////////////////#define DEBUGstruct Floor {int size;struct list_head list;};static inline int ListLen(struct list_head *head){int i = 0;struct list_head *plist;list_for_each(plist, head) {i++;}return i;}void PrintHanoi(int high, int interval,struct list_head *head1,struct list_head *head2,struct list_head *head3){int tower1 = ListLen(head1);int tower2 = ListLen(head2);int tower3 = ListLen(head3);struct Floor *floor;int size,tmpSize;int i,j;for(i=0; i<high; i++){if(i < high-tower1){size=0;}else{size = list_empty(head1)?0:(floor = list_entry(head1->prev, struct Floor, list))->size;#ifdef DEBUGprintf("size1:%d\n",size);#endifhead1=head1->prev;}for(j=0; j<high-size; j++){printf(" ");}for(j=0; j<size; j++){printf("* ");}if(i < high-tower2){tmpSize=0;}else{tmpSize = list_empty(head2)?0:(floor=list_entry(head2->prev, struct Floor, list))->size;#ifdef DEBUGprintf("size2:%d\n",size);#endifhead2=head2->prev;}size += tmpSize;for(j=0; j<high*2+interval-size; j++){printf(" ");}for(j=0; j<tmpSize; j++){printf("* ");}size = tmpSize;if(i < high-tower3){tmpSize=0;}else{tmpSize = list_empty(head3)?0:(floor=list_entry(head3->prev, struct Floor, list))->size;#ifdef DEBUGprintf("size3:%d\n",size);#endifhead3=head3->prev;}size += tmpSize;for(j=0; j<high*2+interval-size; j++){printf(" ");}for(j=0; j<tmpSize; j++){printf("* ");}printf("\n");//while(1);}}void PrintHanoiCheckTower(char src, char dest){struct list_head *srcList, *destList;switch (src){case 'A':{srcList = &head1;break;}case 'B':{srcList = &head2;break;}case 'C':{srcList = &head3;break;}default:printf("error!\n");while(1);}switch (dest){case 'A':{destList = &head1;break;}case 'B':{destList = &head2;break;}case 'C':{destList = &head3;break;}default:printf("error!\n");while(1);}list_move_tail(srcList->prev, destList);PrintHanoi(high, interval, &head1, &head2, &head3);}void Hanoi(int floor,char towerA,char towerB,char towerC){if(1 == floor){printf("get %c to %c\n",towerA,towerC);printf("--------------------------------------->\n");PrintHanoiCheckTower(towerA, towerC);printf("--------------------------------------->\n");}else{Hanoi(floor-1,towerA,towerC,towerB);printf("get %c to %c\n",towerA,towerC);printf("--------------------------------------->\n");PrintHanoiCheckTower(towerA, towerC);printf("--------------------------------------->\n");Hanoi(floor-1,towerB,towerA,towerC);}}int main(){struct list_head *plist;struct Floor *floor;int i; printf("input the tower of hanoi:\n"); scanf("%d",&high);for(i=0; i<high; i++){floor = malloc(sizeof(struct Floor));if(NULL == floor){printf("malloc err! stop!!!--->>\n");}floor->size = high-i;list_add_tail(&floor->list, &head1);}#if 0list_for_each(plist, &head1) {floor = list_entry(plist, struct Floor, list);printf("size: %d\n",floor->size);}printf("*********************************************--->\n");list_move_tail(head1.prev, &head2);list_for_each(plist, &head1) {floor = list_entry(plist, struct Floor, list);printf("size: %d\n",floor->size);}printf("*********************************************--->\n");list_for_each(plist, &head2) {floor = list_entry(plist, struct Floor, list);printf("size: %d\n",floor->size);}printf("*********************************************--->\n");#endif//while(1);PrintHanoi(high, interval, &head1, &head2, &head3);Hanoi(high,'A','B','C');//Hanoi(high, &head1, &head2, &head3);return 0;}
- c_汉诺塔真相
- c_汉诺塔
- 真相
- C_技巧
- C_链表
- c_指针
- C_指针
- C_数据结构
- c_基本概念
- c_基本概念
- c_数组
- c_函数
- c_函数
- c_指针
- c_指针
- C_指针
- C_补码
- 真相与真相
- C++中的namespace
- 华硕tf201刷机教程和工具
- asp.net开篇
- 带色彩恢复的多尺度视网膜增强算法(MSRCR)的原理、实现及应用。
- C++学习笔记(四)
- c_汉诺塔真相
- Yii学习(8)----with中使用多重关系调用
- linux软中断的实现
- 我在npm install express时发生如下错误
- java 文件的重命名
- 第十章 面向对象 简答题5
- 题目1015:还是A+B
- c/c++面试题
- 读《妈妈再打我一次》有感!真是超无聊!