Skip to content

Commit 2b5d143

Browse files
committed
update
1 parent 6ee9ed9 commit 2b5d143

File tree

7 files changed

+110
-30
lines changed

7 files changed

+110
-30
lines changed

lecture1/lecture1/ex063.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ void ex063() {
1818
{4, 2, 2},
1919
{4, 5, 5},
2020
{6, 4, 5} };
21-
vector<vector<int>> outputs(n, vector<int>(n, 0));
21+
vector<vector<int>> map(n, vector<int>(n, 0));
2222

2323
for (int i = 0; i < inputs.size(); i++) {
24-
outputs[inputs[i][0] - 1][inputs[i][1] - 1] = inputs[i][2];
24+
map[inputs[i][0] - 1][inputs[i][1] - 1] = inputs[i][2];
2525
}
2626

27-
for (int i = 0; i < outputs.size(); i++) {
28-
for (int j = 0; j < outputs[0].size(); j++) {
29-
cout << outputs[i][j] << " ";
27+
for (int i = 0; i < map.size(); i++) {
28+
for (int j = 0; j < map[0].size(); j++) {
29+
cout << map[i][j] << " ";
3030
}cout << endl;
3131
}
3232
}

lecture1/lecture1/ex064.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
#include <iostream>
22
#include <vector>
3-
#include <string>
4-
#include <stack>
53
using namespace std;
64

7-
// °æ·Î Ž»ö(DFS)
5+
// 경로 탐색(DFS), 완전 탐색
86

9-
static vector<int> ch(30);
7+
static vector<int> check(30);
108
static int cnt = 0;
119

1210
static void dfs(int v, int n, vector<vector<int>>& map) {
13-
if (v == n - 1) {
11+
if (v == n - 1) {// 도착 지점에 왔느냐?
1412
cnt++;
1513
}
1614
else {
1715
for (int i = 0; i < n; i++) {
18-
if (map[v][i] == 1 && ch[i] == 0) {
19-
ch[i] = 1;
16+
if (map[v][i] == 1 && check[i] == 0) {
17+
check[i] = 1;
2018
dfs(i, n, map);
21-
ch[i] = 0;
19+
check[i] = 0;
2220
}
2321
}
2422
}
2523
}
2624

2725
void ex064() {
2826
int n = 5;
29-
int v = 9;
30-
vector<vector<int>> inputs{ {1, 2}, {1, 3}, {1, 4}, {2, 1}, {2, 3}, {2, 5}, {3, 4}, {4, 2}, {4, 5} };
27+
vector<vector<int>> inputs{ {1, 2},
28+
{1, 3},
29+
{1, 4},
30+
{2, 1},
31+
{2, 3},
32+
{2, 5},
33+
{3, 4},
34+
{4, 2},
35+
{4, 5} };
3136
vector<vector<int>> map(n, vector<int>(n, 0));
3237

3338
for (int i = 0; i < inputs.size(); i++) {
3439
map[inputs[i][0] - 1][inputs[i][1] - 1] = 1;
3540
}
3641

37-
ch[0] = 1;
42+
check[0] = 1;
3843
dfs(0, n, map);
3944
cout << cnt << endl;
4045
}

lecture1/lecture1/ex066.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using namespace std;
66

77
// 경로 탐색(DFS : 인접리스트 방법)
8+
// 인접 행렬에 비해 메모리 사용량이 적다
89
static int cnt = 0;
910
static const int n = 5;
1011
static vector<int> ch(n + 1);
@@ -27,7 +28,15 @@ void dfs(int v) {
2728

2829
void ex066() {
2930
int v = 9;
30-
vector<vector<int>> inputs{ {1, 2}, {1, 3}, {1, 4}, {2, 1}, {2, 3}, {2, 5}, {3, 4}, {4, 2}, {4, 5} };
31+
vector<vector<int>> inputs{ {1, 2},
32+
{1, 3},
33+
{1, 4},
34+
{2, 1},
35+
{2, 3},
36+
{2, 5},
37+
{3, 4},
38+
{4, 2},
39+
{4, 5} };
3140
for (auto itr = inputs.begin(); itr != inputs.end(); ++itr) {
3241
map[((*itr)[0])].push_back((*itr)[1]);
3342
}
@@ -36,6 +45,7 @@ void ex066() {
3645
cout << map[i][j] << " ";
3746
}cout << endl;
3847
}
48+
3949
ch[1] = 1;
4050
dfs(1);
4151
cout << cnt << endl;

lecture1/lecture1/ex067.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
#include <iostream>
22
#include <vector>
3-
#include <string>
4-
#include <stack>
3+
54
using namespace std;
65

7-
// 경로 탐색(DFS : 인접리스트 방법)
6+
// 최소 비용 탐색(DFS : 인접행렬 방법, 가중치 방향 그래프)
87
static int cost = 2147000000;
98
static const int n = 5;
10-
static vector<int> ch(n+1);
11-
static vector<vector<int>> map(n+1 , vector<int>(n+1, 0));
9+
static vector<int> check(n + 1);
10+
static vector<vector<int>> map(n + 1, vector<int>(n + 1, 0));
1211

13-
static void dfs(int v, int sum) {
12+
static void dfs(int v, int sum /* 비용값 */) {
1413
if (v == 5) {
15-
if (cost > sum)
16-
cost = sum;
14+
if (cost > sum) // 지금까지 구한 비용중 최소 비용이면
15+
cost = sum; // 현재 구한 최소비용으로 업데이트
1716
}
1817
else {
1918
for (int i = 1; i < map[v].size(); i++) {
20-
if (ch[i] == 0 && map[v][i] != 0) {
21-
ch[i] = 1;
19+
if (check[i] == 0 && map[v][i] != 0) {
20+
check[i] = 1;
2221
dfs(i, sum + map[v][i]);
23-
ch[i] = 0;
22+
check[i] = 0;
2423
}
2524
}
2625
}
2726
}
2827

2928
void ex067() {
30-
vector<vector<int>> inputs{ {1, 2, 12}, {1, 3, 6}, {1, 4, 10}, {2, 3, 2}, {2, 5, 2}, {3, 4, 3}, {4, 2, 2}, {4, 5, 5} };
29+
vector<vector<int>> inputs{ {1, 2, 12},
30+
{1, 3, 6},
31+
{1, 4, 10},
32+
{2, 3, 2},
33+
{2, 5, 2},
34+
{3, 4, 3},
35+
{4, 2, 2},
36+
{4, 5, 5} };
37+
3138
for (int i = 0; i < inputs.size(); i++) {
3239
map[inputs[i][0]][inputs[i][1]] = inputs[i][2];
3340
}
34-
ch[1] = 1;
41+
42+
check[1] = 1;
3543
dfs(1, 0);
3644
cout << cost << endl;
3745
}

lecture1/lecture1/ex068.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
// 최소 비용 탐색(DFS : 인접리스트 방법, 가중치 방향 그래프)
7+
static int cost = 2147000000;
8+
static const int n = 5;
9+
static vector<int> check(n + 1);
10+
static vector<vector<pair<int, int> >> map(n + 1);
11+
12+
static void dfs(int v, int sum /* 비용값 */) {
13+
if (v == 5) {
14+
if (cost > sum) // 지금까지 구한 비용중 최소 비용이면
15+
cost = sum; // 현재 구한 최소비용으로 업데이트
16+
}
17+
else {
18+
for (int i = 0; i < map[v].size(); i++) {
19+
int next_node = map[v][i].first;
20+
int weight = map[v][i].second;
21+
if (check[next_node] == 0) {
22+
check[next_node] = 1;
23+
dfs(next_node, sum + weight);
24+
check[next_node] = 0;
25+
}
26+
}
27+
}
28+
}
29+
30+
void ex068() {
31+
vector<vector<int>> inputs{ {1, 2, 12},
32+
{1, 3, 6},
33+
{1, 4, 10},
34+
{2, 3, 2},
35+
{2, 5, 2},
36+
{3, 4, 3},
37+
{4, 2, 2},
38+
{4, 5, 5} };
39+
40+
for (int i = 0; i < inputs.size(); i++) {
41+
map[inputs[i][0]].push_back(make_pair(inputs[i][1], inputs[i][2]));
42+
}
43+
44+
check[1] = 1;
45+
dfs(1, 0);
46+
cout << cost << endl;
47+
}
48+
49+
int main() {
50+
ex068();
51+
cout << endl << "done!" << endl;
52+
}
53+
// 13

lecture1/lecture1/lecture1.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<ClCompile Include="ex065.cpp" />
191191
<ClCompile Include="ex066.cpp" />
192192
<ClCompile Include="ex067.cpp" />
193+
<ClCompile Include="ex068.cpp" />
193194
</ItemGroup>
194195
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
195196
<ImportGroup Label="ExtensionTargets">

lecture1/lecture1/lecture1.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,8 @@
216216
<ClCompile Include="ex067.cpp">
217217
<Filter>소스 파일</Filter>
218218
</ClCompile>
219+
<ClCompile Include="ex068.cpp">
220+
<Filter>소스 파일</Filter>
221+
</ClCompile>
219222
</ItemGroup>
220223
</Project>

0 commit comments

Comments
 (0)