Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

09_백트랙킹 #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"files.associations": {
"stack": "cpp",
"xiosbase": "cpp",
"map": "cpp"
"map": "cpp",
"vector": "cpp"
},
"C_Cpp.errorSquiggles": "disabled"
}
65 changes: 65 additions & 0 deletions 09_백트랙킹/필수/14888.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <stack>
#include <deque>

using namespace std;

int N;
int operand[11];
int op[4];
int MIN=1000000001;
int MAX=-1000000001;

void getResult(int result, int idx){
if(idx==N){
if(result>MAX){
MAX=result;
}
if(result<MIN){
MIN=result;
}
return;
}
for(int i=0; i<4; i++){
if(op[i]>0){
op[i]--;
switch(i){
case 0:{
getResult(result+operand[idx], idx+1);
break;
}
case 1:{
getResult(result-operand[idx], idx+1);
break;
}
case 2:{
getResult(result*operand[idx], idx+1);
break;
}
case 3:{
getResult(result/operand[idx], idx+1);
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 가지 피드백을 드린다면,
변하지 않는 값을 상수값으로 정의해서 사용해보세용!

const int ADD = 0, SUB = 1, MUL = 2, DIV = 3;

// 생략...

switch(i) {
    case ADD:{
        // 더하기 로직
    }
}

}
op[i]++;

}
}
return;
}
int main(){

cin >> N;

for(int i=0; i<N; i++){
cin >> operand[i];
}
for(int j=0; j<4; j++){
cin >> op[j];
}
getResult(operand[0], 1);
cout << MAX << "\n";
cout << MIN << "\n";

return 0;
}
44 changes: 44 additions & 0 deletions 09_백트랙킹/필수/15665.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define MAX 9

int N,M;
int input[MAX];
int arr[MAX];
set<vector<int>> s;

void dfs(int k) {
if(k==M) { //끝
vector<int> v;
for(auto i=0;i<M;i++)
v.push_back(arr[i]);
s.insert(v);
v.clear();
}else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이럴때 else를 쓰기보다,
if (k==M) 만족 시 return 하는 방식을 사용해보세용!
코드를 더 깔끔하게 만들 수 있습니당☺️

for(auto i=0; i<N;i++) {
arr[k]=input[i];
dfs(k+1);
}
}
}

int main() {
cin >> N >> M;

for(int i=0;i<N;i++)
cin >> input[i];

sort(input,input+N);

dfs(0);

for(auto vector:s) {
for(auto temp:vector)
cout << temp << " ";
cout << "\n";
}
}
37 changes: 37 additions & 0 deletions 09_백트랙킹/필수/2477.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>

using namespace std;

pair<int, int> li[12];

int findArea() { // 육각형 모양의 영역 너비를 구하는 함수
int big_square; //사각형 중 큰 부분
int small_square; //사각형 중 작은 부분을 의미.
for (int i = 0; i < 9; i++) { //for문을 돌아가며 외부 사각형과 내부 사각형의 너비를 갱신.
if (li[i].first == li[i + 2].first && li[i + 1].first == li[i + 3].first) { // 같은 방향이 한 칸을 건너뛰고 나타나는 형태이면
big_square = (li[i].second + li[i + 2].second) * (li[i + 1].second + li[i + 3].second); // 전체 사각형 갱신
small_square = li[i + 2].second * li[i + 1].second; // 작은 사각형(영역이 없는 구역 갱신)
}
}
return big_square - small_square; //큰사각형-작은 사각형을 빼주는 것을 최종 결과값으로 리턴.
}

int main() {
int k;
cin >> k; //1m2의 넓이에 자라는 참외의 개수

int direction, length;
for (int i = 0; i < 6; i++) {
cin >> direction >> length; //방향과 길이를 입력받음.
li[i] = {direction, length}; //입력받은 방향과 길이를 pair로 묶어 배열에 저장.
}

for (int i = 0; i < 6; i++) { //어디서부터 입력이 들어오는지 모르기 때문에 6개 변을 추가해줌
li[i + 6] = li[i];
}

int area = findArea(); //결과값에 함수의 결과를 할당.
cout << k * area << "\n"; //1m^2에서 자라는 참외의 개수*너비를 곱해준 값이 참외가 자라는 총 개수

return 0;
}