BJ2564 경비원

·2022년 4월 17일
0

백준 알고리즘

목록 보기
9/34

https://www.acmicpc.net/problem/2564

동근이가 시계방향 또는 반시계방향으로 이동할 때, 그 거리만 잘 계산해주면 된다.

이 문제에서는 상점의 수가 3개로 고정이기에 중첩 for문으로 구현했지만,
상점의 수가 많아지면 순열과 그 파라미터값에 거리의 합을 두는 방식으로 구현하면 될 것이다.

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();
	}
}```
profile
SSAFY 7기

0개의 댓글