[Algorithm] [PCCP 기출문제] 1번

gunggme·2023년 12월 4일

알고리즘

목록 보기
33/42


시작

이문제는 구현문제다. 그렇다면 어떻게 구현해야될지부터 알아보자. 우선 자료형부터 알아보자면 bandage라는 1차원 배열, health라는 체력, attacks라는 2차원 배열이 있다. 그렇다면 이것을 가지고 어떻게 해결해야되는지 알고맂므을 우선 짜보자.

알고리즘

  1. 공격을 받는지 안받는지 확인
  2. 만약 공격을 받으면 연속회복 초기화, 체력 줄이기 continue
  3. 공격을 안받으면 체력을 bandage[1]만큼 회복
  4. 만약 bandage[0]만큼 연속적으로 회복했으면, bandage[2] + bandage[1]만큼 회복
  5. 회복이 초기에 정해진 체력보다 클경우 초기의 체력으로 초기화
  6. 만약 0이하의 체력이면 -1 아니면 그대로 return

이런식으로 복잡하다면 복잡한 알고리즘이 작성이 된다. 그렇다면 한번 이 알고리즘을 가지고 코드를 작성을 해보자.

코드

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> bandage, int health, vector<vector<int>> attacks) {
	int maxHP = health, maxTime = attacks[attacks.size() - 1][0];
	int healTime = bandage[0], attackTime;
	int curAttackTime = 1, heal = 0, curAttackType = 0, bonusHeal = bandage[0]-1;
	int answer = 0;
	for (curAttackTime; curAttackTime <= maxTime; curAttackTime++) {
		if (curAttackTime == attacks[curAttackType][0]) {
			health -= attacks[curAttackType][1];
			curAttackType++;
			heal = 0;
            if(health <= 0){
                return -1;
            }
			continue;
		}
		if (heal != bonusHeal) {
			health += bandage[1];
			heal++;
		}
		else if (heal == bonusHeal) {
			health += bandage[2] + bandage[1];
			heal = 0;
		}
		if (health >= maxHP) {
			health = maxHP;
		}
	}
	
    return health;
    
}
profile
안녕하세요!

0개의 댓글