https://school.programmers.co.kr/learn/courses/30/lessons/169198
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
각 좌표에 따른 조건을 나눈 다음 거리값 계산을 피타고라스를 통해 하면 된다.
거리값의 계산을 수직거리의 제곱과 수평거리의 제곱을 더해주면 된다.
#include <cmath>
#include <vector>
#include <climits>
using namespace std;
int calc_dist(int m, int n, int a, int b, int c, int d)
{
int total = INT_MAX;
//수평거리제곱 + 수직거리제곱
//현재 공보다 위에 있을때
if (a != c || b <= d) total = min(total, (int)(pow(a - c, 2) + pow(b + d, 2)));
//현재 공보다 오른쪽에 있을 때
if (a >= c || b != d) total = min(total, (int)(pow(a - 2 * m + c, 2) + pow(b - d, 2)));
//현재 공보다 아래에 있을 때
if (a != c || b >= d) total = min(total, (int)(pow(a - c, 2) + pow(b - 2 * n + d, 2)));
//현재 공보다 왼쪽에 있을때
if (a <= c || b != d) total = min(total, (int)(pow(a + c, 2) + pow(b - d, 2)));
return total;
}
vector<int> solution(int m, int n, int startX, int startY, vector<vector<int>> balls)
{
vector<int> answer;
for (int i = 0; i < balls.size(); i++)
answer.push_back(calc_dist(m, n, startX, startY, balls[i][0], balls[i][1]));
return answer;
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스][LV2][C++]3*n 타일링 (0) | 2024.07.17 |
---|---|
[프로그래머스][LV2][C++]교점에 별 만들기 (1) | 2024.07.16 |
[프로그래머스][LV2][C++][PCCP 기출문제] 2번 / 석유 시추 (0) | 2024.07.12 |
[프로그래머스][LV2][C++]택배 배달과 수거하기 (0) | 2024.07.11 |
[프로그래머스][LV2][C++]순위검색 (0) | 2024.07.10 |