프로그래머스 169198 당구 연습

Jmnote (토론 | 기여)님의 2024년 2월 1일 (목) 22:01 판 (→‎C++)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

프로그래머스 169198 당구 연습

2 C++[ | ]

#include <string>
#include <vector>

using namespace std;

vector<int> solution(int m, int n, int startX, int startY, vector<vector<int>> balls) {
    vector<int> answer;
    for (const auto& ball : balls) {
        int x = ball[0];
        int y = ball[1];
        if (startX == x) {
            int a = min(startX + x, 2 * m - startX - x);
            int b = startY >= y ? 2 * n - startY - y : startY + y;
            answer.push_back(min(a * a + (startY - y) * (startY - y), b * b));
            continue;
        }
        if (startY == y) {
            int a = startX >= x ? 2 * m - startX - x : startX + x;
            int b = min(startY + y, 2 * n - startY - y);
            answer.push_back(min((startX - x) * (startX - x) + b * b, a * a));
            continue;
        }
        int a = min(startX + x, 2 * m - startX - x);
        int b = min(startY + y, 2 * n - startY - y);
        answer.push_back(min((startX - x) * (startX - x) + b * b, a * a + (startY - y) * (startY - y)));
    }
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}