Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 트리
- Unreal Engine 5
- ue5
- 백준
- XR Interaction Toolkit
- 다이나믹 프로그래밍
- Team Fortress 2
- 그래프
- 투 포인터
- 브루트포스
- 자료구조
- 시뮬레이션
- 수학
- 유니온 파인드
- BFS
- VR
- 그리디 알고리즘
- 우선순위 큐
- 알고리즘
- 누적 합
- 문자열
- 백트래킹
- 유니티
- c++
- 정렬
- 재귀
- 다익스트라
- 스택
- DFS
- 구현
Archives
- Today
- Total
1일1알
백준 16987번 계란으로 계란치기 C++ 본문
백트래킹을 이용하여 가능한 모든 경우를 구하고 그 경우들 중에서 계란이 가장 많이 깨진 개수를 구하면 된다.
처음에 문제를 풀때 현재 잡은 계란의 오른쪽 계란만 깰 수 있다고 생각하고 풀어서 답이 안나왔다.
그래서 문제를 다시 읽어보니 그런 조건이 없어서 모든 경우를 탐색하도록 바꿨더니 문제가 풀렸다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int n;
vector<pair<pair<int, int>, int>> v(8);
int ans = 0;
void BT(int index) {
if (index >= n) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (!v[i].second) cnt++;
ans = max(ans, cnt);
}
return;
}
if (v[index].second == false) {
BT(index + 1);
return;
}
bool isAllBreak = true;
for (int i = 0; i < n; i++) {
if (i == index) continue;
if (v[i].second) {
isAllBreak = false;
}
}
if (isAllBreak) {
BT(index + 1);
return;
}
int lastS = v[index].first.first;
int lastW = v[index].first.second;
for (int i = 0; i < n; i++) {
if (i == index) continue;
if (v[i].second == false) {
continue;
}
int lastS2 = v[i].first.first;
int lastW2 = v[i].first.second;
v[index].first.first -= v[i].first.second;
v[i].first.first -= v[index].first.second;
if (v[i].first.first <= 0) {
v[i].first.first = 0;
v[i].second = false;
}
if (v[index].first.first <= 0) {
v[index].first.first = 0;
v[index].second = false;
}
BT(index + 1);
v[index].first.first = lastS;
v[index].first.second = lastW;
v[index].second = true;
v[i].first.first = lastS2;
v[i].first.second = lastW2;
v[i].second = true;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
int s, w;
cin >> s >> w;
v[i].first.first = s;
v[i].first.second = w;
v[i].second = true;
}
BT(0);
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1052번 물병 C++ (0) | 2021.12.25 |
---|---|
백준 1790번 수 이어 쓰기 2 C++ (0) | 2021.12.24 |
백준 10835번 카드게임 C++ (0) | 2021.12.22 |
백준 9009번 피보나치 C++ (0) | 2021.12.21 |
백준 15661번 링크와 스타트 C++ (0) | 2021.12.20 |