
내가 생각했을때 문제에서 원하는부분
The first line of input gives 1 ≤ n ≤ 1000, the total number of control points.
Then follow n lines giving their coordinates, with two floating-point numbers xi and yi, with 0.0 ≤ xi, yi ≤ 10000.0.
The number of routes is then given by 1 ≤ m ≤ 100.
Each route is defined by first a line with 2 ≤ p ≤ 17, the number of control points (including start and goal), and then a line with p indexes 0 ≤ i < n, identifying them.
For each test case output the total track distance, rounded, with no decimals.
내가 이 문제를 보고 생각해본 부분
좌표 배열 x[], y[]에 제어점 좌표를 저장한다.
m 개수만큼 반복하여 각 경로에 대해:
경로 내 점의 개수 p와 점들의 인덱스 배열을 읽는다.
각 점과 다음 점 사이의 유클리드 거리(피타고라스 정리 이용)를 구해 totalDistance에 누적한다.
누적된 거리를 Math.round()로 반올림하여 출력한다.
이 방식은 문제에서 요구하는 각 경로 별 총 거리 계산에 적합하며, 각 점 사이 거리 계산의 정확도를 위해 double 타입을 사용했다.
코드로 구현
package baekjoon.baekjoon_34;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// 백준 11113번 문제
public class Main1364 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
double[] x = new double[n];
double[] y = new double[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
x[i] = Double.parseDouble(st.nextToken());
y[i] = Double.parseDouble(st.nextToken());
}
int m = Integer.parseInt(br.readLine()); // 경로 수
for (int routeIndex = 0; routeIndex < m; routeIndex++) {
int p = Integer.parseInt(br.readLine()); // 경로 내 제어점 수
StringTokenizer st = new StringTokenizer(br.readLine());
int[] points = new int[p];
for (int i = 0; i < p; i++) {
points[i] = Integer.parseInt(st.nextToken());
}
double totalDistance = 0;
for (int i = 0; i < p - 1; i++) {
int current = points[i];
int next = points[i + 1];
double dx = x[next] - x[current];
double dy = y[next] - y[current];
totalDistance += Math.sqrt(dx * dx + dy * dy);
}
System.out.println((int)Math.round(totalDistance));
}
br.close();
}
}
코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.