Algorithm/PS

[알고리즘] 붕대 감기

Basaeng 2026. 7. 9. 15:32

https://school.programmers.co.kr/learn/courses/30/lessons/250137

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

간단한 구현문제입니다.

 

문제의 조건을 읽고 실수하지 않는 것이 가장 중요하겠습니다.

 

// t초 붕대를 감는다.
// 1초마다 x씩 회복하며 성공한다면 y만큼 추가로 회복한다 = t*x+y
// 공격을 받으면 중단당함

// bandage = [시전시간, 회복량x, 추가회복량y]
// attacks = [공격 시간, 데미지] 의 배열

라는 특성을 가지고 있습니다.

 

따라서 공격을 당하면 회복을 하지 않고 멈추기 때문에 이에 대한 제어 순서를 정해야하며, 전체의 시간흐름, 붕대의 시간흐름을 관리하는 변수가 필요하겠습니다.

 

#include <string>
#include <vector>

using namespace std;

int gameTick = 0;
int bandageTick = 0;
int baseHealth;
void Bandaging(vector<int>& bandage, int& health)
{
    ++bandageTick;
    health += bandage[1];
    if (bandageTick == bandage[0])
    {
        health += bandage[2];
        bandageTick = 0;
    }

    if (health > baseHealth)
        health = baseHealth;
}

int solution(vector<int> bandage, int health, vector<vector<int>> attacks) 
{
    int answer = 0;
    baseHealth = health;
    int attackidx = 0;
    while (attackidx < attacks.size())
    {
        if (attacks[attackidx][0] == gameTick)
        {
            health -= attacks[attackidx][1];
            if (health <= 0)
                return -1;
            bandageTick = 0;
            ++attackidx;
        }
        else
        {
            Bandaging(bandage, health);
        }

        ++gameTick;
    }
    answer = health;
    return answer;
}

합치면 위와 같습니다.