[BOJ] 16948: 데스나이트 (JAVA)

2024. 3. 10. 00:13·코테/Algorithm
728x90
 

16948번: 데스 나이트

게임을 좋아하는 큐브러버는 체스에서 사용할 새로운 말 "데스 나이트"를 만들었다. 데스 나이트가 있는 곳이 (r, c)라면, (r-2, c-1), (r-2, c+1), (r, c-2), (r, c+2), (r+2, c-1), (r+2, c+1)로 이동할 수 있다. 크

www.acmicpc.net

package 백준renew;

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

public class 실버1_16948_데스나이트 {
	static int goX[] = {-2, -2, 0, 0, 2, 2};
	static int goY[] = {-1, 1, -2, 2, -1, 1};
	static int N, ans;
	static boolean visited[][];
	static int startX, startY, endX, endY;
	static class Jump{
		int x;
		int y;
		int cnt;
		Jump(int x, int y, int cnt){
			this.x = x;
			this.y = y;
			this.cnt = cnt;
		}
	}
 	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();
		
		N = Integer.parseInt(br.readLine());
		ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		startX = Integer.parseInt(st.nextToken());
		startY = Integer.parseInt(st.nextToken());
		endX = Integer.parseInt(st.nextToken());
		endY = Integer.parseInt(st.nextToken());
		
		visited = new boolean[N][N];
		BFS(startX, startY);
		
		if(ans == 0) {
			sb.append(-1);
		}else {
			sb.append(ans);
		}
		
		bw.write(sb.toString());
		bw.flush();
		bw.close();
		
	}
 	static void BFS(int x, int y) {
 		Queue<Jump> queue = new LinkedList<>();
 		queue.offer(new Jump(x, y, 0));
 		visited[x][y] = true;
 		
 		while(!queue.isEmpty()) {
 			Jump j = queue.poll();
 			
 			if(j.x == endX && j.y == endY) {
 				ans = j.cnt;
 				return;
 			}
 			
 			for(int i=0; i<6; i++) {
 				int newX = j.x + goX[i];
 				int newY = j.y + goY[i];
 				
 				if(newX < 0 || newX >= N || newY < 0 || newY >= N || visited[newX][newY]) {
 					continue;
 				}
 				
 				queue.offer(new Jump(newX, newY, j.cnt+1));
 				visited[newX][newY] = true;
 			}
 		}
 	}
}
728x90

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

[BOJ] 10815: 숫자 카드 (JAVA)  (0) 2024.03.11
[BOJ] 1920: 수 찾기 (JAVA)  (0) 2024.03.11
[BOJ] 9655: 돌 게임 (JAVA)  (0) 2024.03.09
[BOJ] 1343: 폴리오미노 (JAVA)  (0) 2024.03.08
[BOJ] 19638: 센티와 마법의 뿅망치 (JAVA)  (0) 2024.03.08
'코테/Algorithm' 카테고리의 다른 글
  • [BOJ] 10815: 숫자 카드 (JAVA)
  • [BOJ] 1920: 수 찾기 (JAVA)
  • [BOJ] 9655: 돌 게임 (JAVA)
  • [BOJ] 1343: 폴리오미노 (JAVA)
DROPDEW
DROPDEW
💻 Developer | 기록하지 않으면 존재하지 않는다
  • DROPDEW
    제 2장 1막
    DROPDEW
  • 전체
    오늘
    어제
    • Dev (417)
      • App·Android (1)
      • BE (44)
        • HTTP 웹 기본 지식 (8)
        • 스프링 입문 - 코드로 배우는 스프링 부트, 웹 .. (12)
        • 스프링부트와 JPA 활용 (11)
        • 스프링부트 시큐리티 & JWT (0)
        • PHP (6)
      • FE·Client (23)
        • HTML (1)
        • React (19)
        • Unity (1)
      • Data (17)
        • AI (7)
        • Bigdata (6)
        • Database (1)
        • 빅데이터분석기사 (2)
      • Infra (0)
      • Activity (0)
        • Education (0)
        • Intern (0)
        • 리모트 인턴십 6기 (0)
        • 구름톤 유니브 4기 (0)
        • SW교육기부단 15기 (0)
      • CS (8)
      • 취준 (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] 16948: 데스나이트 (JAVA)
상단으로

티스토리툴바