-
백준(BOJ) 16987번 계란으로 계란치기알고리즘 풀이/백준(Boj) 2019. 11. 30. 02:14
문제 : https://www.acmicpc.net/problem/16987
16987번: 계란으로 계란치기
원래 프로그래머의 기본 소양은 팔굽혀펴기를 단 한 개도 할 수 없는 것이라고 하지만 인범이는 3대 500을 넘기는 몇 안되는 프로그래머 중 한 명이다. 인범이는 BOJ에서 틀린 제출을 할 때마다 턱걸이를 5회 하는 기적의 운동 루틴을 통해 뇌와 근육을 동시에 단련한다. 근육을 단련할 때 식단이 정말로 중요하다는 것을 아는 인범이는 탄수화물이 많은 밥이나 빵 따위의 아침 식사를 대신해 단백질이 많은 계란찜을 해먹는다. 계란찜을 먹기 위해서는 계란을 깨야 하
www.acmicpc.net
풀이 :
제일 왼쪽 기준 계란을 시작으로 n-1개의 계란들 중 한 계란을 골라서 깨주어야 한다.
깰수있는 계란들 중 어느것을 골라서 깨느냐에 따라 정답이 달라지기 때문에 하나하나 모든 경우의
수를 다 처리 해주어야 한다. 이때 기준 계란이 깨져있을 경우나 혹은 선택하는 계란들이 모두 깨져있다면
다음 기준으로 곧바로 넘어감을 주의하자.
코드 ( C++ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <iostream> #include <algorithm> using namespace std; int n; int dur[8]; int w[8]; int solve(int target) { if (target == n) { int sum = 0; for (int i = 0; i < n; ++i) if (dur[i] <= 0) sum++; return sum; } if (dur[target] <= 0) return solve(target + 1); bool check = false; int ret = 0; for (int cand = 0; cand < n; ++cand) { if (target == cand) continue; if (dur[cand] <= 0) continue; check = true; dur[target] -= w[cand]; dur[cand] -= w[target]; ret = max(ret, solve(target + 1)); dur[target] += w[cand]; dur[cand] += w[target]; } if (!check) { return solve(target + 1); } return ret; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> dur[i] >> w[i]; } cout << solve(0) << "\n"; } '알고리즘 풀이 > 백준(Boj)' 카테고리의 다른 글
백준(BOJ) 16988번 Baaaaaaaaaduk2 (Easy) (0) 2019.12.02 백준(BOJ) 16935번 배열 돌리기 3 (0) 2019.12.01 백준(BOJ) 16939번 2x2x2 큐브 (0) 2019.11.30 백준(BOJ) 16235번 나무 재테크 (0) 2019.11.26 백준(BOJ) 16985번 - Maaaaaaaaaze (0) 2019.11.25