Skip to content

Commit ca0442a

Browse files
authored
명환 책, 백준 숙제 (#56)
* 명환 책 (그리디) 숙제 * 명환 백준 숙제
1 parent 10d5feb commit ca0442a

File tree

5 files changed

+262
-0
lines changed

5 files changed

+262
-0
lines changed

mhkim/baekjoon/6603.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @file 6603.cpp
3+
* @brief 로또
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* 조합(combination)
7+
* kC6 k개 중에서 6개 뽑기
8+
* DFS?
9+
*/
10+
#include <iostream>
11+
#include <vector>
12+
13+
using namespace std;
14+
15+
#define r 6
16+
17+
int k;
18+
int s[13];
19+
int arr[13];
20+
21+
void comb(int start, int depth)
22+
{
23+
if (depth == r)
24+
{
25+
for (int i = 0; i < r; i++)
26+
{
27+
cout << arr[i] << ' ';
28+
}
29+
cout << '\n';
30+
return;
31+
}
32+
33+
for (int i = start; i < k; i++)
34+
{
35+
arr[depth] = s[i];
36+
comb(i + 1, depth + 1);
37+
}
38+
}
39+
40+
int main()
41+
{
42+
ios_base::sync_with_stdio(false);
43+
cin.tie(nullptr);
44+
45+
while (1)
46+
{
47+
cin >> k;
48+
if (k == 0)
49+
{
50+
break;
51+
}
52+
53+
for (int i = 0; i < k; i++)
54+
{
55+
cin >> s[i];
56+
}
57+
58+
comb(0, 0);
59+
60+
cout << '\n';
61+
}
62+
63+
return 0;
64+
}

mhkim/baekjoon/7562.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @file 7562.cpp
3+
* @brief 나이트의 이동
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* bfs
7+
*
8+
* !! memset - #include <cstring>
9+
*/
10+
#include <iostream>
11+
#include <deque>
12+
#include <utility>
13+
#include <cstring>
14+
15+
using namespace std;
16+
17+
bool chess[300][300];
18+
int l;
19+
pair<int, int> moves[8] = {{-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}};
20+
21+
int bfs(pair<int, int> &start, pair<int, int> &end)
22+
{
23+
deque<pair<pair<int, int>, int>> q;
24+
int step = 0;
25+
q.push_back(make_pair(start, step));
26+
chess[start.first][start.second] = true;
27+
28+
int count = 0;
29+
while (!q.empty())
30+
{
31+
pair<pair<int, int>, int> qNow = q.front();
32+
pair<int, int> now = qNow.first;
33+
count = qNow.second;
34+
q.pop_front();
35+
36+
if (now.first == end.first && now.second == end.second)
37+
{
38+
break;
39+
}
40+
41+
count += 1;
42+
for (int i = 0; i < 8; i++)
43+
{
44+
if (chess[now.first + moves[i].first][now.second + moves[i].second] != true)
45+
{
46+
if (now.first + moves[i].first > l - 1 || now.first + moves[i].first < 0 || now.second + moves[i].second > l - 1 || now.second + moves[i].second < 0)
47+
{
48+
continue;
49+
}
50+
q.push_back(make_pair(make_pair(now.first + moves[i].first, now.second + moves[i].second), count));
51+
chess[now.first + moves[i].first][now.second + moves[i].second] = true;
52+
}
53+
}
54+
}
55+
56+
return count;
57+
}
58+
59+
int main()
60+
{
61+
ios_base::sync_with_stdio(false);
62+
cin.tie(nullptr);
63+
64+
int t;
65+
cin >> t;
66+
while (t--)
67+
{
68+
cin >> l;
69+
int fromX, fromY;
70+
cin >> fromX >> fromY;
71+
pair<int, int> from = {fromX, fromY};
72+
int toX, toY;
73+
cin >> toX >> toY;
74+
pair<int, int> to = {toX, toY};
75+
76+
cout << bfs(from, to) << '\n';
77+
memset(chess, false, sizeof(chess));
78+
}
79+
80+
return 0;
81+
}

mhkim/greedy-1.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file greedy-1.cpp
3+
* @brief 모험가 길드
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* sort
7+
*
8+
*/
9+
#include <iostream>
10+
#include <algorithm>
11+
12+
using namespace std;
13+
14+
int mo[100001];
15+
16+
int main()
17+
{
18+
ios_base::sync_with_stdio(false);
19+
cin.tie(nullptr);
20+
21+
int n;
22+
cin >> n;
23+
24+
for (int i = 0; i < n; i++)
25+
{
26+
cin >> mo[i];
27+
}
28+
29+
sort(mo, mo + n);
30+
31+
int nGroup = 0;
32+
int nMo = 0; // 현재 그룹에 포함된 모험가의 수
33+
for (int i = 0; i < n; i++)
34+
{
35+
nMo++;
36+
if (nMo >= mo[i])
37+
{
38+
nGroup++;
39+
nMo = 0;
40+
}
41+
}
42+
43+
cout << nGroup << '\n';
44+
45+
return 0;
46+
}

mhkim/greedy-2.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @file greedy-2.cpp
3+
* @brief 곱하기 혹은 더하기
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*/
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
int main()
11+
{
12+
ios_base::sync_with_stdio(false);
13+
cin.tie(nullptr);
14+
15+
string s;
16+
cin >> s;
17+
18+
int sum = 0;
19+
for (int i = 0; i < s.length(); i++)
20+
{
21+
int n = s[i] - '0';
22+
int hap = sum + n;
23+
int gop = sum * n;
24+
if (hap < gop)
25+
{
26+
sum = gop;
27+
}
28+
else
29+
{
30+
sum = hap;
31+
}
32+
}
33+
34+
cout << sum << '\n';
35+
36+
return 0;
37+
}

mhkim/greedy-3.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file greedy-3.cpp
3+
* @brief 문자열 뒤집기
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*/
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
int main()
11+
{
12+
ios_base::sync_with_stdio(false);
13+
cin.tie(nullptr);
14+
15+
string s;
16+
cin >> s;
17+
18+
int one2zero, zero2one = 0;
19+
for (int i = 1; i < s.length(); i++)
20+
{
21+
if (s[i - 1] == '0' && s[i - 1] != s[i])
22+
{
23+
zero2one++;
24+
}
25+
else if (s[i - 1] == '1' && s[i - 1] != s[i])
26+
{
27+
one2zero++;
28+
}
29+
}
30+
31+
cout << min(zero2one, one2zero) << '\n';
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)