티스토리 뷰
https://www.acmicpc.net/problem/16987
16987번: 계란으로 계란치기
원래 프로그래머의 기본 소양은 팔굽혀펴기를 단 한 개도 할 수 없는 것이라고 하지만 인범이는 3대 500을 넘기는 몇 안되는 프로그래머 중 한 명이다. 인범이는 BOJ에서 틀린 제출을 할 때마다 턱
www.acmicpc.net
#include <iostream>
#include <vector>
using namespace std;
int n;
int ans = 0;
vector<pair<int,int>> egg;
// 깨진 계란수를 샌다
int CountBrokenEggs()
{
int cnt = 0;
for(int i = 0; i < n; i++)
{
if(egg[i].first <= 0)
cnt++;
}
return cnt;
}
void dfs(int onHand)
{
// 손에 든게 마지막 계란이라면
if(onHand == n)
{
ans = max(ans, CountBrokenEggs());
return;
}
// 손에 든 계란이 깨져있다면 다음 계란을 들러간다
if(egg[onHand].first <= 0)
dfs(onHand+1);
// 손에 든 계란이 안깨진 상태이다
else
{
// 계란을 부딪혔는지 아닌지 판단
// 더이상 칠수 있는 계란이 없다면 false로 남을 것이다
bool hitEgg = false;
for(int i = 0; i < n; i++)
{
// 손에 든 계란이거나 이미 깨진계란 이라면 부딪힐수없다
if(i == onHand || egg[i].first <= 0) continue;
// 손에 든 계란과 다른 계란을 부딪친다
egg[onHand].first = egg[onHand].first - egg[i].second;
egg[i].first = egg[i].first - egg[onHand].second;
// 다음 계란을 집는다
dfs(onHand+1);
// 다른 계란을 쳐보기 위해 계란을 치기전으로 복원한다
egg[onHand].first = egg[onHand].first + egg[i].second;
egg[i].first = egg[i].first + egg[onHand].second;
hitEgg = true; // 어떤 계란이던지 부딪쳤다
}
// 어떤 계란도 부딪히지 못했다면 마지막 남은 계란
if(!hitEgg) dfs(n);
}
}
int main()
{
cin >> n;
for(int i = 0; i < n; i++)
{
int s, w;
cin >> s >> w;
egg.push_back({s,w});
}
dfs(0);
cout << ans;
}
'PS' 카테고리의 다른 글
프로그래머스. 소수 찾기 (0) | 2021.07.27 |
---|---|
프로그래머스. 모의고사 (0) | 2021.07.27 |
백준 16945. 매직 스퀘어로 변경하기 (0) | 2021.07.24 |
백준 10819. 차이를 최대로 (0) | 2021.07.24 |
백준 16283. Farm (0) | 2021.07.24 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DP
- back tracking
- two pointer
- dfs
- C++
- Implementation
- CSS
- binary search
- priority queue
- Stack
- Spring
- C
- 자료구조
- Brute Force
- greedy
- 이분탐색
- recursion
- Kruskal
- permutation
- graph
- MVC
- Tree
- Python
- floyd warshall
- db
- Unity
- BFS
- 조합
- Dijkstra
- 재귀
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
글 보관함