문제 분석

- 전체 경우의 수를 전부 고려하여 구현하였다.

 

 

문제 풀이

1) 상점의 위치가 동서남북방향인 경우로 나눈다

2) 각각의 상점 위치에 따라 동근이의 위치를 동서남북으로 나누어 최소값을 계산한다.

3) 계산한 최소값을 더하여 출력한다.

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	private static int[][] map;
	private static int X, Y, N, min, hap;

	public static void main(String[] args) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		st = new StringTokenizer(br.readLine());
		X = Integer.parseInt(st.nextToken());
		Y = Integer.parseInt(st.nextToken());

		N = Integer.parseInt(br.readLine());
		map = new int[N + 1][2];
		hap = (X + Y) * 2;
		for (int i = 0; i < N + 1; i++) { // 마지막 행에는 동근이의 위치 저장
			st = new StringTokenizer(br.readLine());
			map[i][0] = Integer.parseInt(st.nextToken());
			map[i][1] = Integer.parseInt(st.nextToken());
		}

		for (int i = 0; i < N; i++) {
			min += go(map[i][0], map[i][1]);
		}
		System.out.println(min);
	}// main

	private static int go(int r, int c) {
		if (r == 1) { // 상점 : 북
			if (map[N][0] == 1) { // 동근 : 북
				min = Math.max(map[N][1], c) - Math.min(map[N][1], c); // 큰 수에서 작은 수 빼기
			} else if (map[N][0] == 2) { // 동근 : 남
				// 동근이 위치에서 시계방향
				min = map[N][1] + Y + c;
				// 반시계 방향과 비교해서 작은 수 넣기
				min = Math.min(min, (hap - min));
			} else if (map[N][0] == 3) { // 동근 : 서
				min = map[N][1] + c;
			} else { // 동근 : 동
				min = map[N][1] + (X - c);
			}

		} else if (r == 2) { // 상점 : 남
			if (map[N][0] == 1) { // 동근 : 북
				// 동근이의 위치에서 반시계방향
				min = map[N][1] + Y + c;
				// 시계 방향과 비교해서 작은 수 넣기
				min = Math.min(min, hap - min);
			} else if (map[N][0] == 2) { // 동근 : 남
				min = Math.max(map[N][1], c) - Math.min(map[N][1], c);
			} else if (map[N][0] == 3) { // 동근 : 서
				min = (Y - map[N][1]) + c;
			} else { // 동근 : 동
				min = (X - c) + (Y - map[N][1]);
			}

		} else if (r == 3) { // 상점 : 서
			if (map[N][0] == 1) { // 동근 : 북
				min = map[N][1] + c;

			} else if (map[N][0] == 2) { // 동근 : 남
				min = (Y - c) + map[N][1];
			} else if (map[N][0] == 3) { // 동근 : 서
				min = Math.max(map[N][1], c) - Math.min(map[N][1], c);
			} else { // 동근 : 동
				// 동근이의 위치에서 반시계방향
				min = map[N][1] + X + c;
				min = Math.min(min, hap - min);
			}
		} else { // 상점 : 동
			if (map[N][0] == 1) { // 동근 : 북
				min = (X - map[N][1]) + c;
			} else if (map[N][0] == 2) { // 동근 : 남

				min = (X - map[N][1]) + (Y - c);
			} else if (map[N][0] == 3) { // 동근 : 서
				// 동근이의 위치에서 시계방향
				min = c + X + map[N][1];
				min = Math.min(min, hap - min);
			} else { // 동근 : 동
				min = Math.max(map[N][1], c) - Math.min(map[N][1], c);
				
			}
		}

		return min;
	}// go

}// class-end

'Algorithm > BOJ' 카테고리의 다른 글

BOJ 10157 자리배정  (0) 2021.03.08
BOJ 2527 직사각형  (0) 2021.03.08
BOJ 2563 색종이  (0) 2021.03.08
BOJ_2578_빙고  (0) 2021.03.05
BOJ 2605 줄세우기  (0) 2021.03.05