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

[백트래킹] 3월 31일 - ing #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

yjk395
Copy link
Collaborator

@yjk395 yjk395 commented Mar 31, 2022

내용 & 질문

감사합니다

<기존 제출>

20055, 1205, 15663, 10971, 14888

Copy link

@hoit1302 hoit1302 left a comment

Choose a reason for hiding this comment

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

구현부터 백트래킹까지 이번 모든 과제가 어려울 수 있는데 잘 풀이해주셨어요!
필수 2문제, 선택 3문제 제출 확인했습니다 🥳

p2로 요청드린 두 코멘트 모두 사실 p1으로 드릴까 고민했어요 ㅎㅎ
10971번은 시간을 많이 줄일 수 있고
14888번은 백트래킹을 벗어난 풀이이지만 사실 지금 풀이 방법도 최적의 방식이라서 p2로 뒀습니다!
시간 나시면 꼭 다시 시도해보세요 👍🏻

기록 보니 오늘 아침부터 열심히 푸셨더라구요..!!
단 시간에 구현해내시고 또 코드 보니 충분히 하실 수 있을 것 같습니다 🥰

너무 수고하셨습니다 ✨

Comment on lines +14 to +33
void traveling(int cnt, int city, int cur_cost) {
//순회 완료
if (cnt == n && city == start) { //현재 도시가 시작점과 같음
// cout<<"["<<cur_cost<<"]";
min_cost = min(min_cost, cur_cost); //최솟값 업데이트 후 호출종료
return;
}

for (int i = 0; i < n; i++) {
//비용 0인 곳, 이미 방문한 도시는 갈 수 없음
if (!check[i] && cost[city][i]) {
//한 곳 방문하고 재귀호출
check[i] = true;
// cout<<"["<<cur_cost<<' '<<cost[city][i]<<' '<<cnt<<"]";
traveling(cnt + 1, i, cur_cost + cost[city][i]);

check[i] = false; //방문여부 되돌리기
}
}
}

Choose a reason for hiding this comment

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

p2. 고려해주세요
이 문제는 가지치기를 훨씬 많이 할 수 있는 문제에요.
현재 작성해주신 코드로는 1832ms 가 소요됩니다.

  1. 시작하는 도시는 어떤 도시든 상관 없으므로 한 도시로 고정해도 됩니다!!!
    이 부분에 대해서 더 설명이 필요하다면 말씀해주세요 😊

  2. cur_cost 값이 min_cost 값과 같거나 클 때는 더 이상 해당 경우에 대해서 더 깊게 탐색하지 않도록 막아줍니다.
    (여기서 저는 20ms -> 0ms로 줄일 수 있었습니다.)

Comment on lines +33 to +35
amin = min(amin, result); //답 업데이트
amax = max(amax, result);
} while (next_permutation(op.begin(), op.end())); //연산자 다음 순열

Choose a reason for hiding this comment

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

p2. 고려해주세요
중복되는 원소가 있는 배열의 순열 구하는 연산을 next_permutation을 활용해서 구현해주셨습니다.
next_permutation은 중복되는 순열을 결과값으로 두지 않기 때문에 사실 최적의 풀이라고도 할 수 있을 것 같습니다.
하지만 이번 과제의 취지는 백트래킹으로 구현해보는 것이었습니다..!
따라서 재귀함수를 이용하여 연산을 구현해보시는 것도 좋을 것 같습니다 🥰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants