From c4e4f601e608ea9ea6ce4fceb15303540a19fd49 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 02:20:15 +0900 Subject: [PATCH 01/24] dpfls0922 boj15953 --- WEEK3-B/dpfls0922/boj15953.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 WEEK3-B/dpfls0922/boj15953.cpp diff --git a/WEEK3-B/dpfls0922/boj15953.cpp b/WEEK3-B/dpfls0922/boj15953.cpp new file mode 100644 index 00000000..eb67190e --- /dev/null +++ b/WEEK3-B/dpfls0922/boj15953.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int main(){ + int tc, x, y, money1, money2, rank; + int ct1_reward[6] = {500, 300, 200, 50, 30, 10}; + int ct2_reward[5] = {512, 256, 128, 64, 32}; + + cin >> tc; + for(int i=0; i> x >> y; + for(int j=1; j<7; j++){ + rank += j; + if( x <= rank){ + money1 = ct1_reward[j-1]; + break; + } + } + rank = 0; + for(int j=1; j<6; j++){ + rank += pow(2, j-1); + if( y <= rank){ + money2 = ct2_reward[j-1]; + break; + } + } + cout << (money1+money2)*10000<<'\n'; + } + return 0; +} \ No newline at end of file From af39d350b679efc4f9e5d1874e8050d20f41b52b Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 02:20:30 +0900 Subject: [PATCH 02/24] dpfls0922 boj2941 --- WEEK3-B/dpfls0922/boj2941.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 WEEK3-B/dpfls0922/boj2941.cpp diff --git a/WEEK3-B/dpfls0922/boj2941.cpp b/WEEK3-B/dpfls0922/boj2941.cpp new file mode 100644 index 00000000..2025f4ae --- /dev/null +++ b/WEEK3-B/dpfls0922/boj2941.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() { + string str; + cin >> str; + string croatia[8] = {"c=", "c--", "dz=", "d=", "lj", "nj", "s=", "z="}; + for (int i = 0; i < 8; i++) { + while(true){ + int idx = str.find(croatia[i]); + if (idx == string::npos)break; + str.replace(idx, croatia[i].length(), "a"); + } + } + cout << str.length(); + return 0; +} \ No newline at end of file From f0c78659613bcd4564e34e69ea0e92896c7861bc Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 02:20:49 +0900 Subject: [PATCH 03/24] dpfls0922 boj11723 --- WEEK3-B/dpfls0922/boj11723.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 WEEK3-B/dpfls0922/boj11723.cpp diff --git a/WEEK3-B/dpfls0922/boj11723.cpp b/WEEK3-B/dpfls0922/boj11723.cpp new file mode 100644 index 00000000..e69de29b From b91c50dc8ad0f6fef6b41ce808030bd8d7c465b6 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 02:21:47 +0900 Subject: [PATCH 04/24] dpfls0922 boj5086 --- WEEK3-B/dpfls0922/boj5086.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 WEEK3-B/dpfls0922/boj5086.cpp diff --git a/WEEK3-B/dpfls0922/boj5086.cpp b/WEEK3-B/dpfls0922/boj5086.cpp new file mode 100644 index 00000000..7c2993cf --- /dev/null +++ b/WEEK3-B/dpfls0922/boj5086.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main(){ + int x, y; + while(1){ + cin >> x >> y; + if(x==0 && y==0) break; + if(x%y==0) cout << "multiple" << '\n'; + else if(y%x==0) cout << "factor" << '\n'; + else cout << "neither" << '\n'; + } + return 0; +} \ No newline at end of file From a28e5b82fc1c5b74e147fdfde82663de60d319b1 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 02:46:10 +0900 Subject: [PATCH 05/24] Create README.md --- WEEK3-B/dpfls0922/README.md | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 WEEK3-B/dpfls0922/README.md diff --git a/WEEK3-B/dpfls0922/README.md b/WEEK3-B/dpfls0922/README.md new file mode 100644 index 00000000..c4c09baa --- /dev/null +++ b/WEEK3-B/dpfls0922/README.md @@ -0,0 +1,102 @@ +> # WEEK3-자료구조 +#### 1. 스택 +#### 2. 큐 +#### 3. 리스트 +
+ +# 1️⃣ 스택 +* LIFO(Last-in First-out) 후입선출 규칙을 따르는 자료구조 +* 새로운 원소는 스택의 맨 위(top)에 추가되고 원소를 뺄 때는 맨 위(top)에 원소가 나오는 것 +* 즉, 입구와 출구가 하나인 경우 +* #include 을 하여 c++ stl 라이브러리를 사용함 + +## 연산 +* push(x) +
+ x라는 항목을 스택 맨 위에 삽입 +* pop() +
+ 스택 맨 위에 있는 항목을 삭제 +* empty() +
+ 스택이 비어있는지를 확인 후 true,false 값을 반환 +* size() +
+ 스택에 들어있는 원소의 개수를 반환 +* top() +
+ 스택 맨 위에 있는 항목을 리턴 + +# 2️⃣ 큐 +* FIFO(First-in First-out) 선입선출 규칙을 따르는 자료구조 +* 원소를 꺼낼 때는 맨 위에서 꺼내고 원소를 넣을 때는 맨 밑부터 넣는 것 +* 즉. 입구와 출구가 각각 있는 것 +* #include 을 하여 c++ stl라이브러리를 사용함 + +## 연산 +* push(x) +
+ x라는 항목을 큐의 맨 끝에 삽입 +* pop() +
+ 큐의 맨 앞에 있는 항목을 삭제 +* front() +
+ 큐의 맨 앞에 있는 항목을 리턴 +* back() +
+ 큐의 맨 뒤에 있는 항목을 리턴 +* empty(),size() +
+ 큐가 비어있는지 검사, 큐에 들어있는 원소의 개수 반환 + +# 3️⃣ 리스트 +* 시퀀스 컨테이너 (sequence container)의 일종으로 순서를 유지하는 자료구조 +* #include 하여 c++ stl라이브러리를 사용함 + +## 관련 함수 +#### 반복자 +* begin() +
+ beginning iterator 반환 +* end() +
+ end iterator 반환 +#### 삽입 +* push_front(element) +
+ 리스트 맨 앞에 원소 추가 +* push_back(element) +
+ 리스트 맨 뒤에 원소 추가 +* insert(iterator, element) +
+ iterator가 가리키는 부분의 앞에 원소 추가 +#### 삭제 +* pop_front() +
+ 리스트 맨 앞의 원소 삭제 +* pop_back() +
+ 리스트 맨 뒤의 원소 삭제 +* erase(iterator) +
+ iterator가 가리키는 부분의 원소 삭제 +#### 조회 +* ( * )iterator +
+ iterator가 가리키는 원소에 접근 +* front() +
+ 첫번째 원소 반환 +* back() +
+ 마지막 원소 반환 + +#### 기타 +* empty() +
+ 리스트가 비어있는지 여부 +* size() +
+ 리스트 사이즈 반환 From 96544374276d3999738376cb460b6c4365c519e5 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 10:05:46 +0900 Subject: [PATCH 06/24] dpfls0922 boj11723 --- WEEK3-B/dpfls0922/boj11723.cpp | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/WEEK3-B/dpfls0922/boj11723.cpp b/WEEK3-B/dpfls0922/boj11723.cpp index e69de29b..0e5c163c 100644 --- a/WEEK3-B/dpfls0922/boj11723.cpp +++ b/WEEK3-B/dpfls0922/boj11723.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +int main(){ + int tc, x; + string func; + int alp[21] = {0, }; + string function[6] = {"add", "remove", "check", "toggle", "all", "empty"}; + cin >> tc; + for(int i=0; i> func; + for(int j=0; j<6; j++){ + int idx = (func == function[i]); + switch(idx){ + case 1: // add + cin >> x; + alp[x] = 1; + break; + // x continue + case 2: // remove + cin >> x; + alp[x] = 0; + break;//// + case 3: // check + cin >> x; + int state = alp[x]==1 ? 1 : 0; + cout << state << '\n'; + case 4: // toggle + cin >> x; + alp[x]==1 ? alp[x]=0 : alp[x]=1; + case 5: // all + for(int i=0; i<21; i++) + alp[i]=1; + case 6: //empty + for(int i=0; i<21; i++) + alp[i]=1; + } + } + } + return 0; +} \ No newline at end of file From c996d9ecaddfc378394a41c12daf45a8cca76a74 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 26 Sep 2021 10:07:25 +0900 Subject: [PATCH 07/24] boj11723 --- WEEK3-B/dpfls0922/boj11723.cpp | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/WEEK3-B/dpfls0922/boj11723.cpp b/WEEK3-B/dpfls0922/boj11723.cpp index e69de29b..db3a75ec 100644 --- a/WEEK3-B/dpfls0922/boj11723.cpp +++ b/WEEK3-B/dpfls0922/boj11723.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +int main(){ + int tc, x; + string func; + int alp[21] = {0, }; + string function[6] = {"add", "remove", "check", "toggle", "all", "empty"}; + cin >> tc; + for(int i=0; i> func; + for(int j=0; j<6; j++){ + int idx = (func == function[i]); + switch(idx){ + case 1: // add + cin >> x; + alp[x] = 1; + break; + // x 있으면 continue + case 2: // remove + cin >> x; + alp[x] = 0; + break;//// + case 3: // check + cin >> x; + int state = alp[x]==1 ? 1 : 0; + cout << state << '\n'; + case 4: // toggle + cin >> x; + alp[x]==1 ? alp[x]=0 : alp[x]=1; + case 5: // all + for(int i=0; i<21; i++) + alp[i]=1; + case 6: //empty + for(int i=0; i<21; i++) + alp[i]=1; + } + } + } + return 0; +} From bc205a082deadd282f13f7d9abc964fa3d7c6e24 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sat, 2 Oct 2021 00:06:17 +0900 Subject: [PATCH 08/24] dpfls0922 boj10829 --- WEEK4-B/dpfls0922/boj10829.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 WEEK4-B/dpfls0922/boj10829.cpp diff --git a/WEEK4-B/dpfls0922/boj10829.cpp b/WEEK4-B/dpfls0922/boj10829.cpp new file mode 100644 index 00000000..cda4bb2f --- /dev/null +++ b/WEEK4-B/dpfls0922/boj10829.cpp @@ -0,0 +1,16 @@ +#include +#include +using namespace std; + +int main() { + long long n; + vector binary; + cin >> n; + while (n) { + binary.push_back(n % 2); + n /= 2; + } + for (int i = binary.size()-1; i >= 0; i--) { + cout << binary[i]; + } +} \ No newline at end of file From cb1942d113d248267124ecde6e4eed5ddd3be2cc Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sat, 2 Oct 2021 15:20:59 +0900 Subject: [PATCH 09/24] dpfls0922 boj13706 --- WEEK4-B/boj13706.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 WEEK4-B/boj13706.py diff --git a/WEEK4-B/boj13706.py b/WEEK4-B/boj13706.py new file mode 100644 index 00000000..12248548 --- /dev/null +++ b/WEEK4-B/boj13706.py @@ -0,0 +1,13 @@ +def binary_search(s, e): + target = e + while True: + mid = (s + e) // 2 + if (mid ** 2) == target: + return mid + if mid ** 2 > target: + e = mid + elif mid ** 2 < target: + s = mid + +N = int(input()) +print(binary_search(1, N)) \ No newline at end of file From dc93e9472721082b9d33b702855f9f480a5ac100 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sat, 2 Oct 2021 15:37:19 +0900 Subject: [PATCH 10/24] dpfls0922 boj1769 --- WEEK4-B/dpfls0922/boj1769.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 WEEK4-B/dpfls0922/boj1769.cpp diff --git a/WEEK4-B/dpfls0922/boj1769.cpp b/WEEK4-B/dpfls0922/boj1769.cpp new file mode 100644 index 00000000..d566f9e1 --- /dev/null +++ b/WEEK4-B/dpfls0922/boj1769.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +string changeNum(string n) { + int sum = 0; + for (int i = 0; i < n.length(); i++) + sum += n[i] - '0'; + return to_string(sum); +} + +int main() { + int cnt = 0; + string n; + cin >> n; + while (n.length()!=1) { + n = changeNum(n); + cnt++; + } + cout << cnt << '\n'; + if (n == "3" || n == "6" || n == "9") cout << "YES"; + else cout << "NO"; + return 0; +} \ No newline at end of file From 3e4f06930357a7a651ffafa0ee2051ce2fc4dc7e Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sat, 2 Oct 2021 17:05:10 +0900 Subject: [PATCH 11/24] dpfls0922 boj2630 --- WEEK4-B/dpfls0922/boj2630.cpp | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 WEEK4-B/dpfls0922/boj2630.cpp diff --git a/WEEK4-B/dpfls0922/boj2630.cpp b/WEEK4-B/dpfls0922/boj2630.cpp new file mode 100644 index 00000000..2d6fb24e --- /dev/null +++ b/WEEK4-B/dpfls0922/boj2630.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +int arr[128][128]; +int white, blue; + +void cutPaper(int x, int y, int size) { + if (size == 1){ + if (arr[x][y] == 0) white++; + else blue++; + return; + } + bool flag = true; + for (int i = x; i < x + size; i++) { + for (int j = y; j < y + size; j++) { + if (arr[x][y] != arr[i][j]) { + flag = false; + break; + } + } + } + if (flag) { + if (arr[x][y] == 0) white++; + else blue++; + return; + } + cutPaper(x, y, size / 2); + cutPaper(x + size / 2, y, size / 2); + cutPaper(x, y + size / 2, size / 2); + cutPaper(x + size / 2, y + size / 2, size / 2); +} + +int main() { + int n; + cin >> n; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> arr[i][j]; + + cutPaper(0, 0, n); + cout << white << '\n'; + cout << blue << '\n'; + + return 0; +} \ No newline at end of file From 5140c2b46b9859df2a43b7fed2033c845db237de Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sat, 2 Oct 2021 17:25:11 +0900 Subject: [PATCH 12/24] dpfls0922 boj1992 --- WEEK4-B/dpfls0922/boj1992.cpp | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 WEEK4-B/dpfls0922/boj1992.cpp diff --git a/WEEK4-B/dpfls0922/boj1992.cpp b/WEEK4-B/dpfls0922/boj1992.cpp new file mode 100644 index 00000000..04ad761f --- /dev/null +++ b/WEEK4-B/dpfls0922/boj1992.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +int arr[64][64]; + +void cutPaper(int x, int y, int size) { + if (size == 1) { + if (arr[x][y] == 0) { + cout << arr[x][y]; + return; + } + } + bool flag = true; + for (int i = x; i < x + size; i++) { + for (int j = y; j < y + size; j++) { + if (arr[x][y] != arr[i][j]) { + flag = false; + break; + } + } + } + if (flag) { + cout << arr[x][y]; + return; + } + cout << "("; + cutPaper(x, y, size / 2); + cutPaper(x, y + size / 2, size / 2); + cutPaper(x + size / 2, y, size / 2); + cutPaper(x + size / 2, y + size / 2, size / 2); + cout << ")"; +} + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + scanf("%1d", &arr[i][j]); + + cutPaper(0, 0, n); + + return 0; +} \ No newline at end of file From 511e6bce424cdb3f169e700563db8f7574684a0f Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 3 Oct 2021 03:22:17 +0900 Subject: [PATCH 13/24] README.md --- WEEK4-B/dpfls0922/README.md | 134 ++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 WEEK4-B/dpfls0922/README.md diff --git a/WEEK4-B/dpfls0922/README.md b/WEEK4-B/dpfls0922/README.md new file mode 100644 index 00000000..ec2419aa --- /dev/null +++ b/WEEK4-B/dpfls0922/README.md @@ -0,0 +1,134 @@ +> # WEEK4-분할정복 +#### 1. 재귀 +#### 2. 이분 탐색 +#### 3. 큰 수 연산 +
+ +# 1️⃣ 재퀴 +## 정의 +* 재귀(recursion)는 어떤 것을 정의할 때 자기 자신을 참조하는 것을 뜻함 +* 재귀 함수 : 재귀 호출(recursive call)을 사용하는 함수 +* 재귀 호출 : 어떤 함수가 내부에서 자기 자신을 다시 호출하는 것 +* 재귀 호출을 사용할 경우 언제 호출을 그만둘 것인지를 정의해야 함. 그렇지 않으면 무한루프처럼 계속 자기 자신을 호출하게 됨 + +## 장단점 +* 장점 +* * 일반적인 수학의 점화식을 그대로 표현가능한 능력을 가지고 있어 복잡한 문제를 간단하고 논리적으로 표현할 수 있음 +* * 변수의 사용을 줄일 수 있음 +* * 프랙탈, 분할 반복 등에서 엄청난 효율을 보여줌 + +* 단점 +* * 함수의 call이 반복되므로 stack의 메모리를 많이 사용하여 stack overflow의 위험이 큼 + + + +# 2️⃣ 이분 탐색 +## 정의 +* 오름차순으로 정렬된 리스트에서 특정한 값의 위치를 찾는 알고리즘 +* 오름차순으로 정렬된 리스트의 중간 값을 임의의 값으로 선택하여, 찾고자 하는 Key 값과 비교하는 동작을 반복함 +* 순차적으로 탐색하는 일반 탐색 기법과 달리, 이분 탐색은 탐색 대상을 절반씩 없애며 탐색함 +* 데이터의 양이 많아질 수록 이분 탐색이 효율적인 알고리즘이 됨 + +## 장단점 +* 장점 +* * 검색이 될때마다 선형탐색(Linear Search)와는 비교할 수 없게 빨라짐 +* 단점 +* * 정렬된 리스트에서만 사용할 수 있음 + +## 시간 복잡도 +* log2n +* O(log n) + +## 이진 탐색의 구현 +1) 반복문 +``` C++ +bool BinarySearch(int *arr, int len, int key){ + int start = 0; + int end = len-1; + int mid; + + while(end - start >= 0) { + mid = (start + end) / 2; + + if (arr[mid] == key) + return true; + + else if (arr[mid] > key) + end = mid - 1; + + else + start = mid + 1; + } + return false; +} +``` +2) 재귀 +``` C++ +bool BinarySearch(int *arr, int start, int end, int key) { + + if (start > end) return false; + + int mid = (start + end) / 2; + + if (arr[mid] == key) + return true; + + else if (arr[mid] > key) + return BinarySearch(arr, start, mid - 1, key); + + else + return BinarySearch(arr, mid + 1, end, key); + } +} +``` +3) STL +* 해더 파일안에 있는 binary_search 함수를 이용하면 됨 +``` C++ +template + bool binary_search (ForwardIterator first, ForwardIterator last, const T& val) +``` + +# 3️⃣ 큰 수 연산 + 자료형으로 담을 수 있는 범위를 벗어나는 경우 +1) 배열, 벡터 이용 +2) 문자열 string 이용 + ``` C++ +// 문자열 덧셈 +string add(string& a, string& b) { + string c(max(a.size(), b.size()), '0'); + int sum = 0; + for (int i = 0; i < c.size(); i++) { + if (i < a.size()) sum += a[a.size() - i - 1] - '0'; + if (i < b.size()) sum += b[b.size() - i - 1] - '0'; + c[c.size() - i - 1] = sum % 10 + '0'; + + sum /= 10; + } + if (sum) c.insert(c.begin(), '1'); + return c; +} +``` +``` C++ +// 문자열 곱셈 +string multiply(string& a, string& b) { + string c = "0"; + for (int i = 0; i < b.size(); i++) { + string line(a); + int carry = 0; + for (int j = a.size()-1; j >= 0; j--) { + int sum = carry; + carry = 0; + sum += (a[j] - '0') * (b[b.size() - i - 1] - '0'); + if (sum >= 10) { + carry = sum / 10; + sum %= 10; + } + line[j] = sum + '0'; + } + if (carry > 0) line.insert(line.begin(), carry + '0'); + line += string(i, '0'); + c = add(c, line); + } + return c; +} +``` From af223d1871c6a74ba4f58b75d9cb01aa09b0bc9b Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 3 Oct 2021 04:01:03 +0900 Subject: [PATCH 14/24] dpfls0922 boj1074 --- WEEK4-B/dpfls0922/boj1074.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 WEEK4-B/dpfls0922/boj1074.cpp diff --git a/WEEK4-B/dpfls0922/boj1074.cpp b/WEEK4-B/dpfls0922/boj1074.cpp new file mode 100644 index 00000000..a6321c24 --- /dev/null +++ b/WEEK4-B/dpfls0922/boj1074.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int n, row, col; +int ans = 0; + +void Z(int r, int c, int size) { + + if (r == row && c == col){ + cout << ans << '\n'; + return; + } + + if (row >= r && row < r + size && col >= c && col < c + size) { + Z(r, c, size / 2); + Z(r, c + size / 2, size / 2); + Z(r + size / 2, c, size / 2); + Z(r + size / 2, c + size / 2, size / 2); + } + else + ans += size * size; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin >> n >> row >> col; + Z(0, 0, (1 << n)); + + return 0; +} \ No newline at end of file From 521491b7ed7e72e9c22eed73cf32bf55160b26d7 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 14 Nov 2021 00:28:40 +0900 Subject: [PATCH 15/24] dpfls0922 boj9327 --- WEEK5-B/boj9327.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 WEEK5-B/boj9327.cpp diff --git a/WEEK5-B/boj9327.cpp b/WEEK5-B/boj9327.cpp new file mode 100644 index 00000000..ca0c4bf4 --- /dev/null +++ b/WEEK5-B/boj9327.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() { + int t; + cin >> t; + + while (t--) { + int n, m; + cin >> n >> m; + + int a, b; + for(int i=0; i> a >> b; + } + cout << n - 1 << endl; + } + +} \ No newline at end of file From 48071a9e8ec6d9c853cffdaf35fb296f6ec0418b Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 14 Nov 2021 01:18:44 +0900 Subject: [PATCH 16/24] dpfls0922 boj1260 --- WEEK5-B/boj1260.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 WEEK5-B/boj1260.cpp diff --git a/WEEK5-B/boj1260.cpp b/WEEK5-B/boj1260.cpp new file mode 100644 index 00000000..bf7f569d --- /dev/null +++ b/WEEK5-B/boj1260.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + +vector graph[10001]; +int dfs_visited[1001] = { 0, }; + +void dfs(int v) { + dfs_visited[v] = 1; + cout << v << ' '; + for (int i = 0; i < graph[v].size(); i++) { + int next = graph[v][i]; + if (!dfs_visited[next]) + dfs(next); + } +} + +int bfs_visited[1001] = { 0, }; +void bfs(int v) { + queue q; + bfs_visited[v] = 1; + q.push(v); + while (!q.empty()) { + v = q.front(); + q.pop(); + cout << v << ' '; + for (int i = 0; i < graph[v].size(); i++) { + int next = graph[v][i]; + if (!bfs_visited[next]) { + q.push(next); + bfs_visited[next] = 1; + } + } + } +} + +int main() { + int n, m, v; + int a, b; + + cin >> n >> m >> v; + while(m--){ + cin >> a >> b; + graph[a].push_back(b); + graph[b].push_back(a); + } + + for (int i = 1; i <= n; i++) + sort(graph[i].begin(), graph[i].end()); + + dfs(v); + cout << '\n'; + bfs(v); + return 0; +} From bf67ca235538270a93e3fa5407c023ef8486f1d7 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 14 Nov 2021 02:31:07 +0900 Subject: [PATCH 17/24] Create README.md --- WEEK5-B/README.md | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 WEEK5-B/README.md diff --git a/WEEK5-B/README.md b/WEEK5-B/README.md new file mode 100644 index 00000000..d4368353 --- /dev/null +++ b/WEEK5-B/README.md @@ -0,0 +1,84 @@ +> # WEEK5-그래프 +#### 1. 그래프 탐색 +#### 2. 깊이 우선 탐색 +#### 3. 너비 우선 탐색 +
+ +# 1️⃣ 그래프 탐색 +### 그래프 +* G = (V, E) // vertex, edge +* V는 vertex의 집합 (=node, point) +* E는 edge의 집합 (=arcs, lines) + + + +# 2️⃣ 깊이 우선 탐색 +* 현재 정점에서 갈 수 있는 점들까지 들어가면서 탐색 +* 수직 방향으로 탐색함 +* 스택 또는 재귀함수로 구현 + +### 알고리즘 + 1. 탐색 시작 노드를 스택에 삽입하고 방문처리 표시 + 2. 스택의 최상단 노드에 방문하지 않은 인접한 노드가 하나라도 있으면 그 노드를 스택에 넣고 방문처리 + 방문하지 않은 인접 노드가 없으면 스택에서 최상단 노드를 꺼냄 + 3. 2번의 과정을 수행할 수 없을 때까지 반복함 + +### 장단점 +* 장점 + - 현재 진행하고 있는 경로의 노드만을 기억하면 되므로 차지하는 저장공간이 비교적 작음 + - 목표노드가 깊은 단계에 있을 경우 해를 빨리 구할 수 있음 +* 단점 + - 얻어진 해가 최단 경로가 된다는 보장이 없음. 이는 목표에 이르는 경로가 다수인 문제에 대해 DFS는 해에 다다르면 탐색을 끝내버리므로, 이때 얻어진 해는 최적이 아닐 수도 있음 + +### C++ 코드 +```C++ +void dfs(int x) +{ + visited[x] = true; + cout << x << " "; + for (int i = 0; i < graph[x].size(); i++) + { + int y = graph[x][i]; + if (!visited[y]) + dfs(y); + } +} +``` +# 3️⃣ 너비 우선 탐색 +* 현재 정점에 연결된 가까운 점들부터 탐색 +* 수평 방향으로 탐색함 +* 큐를 이용해서 구현 + +### 알고리즘 +1. 탐색 시작 노드를 큐에 삽입하고 방문처리 표시 +2. 큐에서 노드를 꺼낸 뒤에 꺼내진 노드의 인접 노드 중 방문하지 않은 노드를 모두 큐에 삽입 후 방문 처리함 +3. 2번 반복 + +### 장단점 +* 장점 + - 출발노드에서 목표노드까지의 최단 길이 경로를 보장함 +* 단점 + - 경로가 매우 길 경우에는 탐색 가지가 급격히 많아져 보다 많은 기억 공간을 필요로 함 + +### C++ 코드 +```C++ +// BFS 함수 정의 +void bfs(int start) { + queue q; + q.push(start); + visited[start] = true; + + while (!q.empty()) { + int x = q.front(); + q.pop(); + cout << x << ' '; + for (int i = 0; i < graph[x].size(); i++) { + int y = graph[x][i]; + if (!visited[y]) { + q.push(y); + visited[y] = true; + } + } + } +} +``` From a5ebd493c43cab2e2f712688a0278834e3dd0bb0 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 02:27:28 +0900 Subject: [PATCH 18/24] dpfls0922 boj4690 --- WEEK6-B/dpfls0922/boj4690.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 WEEK6-B/dpfls0922/boj4690.cpp diff --git a/WEEK6-B/dpfls0922/boj4690.cpp b/WEEK6-B/dpfls0922/boj4690.cpp new file mode 100644 index 00000000..e69de29b From 3d1d2f728b7214073fb3b35d4e136bf97bf45035 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 02:35:51 +0900 Subject: [PATCH 19/24] dpfls0922 boj2231 --- WEEK6-B/boj2231.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 WEEK6-B/boj2231.cpp diff --git a/WEEK6-B/boj2231.cpp b/WEEK6-B/boj2231.cpp new file mode 100644 index 00000000..e69de29b From 5246455ccbb1e7504886229e9da46b9e21e0523a Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 02:50:00 +0900 Subject: [PATCH 20/24] dpfls0922 boj2798 --- WEEK6-B/dpfls0922/boj2798.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 WEEK6-B/dpfls0922/boj2798.cpp diff --git a/WEEK6-B/dpfls0922/boj2798.cpp b/WEEK6-B/dpfls0922/boj2798.cpp new file mode 100644 index 00000000..d9443d26 --- /dev/null +++ b/WEEK6-B/dpfls0922/boj2798.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() { + int a[101]; + int n, m, sum = 0, min = 9999999, answer; + cin >> n >> m; + + for (int i = 0; i < n; i++) + cin >> a[i]; + + for (int i = 0; i < n - 2; i++) { + for (int j = i + 1; j < n - 1; j++) { + for (int k = j + 1; k < n; k++) { + sum = a[i] + a[j] + a[k]; + if (m - sum >= 0 && m - sum < min) { + min = m - sum; + answer = sum; + } + } + } + } + cout << answer; + return 0; +} \ No newline at end of file From 61ea733f87d5fcc9929402d7502dc203c73faad7 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 03:33:04 +0900 Subject: [PATCH 21/24] dpfls0922 boj15657 --- WEEK6-B/boj15657.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 WEEK6-B/boj15657.cpp diff --git a/WEEK6-B/boj15657.cpp b/WEEK6-B/boj15657.cpp new file mode 100644 index 00000000..f7a106ed --- /dev/null +++ b/WEEK6-B/boj15657.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +using namespace std; + +int n, m; +vector v(8, 0); +vector result; + +void fun(int cnt, int start) { + if (cnt == m) { + for (int i = 0; i < m; i++) + cout << result[v[i]] << " "; + cout << "\n"; + } + else { + for (int i = start; i < n; i++) { + v[cnt] = i; + fun(cnt + 1, i); + } + } +} + +int main() { + int num; + cin >> n >> m; + + for (int i = 0; i < n; i++) { + cin >> num; + result.push_back(num); + } + sort(result.begin(), result.end()); + fun(0, 0); + + return 0; +} \ No newline at end of file From 1a87ca8d1bba7cb832ad21d449a4e4161b21db45 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 03:44:03 +0900 Subject: [PATCH 22/24] dpfls0922 boj15657 --- WEEK6-B/boj15657.cpp | 36 ----------------------------------- WEEK6-B/dpfls0922/boj2231.cpp | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 36 deletions(-) delete mode 100644 WEEK6-B/boj15657.cpp create mode 100644 WEEK6-B/dpfls0922/boj2231.cpp diff --git a/WEEK6-B/boj15657.cpp b/WEEK6-B/boj15657.cpp deleted file mode 100644 index f7a106ed..00000000 --- a/WEEK6-B/boj15657.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -using namespace std; - -int n, m; -vector v(8, 0); -vector result; - -void fun(int cnt, int start) { - if (cnt == m) { - for (int i = 0; i < m; i++) - cout << result[v[i]] << " "; - cout << "\n"; - } - else { - for (int i = start; i < n; i++) { - v[cnt] = i; - fun(cnt + 1, i); - } - } -} - -int main() { - int num; - cin >> n >> m; - - for (int i = 0; i < n; i++) { - cin >> num; - result.push_back(num); - } - sort(result.begin(), result.end()); - fun(0, 0); - - return 0; -} \ No newline at end of file diff --git a/WEEK6-B/dpfls0922/boj2231.cpp b/WEEK6-B/dpfls0922/boj2231.cpp new file mode 100644 index 00000000..dd471266 --- /dev/null +++ b/WEEK6-B/dpfls0922/boj2231.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; i++) { + int m = i, tmp = i; + while (tmp) { + m += tmp % 10; + tmp /= 10; + } + if (m == n) { + cout << i << "\n"; + break; + } + if (i == n - 1) cout << "0\n"; + } + return 0; +} \ No newline at end of file From 3fffbfff6c71313065be1f0f158cea8b5df3fd7e Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 03:47:19 +0900 Subject: [PATCH 23/24] dpfls0922 boj15657 --- WEEK6-B/dpfls0922/boj15657.cpp | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 WEEK6-B/dpfls0922/boj15657.cpp diff --git a/WEEK6-B/dpfls0922/boj15657.cpp b/WEEK6-B/dpfls0922/boj15657.cpp new file mode 100644 index 00000000..f7a106ed --- /dev/null +++ b/WEEK6-B/dpfls0922/boj15657.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +using namespace std; + +int n, m; +vector v(8, 0); +vector result; + +void fun(int cnt, int start) { + if (cnt == m) { + for (int i = 0; i < m; i++) + cout << result[v[i]] << " "; + cout << "\n"; + } + else { + for (int i = start; i < n; i++) { + v[cnt] = i; + fun(cnt + 1, i); + } + } +} + +int main() { + int num; + cin >> n >> m; + + for (int i = 0; i < n; i++) { + cin >> num; + result.push_back(num); + } + sort(result.begin(), result.end()); + fun(0, 0); + + return 0; +} \ No newline at end of file From 940882aa89962bb1e9f0267f80aea301c126aba5 Mon Sep 17 00:00:00 2001 From: dpfls0922 <83818069+dpfls0922@users.noreply.github.com> Date: Sun, 21 Nov 2021 04:02:21 +0900 Subject: [PATCH 24/24] README.md --- WEEK6-B/dpfls0922/README.md | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 WEEK6-B/dpfls0922/README.md diff --git a/WEEK6-B/dpfls0922/README.md b/WEEK6-B/dpfls0922/README.md new file mode 100644 index 00000000..d9b177bf --- /dev/null +++ b/WEEK6-B/dpfls0922/README.md @@ -0,0 +1,46 @@ +> # WEEK5-완전탐색 +#### 1. 브루트포스 +#### 2. 백트래킹(DFS) +
+ +# 1️⃣ 브루트포스 +### 완전탐색 (=브루트포스) +* 가능한 모든 경우의 수를 다 체크해서 정답을 찾는 방법 +* 직관적이어서 이해하기 쉽고 문제의 정확한 결과값을 얻어낼 수 있는 가장 확실하며 기초적인 방법 + +### 완전탐색 기법 활용 방법 +1) Brute Force 기법 - 반복 / 조건문을 활용해 모두 테스트하는 방법 +2) 순열(Permutation) - n개의 원소 중 r개의 원소를 중복 허용 없이 나열하는 방법 +3) 재귀 호출 +4) 비트마스크 - 2진수 표현 기법을 활용하는 방법 +5) BFS, DFS를 활용하는 방법 + +# 2️⃣ 백트래킹(DFS) +* DFS는 그래프 자료 구조에서 모든 정점을 탐색하기 위한 방법 +* DFS는 깊이 우선 탐색으로 현재 인접한 정점을 탐색 후 그 다음 인접한 정점을 탐색하여 끝까지 탐색하는 방식 +* BFS와의 큰 차이점은, DFS는 탐색을 한 뒤 이전의 정점으로 돌아온다는 것 +* 이것을 백트래킹(Backtracking)이라고 함 +* 이미 방문한 정점을 다시 방문하지 않음 + +### DFS를 사용하는 예시 +* 그래프 순환 탐지 + - 기타 경로 찾기, 위상 정렬, 미로 찾기 등등 + +### 구현 방법 +* 스택 +* 재귀 방식 + +### C++ 코드 +```C++ +void dfs(int x) +{ + visited[x] = true; + cout << x << " "; + for (int i = 0; i < graph[x].size(); i++) + { + int y = graph[x][i]; + if (!visited[y]) + dfs(y); + } +} +```