Processing math: 100%

2564 - 경비원

2025. 3. 4. 14:41PS/백준

문제

https://www.acmicpc.net/problem/2564

느낀점

구현 문제, 그냥 생각나는대로 구현하는 게 제일 빠른 경우도 있다. 구현 시간이 길어질 때, 집중력 잃지 말기

풀이

#include <iostream>
#include <vector>

using namespace std;

#define pii pair<int, int>
#define pipii pair<int, pii>

int calcMinDistance(pipii security, pipii store, int width, int height)
{
    if (security.first == store.first)
    {
        return abs(security.second.first - store.second.first);
    }
    else
    {
        if (security.first == 1 && store.first == 2 || security.first == 2 && store.first == 1)
        {
            if (store.second.first + security.second.first <= store.second.second + security.second.second)
            {
                return store.second.first + security.second.first + height;
            }
            else
            {
                return store.second.second + security.second.second + height;
            }
        }
        else if (security.first == 1 && store.first == 3)
        {
            return security.second.first + store.second.first;
        }
        else if (security.first == 1 && store.first == 4)
        {
            return security.second.second + store.second.first;
        }
        else if (security.first == 2 && store.first == 3)
        {
            return security.second.first + store.second.second;
        }
        else if (security.first == 2 && store.first == 4)
        {
            return security.second.second + store.second.second;
        }
        else if (security.first == 3 && store.first == 4 || security.first == 4 && store.first == 3)
        {
            if (store.second.first + security.second.first <= store.second.second + security.second.second)
            {
                return store.second.first + security.second.first + width;
            }
            else
            {
                return store.second.second + security.second.second + width;
            }
        }
        else if (security.first == 3 && store.first == 1)
        {
            return security.second.first + store.second.first;
        }
        else if (security.first == 3 && store.first == 2)
        {
            return security.second.second + store.second.first;
        }
        else if (security.first == 4 && store.first == 1)
        {
            return security.second.first + store.second.second;
        }
        else if (security.first == 4 && store.first == 2)
        {
            return security.second.second + store.second.second;
        }
    }
}

pii calcDistance(int width, int height, int direction, int distance)
{
    if (direction == 1 || direction == 2)
    {
        return make_pair(distance, width - distance);
    }
    else if (direction == 3 || direction || 4)
    {
        return make_pair(distance, height - distance);
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    int storeNumber;
    cin >> storeNumber;
    int direction, distance;
    int answer = 0;
    vector<pipii> store;
    for (int i = 0; i < storeNumber; i++)
    {
        cin >> direction >> distance;
        store.push_back(make_pair(direction, calcDistance(n, m, direction, distance)));
    }
    cin >> direction >> distance;
    pipii security = make_pair(direction, calcDistance(n, m, direction, distance));
    for (size_t i = 0; i < store.size(); i++)
    {
        int dist = calcMinDistance(security, store[i], n, m);
        answer += dist;
    }

    cout << answer;

    return 0;
}

'PS > 백준' 카테고리의 다른 글

3495 - 아스키 도형  0 2025.03.04
2615 - 오목  0 2025.03.04
2002 - 추월  0 2025.02.28
1986 - 체스  0 2025.02.26
1713 - 후보 추천하기  0 2025.02.26