-
Notifications
You must be signed in to change notification settings - Fork 0
[백트래킹] 5월 1일 #9
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
base: main
Are you sure you want to change the base?
[백트래킹] 5월 1일 #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #include <iostream> | ||
| #include <algorithm> | ||
| #include <vector> | ||
| using namespace std; | ||
| int N; | ||
| bool visited[10]; | ||
| int operators[4]; | ||
| int W[10][10]; | ||
| int min_cost = 1e9; | ||
| void backtracking(int idx, int count, int cost, int start) { | ||
| if (count == N) { | ||
| if (W[idx][start] != 0) { | ||
| min_cost = min(min_cost, cost + W[idx][start]); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| for (int next = 0; next < N; next++) { | ||
| if (!visited[next] && W[idx][next] != 0) { | ||
| visited[next] = true; | ||
| backtracking(next, count + 1, cost + W[idx][next], start); | ||
| visited[next] = false; | ||
| } | ||
| } | ||
|
Comment on lines
+18
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메인 로직 아주 깔끔합니다! |
||
| } | ||
| int main() { | ||
| //입력 | ||
| cin >> N; | ||
| for (int i = 0; i < N; i++) { | ||
| for (int j = 0; j < N; j++) { | ||
| cin >> W[i][j]; | ||
| } | ||
| } | ||
| //연산 | ||
| for (int i = 0; i < N; i++) { | ||
| fill(visited, visited + N, false); | ||
| visited[i] = true; | ||
| backtracking(i, 1, 0, i); | ||
| } | ||
|
Comment on lines
+34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. 문제의 조건은 한바퀴 도는 것이었어요! |
||
| //출력 | ||
| cout << min_cost; | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #include <iostream> | ||
| #include <algorithm> | ||
| #include <vector> | ||
| using namespace std; | ||
| int N; | ||
| vector<int> numbers; | ||
| int operators[4]; | ||
| int max_result = -1e9; | ||
| int min_result = 1e9; | ||
| void backtracking(int idx, int current) { | ||
| if (idx == N) { | ||
| max_result = max(max_result, current); | ||
| min_result = min(min_result, current); | ||
| return; | ||
| } | ||
| for (int i = 0; i < 4; i++) { | ||
| if (operators[i] > 0) { | ||
| operators[i]--; | ||
| int next = current; | ||
| if (i == 0) { | ||
| next += numbers[idx]; | ||
| } | ||
| else if (i == 1) { | ||
| next -= numbers[idx]; | ||
| } | ||
| else if (i == 2) { | ||
| next *= numbers[idx]; | ||
| } | ||
| else if (i == 3) { | ||
| if (next < 0) { | ||
| next = -(-next / numbers[idx]); | ||
| } | ||
|
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 계산 결과가 달라지지 않아서 조건을 나누실 필요가 없습니다! |
||
| else next /= numbers[idx]; | ||
| } | ||
|
Comment on lines
+20
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p3. 이 부분을 함수로 빼주면 조금 더 깔끔한 코드가 될 것 같아요! |
||
| backtracking(idx + 1, next); | ||
| operators[i]++; | ||
| } | ||
| } | ||
| } | ||
| int main() { | ||
| //입력 | ||
| cin >> N; | ||
| numbers.resize(N); | ||
| for (int i = 0; i < N; i++) { | ||
| cin >> numbers[i]; | ||
| } | ||
| for (int i = 0; i < 4; i++) { | ||
| cin >> operators[i]; | ||
| } | ||
| //연산 | ||
| backtracking(1, numbers[0]); | ||
| //출력 | ||
| cout << max_result << "\n" << min_result; | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| using namespace std; | ||
| int N, K; | ||
| vector<int> robot; | ||
| vector<bool> location; | ||
| int main() { | ||
| //입력 | ||
| cin >> N >> K; | ||
| roboot.resize(2 * N); | ||
| location.resize(N, false); | ||
| for (int i = 0; i < 2 * N; i++) { | ||
| cin >> robot[i]; | ||
| } | ||
| int step = 0; | ||
| //연산 | ||
| //출력 | ||
| cout << step; | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #include <iostream> | ||
| #include <deque> | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct info { // 각 칸마다 내구도와 로봇 존재 여부 저장하는 구조체 | ||
| int power; //내구도 | ||
| bool is_on = false; //로봇이 해당 칸 위에 있는지 여부(default=없음) | ||
| }; | ||
|
|
||
| // 벨트를 한 칸 회전 | ||
| void rotateBelt(deque<info>& belt, int n) { | ||
| belt.push_front(belt.back()); // 벨트의 마지막 칸을 앞에 push | ||
| belt.pop_back(); // 마지막 칸을 pop | ||
| belt[n - 1].is_on = false; // 내리는 위치(n-1)에 로봇이 있으면 로봇 내리기 | ||
| } | ||
|
|
||
| // 로봇을 움직일 수 있다면 한 칸 이동 | ||
| void moveRobot(deque<info>& belt, int n) { | ||
| for (int i = n - 2; i >= 0; i--) { // 배열 접근 가능 범위가 0~n-1인데 다음 칸과 비교하기 위해 0~n-2까지 반복 | ||
| // 현재 칸에 로봇이 존재하고, 다음 칸에 로봇이 없으며, 다음 칸에 내구도가 남아있을 때 이동 가능 | ||
| if (belt[i].is_on && !belt[i + 1].is_on && (belt[i + 1].power >= 1)) { | ||
| belt[i].is_on = false; //현재 위치에서 로봇 제거 | ||
| belt[i + 1].is_on = true; //다음 위치에서 로봇 배치 | ||
| belt[i + 1].power--; //다음 위치 내구도 감소 | ||
| } | ||
| } | ||
| belt[n - 1].is_on = false; // 내리는 위치에 로봇이 도달하면 로봇 내리기 | ||
| } | ||
|
|
||
| // 올리는 칸에 로봇을 올릴 수 있다면 올리기 | ||
| void putRobot(deque<info>& belt) { | ||
| // 올리는 칸의 내구도가 남아 있으면(1이상) | ||
| if (belt[0].power >= 1) { | ||
| belt[0].is_on = true; //로봇 올리기 | ||
| belt[0].power--; //내구도 감소 | ||
| } | ||
| } | ||
|
|
||
| // 벨트의 내구도 체크(내구도가 0인 칸의 개수가 k개 이상인지 확인) | ||
| bool checkFinish(deque<info>& belt, int n, int k) { | ||
| int cnt = 0; // 내구도 0인 칸의 개수 | ||
| for (int i = 0; i < 2 * n; i++) { | ||
| if (belt[i].power == 0) { | ||
| cnt++; //내구도가 0인 칸 수 세기 | ||
| } | ||
| } | ||
|
|
||
| return cnt >= k; //내구도 0인 칸이 k개 이상이면 true반환 | ||
| } | ||
| //전체 시뮬레이션을 실행하고 종료 시점의 단계를 반환 | ||
| int solution(deque<info>& belt, int n, int k) { | ||
| // 1단계부터 시작 | ||
| int step = 1; | ||
| while (true) { | ||
| // 1. 벨트 회전 | ||
| rotateBelt(belt, n); | ||
|
|
||
| // 2. 로봇 이동 | ||
| moveRobot(belt, n); | ||
|
|
||
| // 3. 로봇 올리기 | ||
| putRobot(belt); | ||
|
|
||
| // 4. 내구도가 0인 칸의 개수가 k개 이상인지 체크 | ||
| if (checkFinish(belt, n, k)) { | ||
| return step; //조건을 충족하면 현재 단계를 반환 | ||
| } | ||
| step++; //다음단계로 | ||
| } | ||
| } | ||
| int main() | ||
| { | ||
| ios::sync_with_stdio(false); | ||
| cin.tie(NULL); | ||
| cout.tie(NULL); | ||
|
|
||
| // 입력 | ||
| int n, k; // n:컨베이어 벨트의 길이, k:내구도 0인 칸의 최대 개수 | ||
| cin >> n >> k; | ||
| deque<info> belt(2 * n); // 2n개의 칸을 가지는 벨트 deque | ||
|
|
||
| // 벡트의 각 칸마다 내구도 입력, 로봇 존재 여부 초기화 | ||
| for (int i = 0; i < 2 * n; i++) | ||
| { | ||
| cin >> belt[i].power; //내구도 저장 | ||
| } | ||
|
|
||
| // 연산 & 출력 | ||
| cout << solution(belt, n, k); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2. 지금 목표가 최솟값을 찾는 것이니, 그보다 큰 값은 끝까지 탐색하지 않아도 괜찮아요!
이 조건을 넣어주면 탐색의 효율이 더 높아질 수 있습니다!