BJ 2564保安


https://www.acmicpc.net/problem/2564
同じルートを時計回りまたは反時計回りに移動する場合は、その距離を計算すればよい.
この問題ではショップ数が3つに固定されているので、ドアで表します.
店舗数が増加すると、シーケンスとそのパラメータ値との間に距離を加えることで実現できます.
package day0210;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Security {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));

		st = new StringTokenizer(br.readLine());
		int lengthOfY = Integer.parseInt(st.nextToken());
		int lengthOfX = Integer.parseInt(st.nextToken());

		int N = Integer.parseInt(br.readLine());
		int[][] loca = new int[N + 1][2];

		for (int i = 0; i < N + 1; i++) { // loca[N]은 동근이의 위치
			st = new StringTokenizer(br.readLine());
			switch (Integer.parseInt(st.nextToken())) {
			case 1:
				loca[i][0] = lengthOfX;
				loca[i][1] = Integer.parseInt(st.nextToken());
				break;
			case 2:
				loca[i][0] = 0;
				loca[i][1] = Integer.parseInt(st.nextToken());
				break;
			case 3:
				loca[i][0] = lengthOfX - Integer.parseInt(st.nextToken());
				loca[i][1] = 0;
				break;
			case 4:
				loca[i][0] = lengthOfX - Integer.parseInt(st.nextToken());
				loca[i][1] = lengthOfY;
				break;
			}

		}
		int sumofmin = 0;
		for (int i = 0; i < N; i++) {
			if (Math.abs(loca[i][1] - loca[N][1]) == lengthOfY) {		
				sumofmin += lengthOfY
						+ Math.min(loca[i][0] + loca[N][0], lengthOfX - loca[i][0] + lengthOfX - loca[N][0]);
			} else if (Math.abs(loca[i][0] - loca[N][0]) == lengthOfX) {
				sumofmin += lengthOfX
						+ Math.min(loca[i][1] + loca[N][1], lengthOfY - loca[i][1] + lengthOfY - loca[N][1]);
			} else {
				sumofmin += Math.abs(loca[i][0] - loca[N][0]) + Math.abs(loca[i][1] - loca[N][1]);
			}
		}
		/*for (int i = 0; i < N + 1; i++) {
			System.out.println(loca[i][0] + " "+ loca[i][1]);
		}*/
		System.out.print(sumofmin);
		bw.flush();
		bw.close();
	}
}```