본문 바로가기
알고리즘-자료구조

[개탱][C언어][하노이탑][간단한 알고리즘][재귀][Stack][스택][그래픽화][그래픽][도형]

by 개탱 2017. 12. 21.
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <cmath>
#include <list>
 
using namespace std;
 
list<pair<int,int>> a;
list<pair<int,int>> b;
list<pair<int,int>> c;
int iCount;
int n;
void hanoi(int n, char from, char tmp, char to);
int iNum;
void print();
 
int main() {
    // '1', '2', '3'
    scanf("%d",&n);
 
    
    printf("┌──────────────┐\n");
    printf("│%2d개의 하노이탑을 진행합니다│\n",n);
    printf("├──────────────┤\n");
   for(int i = 0; i < n; i++)
   {
      a.push_back(make_pair(i+1,n-i));
   }
    
    printf("│\t 총%6d번 이동      │\n",(int)pow(2,n)-1);
    printf("└──────────────┘\n\n\n");
    print();
    hanoi(n,'1','2','3');
}
void hanoi(int n, char from, char tmp, char to)
{
   int iTmp;
    if(n==1)
    {
      iCount++;
      switch(from)
      {
      case '1':
         iTmp = a.front().first;
         a.pop_front();
         break;
      case '2':
         iTmp = b.front().first;
         b.pop_front();
         break;
      case '3':
         iTmp = c.front().first;
         c.pop_front();
         break;
      }
      switch(to)
      {
      case '1':
         if(!a.empty())
         a.push_front(make_pair(iTmp,a.front().second+1));
         else
            a.push_front(make_pair(iTmp,1));
         break;
      case '2':
         if(!b.empty())
         b.push_front(make_pair(iTmp,b.front().second+1));
         else
            b.push_front(make_pair(iTmp,1));
         break;
      case '3':
         if(!c.empty())
         c.push_front(make_pair(iTmp,c.front().second+1));
         else
            c.push_front(make_pair(iTmp,1));
         break;
      }
      print();
        //printf("%c %c\n",from,to);
        iNum++;
      
        return;
      
    }
    hanoi(n-1,from,to,tmp);
    hanoi(1,from,tmp,to);
    hanoi(n-1,tmp,from,to);
   
}
 
 
void print()
{
 
    
    printf("┌─────┐\n");
    printf("│%5d번째 │\n",iCount+1);
    printf("└─────┘\n\n\n");
    
   list<pair<int,int>>::iterator iterA = a.begin();
   list<pair<int,int>>::iterator iterB = b.begin();
   list<pair<int,int>>::iterator iterC = c.begin();
   for(int i = n; i >= 1; i--)
   {
      if(!a.empty() && (*iterA).second == i)
      {
         for(int j = 0; j < (*iterA).first; j++)
         {
            printf("□");
         }
         for(int j = 0; j < n-((*iterA).first); j++)
         {
            printf(" ");
         }
         iterA++;
      }else
      {
         for(int j = 0; j < n; j++)
         {
            printf(" ");
         }
      }
         printf("\t\t\t");
      if(!b.empty() && (*iterB).second == i)
      {
         for(int j = 0; j < (*iterB).first; j++)
         {
            printf("□");
         }
         for(int j = 0; j < n-((*iterB).first); j++)
         {
            printf(" ");
         }
         iterB++;
      }else
      {
         for(int j = 0; j < n; j++)
         {
            printf(" ");
         }
      }
         printf("\t\t\t");
      if(!c.empty() && (*iterC).second == i)
      {
         for(int j = 0; j < (*iterC).first; j++)
         {
            printf("□");
         }
         for(int j = 0; j < n-((*iterC).first); j++)
         {
            printf(" ");
         }
         iterC++;
      }else
      {
         for(int j = 0; j < n; j++)
         {
            printf(" ");
         }
      }
      printf("\n");
   }
 
   for(int i = 0; i < 3; i++)
   {
      for(int j = 0; j < n; j++)
      {
         printf("■");
      }
      printf("\t\t\t");
   }
   printf("\n\n");
}
 
cs




댓글