프로그래머스 169198 당구 연습

Jmnote (토론 | 기여)님의 2024년 2월 1일 (목) 22:00 판 (새 문서: ==개요== {{프로그래머스|레벨=2|페이지=1|분류=연습문제}} ==C++== <syntaxhighlight lang='cpp'> #include <string> #include <vector> #include <algorithm> using namespa...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

프로그래머스 169198 당구 연습

2 C++

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(int m, int n, int startX, int startY, vector<vector<int>> balls) {
    vector<int> distances;

    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;
            distances.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);
            distances.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);
        distances.push_back(min((startX - x) * (startX - x) + b * b, a * a + (startY - y) * (startY - y)));
    }

    return distances;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}