Skip to content

Commit b34e028

Browse files
committed
Lv3_하노이의탑
1 parent 669d656 commit b34e028

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <string>
2+
#include <vector>
3+
#include <stack>
4+
using namespace std;
5+
6+
vector<vector<int>> answer102;
7+
void hanoi(int n, int from, int by, int to) // 1.재귀함수 이용
8+
{
9+
if (n == 1)
10+
{
11+
vector<int> v;
12+
v.push_back(from);
13+
v.push_back(to);
14+
answer102.push_back(v);
15+
return;
16+
}
17+
18+
hanoi(n - 1, from, to, by); // n-1 개의 원판을 1에서 3을 이용하여 2로 옮긴다.
19+
vector<int> v;
20+
v.push_back(from);
21+
v.push_back(to);
22+
answer102.push_back(v);
23+
hanoi(n - 1, by, from, to); // n-1개의 원판을 2에서 1을 이용하여 3으로 옮긴다.
24+
}
25+
26+
vector<vector<int>> solution102(int n) {
27+
//hanoi(n, 1, 2, 3);
28+
stack<int> s; // 2. 비재귀
29+
int from = 1;
30+
int by = 2;
31+
int to = 3;
32+
while (1){
33+
while (n > 1){
34+
s.push(to); // 인자리스트 푸쉬
35+
s.push(by);
36+
s.push(from);
37+
s.push(n);
38+
n--; // 인자리스트 변경 1
39+
s.push(to); // to 와 by를 교환하기 위해 임시로 저장
40+
to = by;
41+
by = s.top();
42+
s.pop();
43+
}
44+
45+
vector<int> v;
46+
v.push_back(from);
47+
v.push_back(to);
48+
answer102.push_back(v);
49+
50+
if (!s.empty()){
51+
n = s.top();
52+
s.pop();
53+
from = s.top();
54+
s.pop();
55+
by = s.top();
56+
s.pop();
57+
to = s.top();
58+
s.pop();
59+
60+
vector<int> v;
61+
v.push_back(from);
62+
v.push_back(to);
63+
answer102.push_back(v);
64+
65+
n--; // 인자리스트 변경 2
66+
s.push(from);
67+
from = by;
68+
by = s.top();
69+
s.pop();
70+
}
71+
else{
72+
break;
73+
}
74+
}
75+
76+
return answer102;
77+
}

Programmers/Programmers.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<ClCompile Include="Lv3\Lv3_등굣길.cpp" />
105105
<ClCompile Include="Lv3\Lv3_디스크컨트롤러.cpp" />
106106
<ClCompile Include="Lv3\Lv3_방문길이.cpp" />
107+
<ClCompile Include="Lv3\Lv3_배달.cpp" />
107108
<ClCompile Include="Lv3\Lv3_베스트앨범.cpp" />
108109
<ClCompile Include="Lv3\Lv3_보행자천국.cpp" />
109110
<ClCompile Include="Lv3\Lv3_브라이언의고민.cpp" />
@@ -115,9 +116,11 @@
115116
<ClCompile Include="Lv3\Lv3_입국심사.cpp" />
116117
<ClCompile Include="Lv3\Lv3_저울.cpp" />
117118
<ClCompile Include="Lv3\Lv3_정수삼각형.cpp" />
119+
<ClCompile Include="Lv3\Lv3_줄서는방법.cpp" />
118120
<ClCompile Include="Lv3\Lv3_짝지어제거하기.cpp" />
119121
<ClCompile Include="Lv3\Lv3_카카오프렌즈컬러링북.cpp" />
120122
<ClCompile Include="Lv3\Lv3_타일장식물.cpp" />
123+
<ClCompile Include="Lv3\Lv3_하노이의탑.cpp" />
121124
<ClCompile Include="Lv4\Lv4_카드게임.cpp" />
122125
<ClCompile Include="실전모의고사2회\배열회전.cpp" />
123126
<ClCompile Include="실전모의고사2회\가로등.cpp" />

Programmers/Programmers.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,14 @@
324324
<ClCompile Include="Lv3\Lv3_방문길이.cpp">
325325
<Filter>소스 파일</Filter>
326326
</ClCompile>
327+
<ClCompile Include="Lv3\Lv3_줄서는방법.cpp">
328+
<Filter>소스 파일</Filter>
329+
</ClCompile>
330+
<ClCompile Include="Lv3\Lv3_하노이의탑.cpp">
331+
<Filter>소스 파일</Filter>
332+
</ClCompile>
333+
<ClCompile Include="Lv3\Lv3_배달.cpp">
334+
<Filter>소스 파일</Filter>
335+
</ClCompile>
327336
</ItemGroup>
328337
</Project>

0 commit comments

Comments
 (0)