Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- DFS
- 모르고리즘
- BFS
- HAVE A GOOD DAY
- 우유가 옆으로 넘어지면 아야
- amazon
- Hamming weight
- 아자아자 화이팅
- Have a good day :)
- Java 환경 설정
- 자료구조
- LeetCode #릿코드 #좋은 하루 되세요 #Have a nice day
- 코로나 싫어요
- 네트워크
- SSAFY 10기 화이팅
- 텐션 업 10기 화이팅
- Have a nice day.
- SSAFY 화이팅
- I am Korean
- have a nice day
- SeongSeobDang
- 수학
- 텐션 업 10기!
- SSAFY IM/A
- 우유아야
- 자고 싶다
- SSAFY 테스트
- DP
- 우유가옆으로넘어지면아야
Archives
- Today
- Total
Hope Everyone Is Happy
[실버1] 7562. 나이트의 이동 (Java) 본문
※ 백준 (Baekjoon)/[Java] 문제 풀이 ( Solve the problems)
[실버1] 7562. 나이트의 이동 (Java)
J 크 2023. 9. 25. 22:14728x90
반응형
https://www.acmicpc.net/problem/7562
7562번: 나이트의 이동
체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수
www.acmicpc.net
왤케 체스를 좋아해,,
※ 문제를 요약하면 아래와 같습니다.
▶ NxN 체스판에서 어떤 나이트의 현재 위치와 목표 위치가 주어 졌을 때, 최소로 움직여 도달할 수 있는 횟수 구하기
▶ Input : 첫째 줄엔 테스트 케이스 경우 입력, 각 테스트 별 첫째 줄엔 N, 다음 줄에 현재 위치, 다음 줄에 목표 위치 입력
( 위치는 (Row, Col)의 0 부터 시작하는 좌표값 )
▶ Output : 각 테스트 케이스 별로 나이트가 최소 몇 번만에 이동할 수 있는지 출력
◈ Input - 1
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
◈ Output - 1
5
28
0
◎ 코드 작성 전, 아래와 같이 솔루션을 정리하였습니다.
▶ 최소 횟수 탐색 == BFS
▶ 방문 처리 필수
▶ BFS에서 델타 탐색을 상,하,좌,우 가아닌 실제 체스에서 말이 이동 가능한 경로를 탐색 (가로2 세로1 or 가로1 세로2)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
class Point {
int m_nRow;
int m_nCol;
int m_nCount;
public Point(int m_nRow, int m_nCol, int m_nCount) {
super();
this.m_nRow = m_nRow;
this.m_nCol = m_nCol;
this.m_nCount = m_nCount;
}
}
public class Main {
static boolean[][] barrVisited;
static int N, nStartRow, nStartCol, nEndRow, nEndCol, nResult;
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(bReader.readLine());
for (int i = 0; i < T; i++) {
N = Integer.parseInt(bReader.readLine());
// 맵 크기 설정 하자
barrVisited = new boolean[N][N];
StringTokenizer st = new StringTokenizer(bReader.readLine());
nStartRow = Integer.parseInt(st.nextToken());
nStartCol = Integer.parseInt(st.nextToken());
st = new StringTokenizer(bReader.readLine());
nEndRow = Integer.parseInt(st.nextToken());
nEndCol = Integer.parseInt(st.nextToken());
nResult = 0;
BFS();
bWriter.write(String.valueOf(nResult) + '\n');
bWriter.flush();
}
bWriter.close();
}
private static void BFS() {
Queue<Point> queueBFS = new LinkedList<>();
queueBFS.add(new Point(nStartRow, nStartCol, 0));
// 나이트 움직임 만큼 가자
// 북동 부터 북서로 시계방향
int[] dr = { 2, 1, -1, -2, -2, -1, 1, 2 };
int[] dc = { 1, 2, 2, 1, -1, -2, -2, -1 };
while (!queueBFS.isEmpty()) {
Point p = queueBFS.poll();
if(p.m_nRow == nEndRow && p.m_nCol == nEndCol) {
nResult = p.m_nCount;
queueBFS.clear();
break;
}
if(barrVisited[p.m_nRow][p.m_nCol])
continue;
barrVisited[p.m_nRow][p.m_nCol] = true;
for(int i = 0; i < dr.length; i++) {
int nSearchRow = p.m_nRow + dr[i];
int nSearchCol = p.m_nCol + dc[i];
// 범위 벗어나거나 방문했으면 나가버려
if(nSearchRow < 0 || nSearchCol < 0 || nSearchRow >= N || nSearchCol >= N
|| barrVisited[nSearchRow][nSearchCol])
continue;
queueBFS.add(new Point(nSearchRow, nSearchCol, p.m_nCount+1));
}
}
}
}
Good Luck! (피드백 감사합니다!)
'※ 백준 (Baekjoon) > [Java] 문제 풀이 ( Solve the problems)' 카테고리의 다른 글
[골드3] 17135. 캐슬 디펜스(Java) (0) | 2023.09.28 |
---|---|
[골드5] 7562. 파이프 옮기기(Java) (0) | 2023.09.27 |
[골드4] 4485. 녹색 옷 입은 애가 젤다지? (Java) (2) | 2023.09.25 |
[실버2] 11060. 점프 점프 (Java) (0) | 2023.09.25 |
[실버3] 15654. N과M(5) (Java) (0) | 2023.09.25 |