[BOJ] 2564: 경비원 (JAVA)

2024. 5. 3. 02:21·코테/Algorithm
728x90

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

 

 

풀이

 

조건분기가 굉장히 많았던 문제

그래서 그냥 시작점부터 끝점까지의 거리를 냅다 구해줬다.

사실 공개하기 창피한 코드

ㅋ

 

전체코드
package 백준renew;

import java.io.*;
import java.util.*;

public class 실버1_2564_경비원 {
	static int[] reverse = {0, 3, 4, 1, 2};
	static int N;
	static int line[][];
	static int go[];
	static int dong[];
	static int ans;
	static int topDown, leftRight, all;
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		StringBuilder sb = new StringBuilder();
		
		//1은 북쪽 2는 남쪽 3은 서쪽 4는 동쪽 
		//북 남은 첫번째, 동 서는 두번째 숫자.
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		topDown = Integer.parseInt(st.nextToken()); // 1 3 가로 
		leftRight = Integer.parseInt(st.nextToken()); // 2 4 세로 
		all = topDown*2+leftRight*2;
		
		
		N = Integer.parseInt(br.readLine());
		
		line = new int[N][2];
		go = new int[N+1];
		Arrays.fill(go, Integer.MAX_VALUE);
		
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0; j<2; j++) {
				line[i][j] = Integer.parseInt(st.nextToken());
				if(line[i][0] == 3 || line[i][0] == 4) {
					line[i][1] = leftRight - line[i][1];
				}
			}
		}
		
		dong = new int[2];
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<2; i++) {
			dong[i] = Integer.parseInt(st.nextToken());
			if(dong[0] == 3 || dong[0] == 4) {
				dong[1] = leftRight - dong[1];
			}
		}
		ans = 0;
		
		Cal();
		
		sb.append(ans);
		bw.write(sb.toString());
		bw.close();
	}
	public static void Cal() {
		if(dong[0] == 1) {
			Dir1();
		}else if(dong[0]==3) {
			Dir3();
		}else if(dong[0]==2) {
			Dir2();
		}else {
			Dir4();
		}
	}
	public static void Dir1() {
		for(int i=0; i<N; i++) {
			int num = Integer.MAX_VALUE;
			if(line[i][0] == 1) {
				num = Math.min(num, Math.abs(dong[1] - line[i][1]));
			}else if(line[i][0] == 3) {
				num = Math.min(leftRight+dong[1]-line[i][1], all-(leftRight+dong[1]-line[i][1]));
			}else if(line[i][0] == 2) {
				num = Math.min(leftRight+dong[1]+line[i][1], all-(leftRight+dong[1]+line[i][1]));
			}else {
				num = Math.min(dong[1]+line[i][1]+leftRight+topDown, all-(dong[1]+line[i][1]+leftRight+topDown));
			}
			ans += num;
		}
	}
	public static void Dir3() {
		for(int i=0; i<N; i++) {
			int num = Integer.MAX_VALUE;
			if(line[i][0] == 1) {
				num = Math.min(leftRight-dong[1]+line[i][1], all-(leftRight-dong[1]+line[i][1]));
			}else if(line[i][0] == 3) {
				num = Math.min(num, Math.abs(dong[1] - line[i][1]));
			}else if(line[i][0] == 2) {
				num = Math.min(dong[1]+line[i][1], all-(dong[1]+line[i][1]));
			}else {
				num = Math.min(dong[1]+line[i][1]+topDown, all-(dong[1]+line[i][1]+topDown));
			}
			ans += num;
		}
	}
	public static void Dir2() {
		for(int i=0; i<N; i++) {
			int num = Integer.MAX_VALUE;
			if(line[i][0] == 1) {
				num = Math.min(leftRight+dong[1]+line[i][1], all-(leftRight+dong[1]+line[i][1]));
			}else if(line[i][0] == 3) {
				num = Math.min(dong[1]+line[i][1], all-(dong[1]+line[i][1]));
			}else if(line[i][0] == 2) {
				num = Math.min(num, Math.abs(dong[1] - line[i][1]));
			}else {
				num = Math.min(line[i][1]-dong[1]+topDown, all-(line[i][1]-dong[1]+topDown));
			}
			ans += num;
		}
	}
	public static void Dir4() {
		for(int i=0; i<N; i++) {
			int num = Integer.MAX_VALUE;
			if(line[i][0] == 1) {
				num = Math.min(topDown+leftRight+dong[1]+line[i][1], all-(topDown+leftRight+dong[1]+line[i][1]));
			}else if(line[i][0] == 3) {
				num = Math.min(dong[1]+line[i][1]+topDown, all-(dong[1]+line[i][1]+topDown));
			}else if(line[i][0] == 2) {
				num = Math.min(topDown-line[i][1]+dong[1], all-(topDown-line[i][1]+dong[1]));
			}else {
				num = Math.min(num, Math.abs(dong[1] - line[i][1]));
			}
			ans += num;
		}
	}
}
728x90

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

[BOJ] 2839: 설탕배달 (JAVA)  (0) 2024.05.05
[BOJ] 1915: 가장 큰 정사각형 (JAVA)  (0) 2024.05.04
[BOJ] 16173: 점프왕 쩰리(Small) (JAVA)  (0) 2024.05.02
[Programmers] 잡은 물고기 중 가장 큰 물고기의 길이 구하기 (MySQL)  (0) 2024.05.01
[BOJ] 1051: 숫자 정사각형 (JAVA)  (0) 2024.04.24
'코테/Algorithm' 카테고리의 다른 글
  • [BOJ] 2839: 설탕배달 (JAVA)
  • [BOJ] 1915: 가장 큰 정사각형 (JAVA)
  • [BOJ] 16173: 점프왕 쩰리(Small) (JAVA)
  • [Programmers] 잡은 물고기 중 가장 큰 물고기의 길이 구하기 (MySQL)
DROPDEW
DROPDEW
💻 Developer | 기록하지 않으면 존재하지 않는다
  • DROPDEW
    제 2장 1막
    DROPDEW
  • 전체
    오늘
    어제
    • Dev (410) N
      • App·Android (1)
      • BE (42) N
        • HTTP 웹 기본 지식 (8)
        • 스프링 입문 - 코드로 배우는 스프링 부트, 웹 .. (12)
        • 스프링부트와 JPA 활용 (9) N
        • 스프링부트 시큐리티 & JWT (0)
        • PHP (6)
      • FE·Client (23)
        • HTML (1)
        • React (19)
        • Unity (1)
      • Data (12)
        • AI (4)
        • Bigdata (6)
        • Database (1)
        • 빅데이터분석기사 (0)
      • Infra (0)
      • Activity (0)
        • Education (0)
        • Intern (0)
        • 리모트 인턴십 6기 (0)
        • 구름톤 유니브 4기 (0)
        • SW교육기부단 15기 (0)
      • CS (8) N
      • 취준 (13)
        • 자격증 (4)
        • 인적성·NCS (6)
        • 코테·필기·면접 후기 (3)
      • 코테 (270)
        • Algorithm (222)
        • SQL (35)
        • 정리 (13)
      • 인사이트 (27)
        • 회고 (0)
        • 금융경제뉴스 (7)
        • 금융용어·지식 (2)
        • 북마크 (7)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    누적합
    오블완
    정렬
    문자열
    다이나믹프로그래밍
    최단경로
    브루트포스 알고리즘
    티스토리챌린지
    구현
    백준
    시뮬레이션
    그래프이론
    그리디알고리즘
    매개변수탐색
    너비우선탐색
    수학
    이분탐색
    투포인터
    자료구조
    그래프탐색
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
DROPDEW
[BOJ] 2564: 경비원 (JAVA)
상단으로

티스토리툴바