JonsenElizee

Software Developing Blog

"An idea is fragile . It can be killed by a scornful smile or a yawn .It can be mound down by irony and scared to death by a cold look."
"Most cultures throughout human history have not liked creative individuals .They ignore them or kill them.It is a very efficient way of stopping creativity."

------Advertising boss Charles Browe and Howard Gardner ,professor at Harvard

   :: 首页 :: 新随笔 ::  ::  :: 管理 ::
Here is my simple implementation for hanoi issue.
non-recursive implementation is given also.

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <stdext.h>
  4 #define N 2
  5 
  6 struct _tow
  7 {
  8     char nam;   // name
  9     int top;    // index of the top layer
 10     int lay[N]; // lay[i-1] < lay[i] < lay[i+1]
 11 };
 12 typedef struct _tow tow;
 13 
 14 static int cnt = 0// move-count
 15 /*
 16  * move num layers from a to c
 17  */
 18 void hanoi_r(int num, tow* a, tow* b, tow* c)
 19 {
 20     if(num == 1)
 21     {
 22         /* move 1 layer is a easy thing */
 23         int i = a->lay[a->top];
 24         c->top ++;;
 25         c->lay[c->top] = a->lay[a->top];
 26         a->lay[a->top] = 0;
 27         a->top --;
 28         printf("move %d from %c to %c\n", i, a->nam, c->nam);
 29         cnt++;
 30     }
 31     else
 32     {
 33         hanoi_r(num - 1, a, c, b); // move num-1 layers from tow-a to tower-b
 34         hanoi_r(      1, a, b, c); // move     1 layers from tow-a to tower-c
 35         hanoi_r(num - 1, b, a, c); // move num-1 layers from tow-b to tower-c
 36     }
 37 }
 38 
 39 /*
 40  * struct used to record the mid-state of nonrecursive metod
 41  */
 42 struct _state
 43 {
 44     int num;
 45     tow* frm; // move layers from this tower
 46     tow* hel; // for temp place to put layers
 47     tow* aim; // target tower accepting num layers
 48 
 49 };
 50 typedef struct _state state;
 51 
 52 /*
 53  * move num layers from tower-a to tower-c with help of tower-b
 54  */
 55 void hanoi(int num, tow* a, tow* b, tow* c)
 56 {
 57     cnt = 0;
 58     pstack* stc = pstack_create();
 59     /* set initial state */
 60     state sta; 
 61     sta.num = num; 
 62     sta.frm = a; 
 63     sta.aim = c; 
 64     sta.hel = b; 
 65     /* stack initial state */
 66     pstack_push(&stc, &sta);
 67 
 68     /* keep moving when stack is not empty */
 69     while(pstack_hasmore(stc))
 70     {
 71         /* pop up top state in stack */
 72         state s = *(state*)pstack_pop(&stc);
 73         /* move 1 layer when num is 1 */
 74         if(s.num == 1) {
 75             int i = s.frm->lay[s.frm->top];
 76             s.aim->top ++;
 77             s.aim->lay[s.aim->top] = s.frm->lay[s.frm->top];
 78             s.frm->lay[s.frm->top] = 0;
 79             s.frm->top --;
 80             /* print out message */
 81             printf("move %d from %c to %c\n", i, s.frm->nam, s.aim->nam);
 82             cnt++;
 83         }
 84         else {
 85             /* 
 86              * stack action to execute for further processing and 
 87              * pay attention to the sequence of stacking. FILO make
 88              * we to push s3 first and to push s1 at last.
 89              */
 90             state s3; 
 91             s3.num = num -1
 92             s3.frm = s.hel; 
 93             s3.aim = s.aim; 
 94             s3.hel = s.frm; 
 95             pstack_push(&stc, &s3);
 96             state s2; s2.num = 1;      
 97             s2.frm = s.frm; 
 98             s2.aim = s.aim; 
 99             s2.hel = s.hel; 
100             pstack_push(&stc, &s2);
101             state s1; 
102             s1.num = num -1
103             s1.frm = s.frm; 
104             s1.aim = s.hel; 
105             s1.hel = s.aim; 
106             pstack_push(&stc, &s1);
107         }
108     }
109 
110     /* remember to free all resources */
111     pstack_free(&stc);
112 }
113 
114 int main()
115 {
116     tow a, b, c;
117     a.nam = 'a'; b.nam = 'b'; c.nam = 'c';
118     int i;
119     for(i = N; i > 0; i--
120     {
121         a.lay[N-i] = i; b.lay[N-i] = 0; c.lay[N-i] = 0;
122     }
123     a.top = N-1; b.top = -1; c.top = -1;
124     /*
125      * a, b and c is initialized one time, so, one method could be
126      * called at one time.
127      *
128     hanoi_r(N, &a, &b, &c);
129     printf("hanoi_r: mvoe %d times for %d layers\n", cnt, N);
130     */
131 
132     hanoi(N, &a, &b, &c);
133     printf("hanoi  : mvoe %d times for %d layers\n", cnt, N);
134 
135     return 0;
136 }
137 


posted on 2010-10-09 23:24 JonsenElizee 阅读(267) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


By JonsenElizee