티스토리 뷰
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
import java.io.*;
import java.util.*;
public class boj_7562 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int[] dx = {2, 1, -1, -2, -2, -1, 1, 2};
static int[] dy = {1, 2, 2, 1, -1, -2, -2, -1};
static int t, l, nx, ny;
public static void main(String[] args) throws IOException {
t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
l = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
Dot start = new Dot(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), 0);
st = new StringTokenizer(br.readLine());
Dot end = new Dot(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), 0);
System.out.println(bfs(l, start, end));
}
}
private static int bfs(int l, Dot start, Dot end) {
Queue<Dot> q = new LinkedList<>();
boolean[][] v = new boolean[l][l];
int result = 0;
v[start.x][start.y] = true;
q.offer(start);
if (start.x == end.x && start.y == end.y) {
return result;
}
while (!q.isEmpty()) {
Dot now = q.poll();
int day = now.d;
for (int j = 0; j < 8; j++) {
nx = now.x + dx[j];
ny = now.y + dy[j];
if (nx < 0 || nx >= l || ny < 0 || ny >= l) continue;
if (nx == end.x && ny == end.y) {
return day + 1;
}
if (!v[nx][ny]) {
v[nx][ny] = true;
q.offer(new Dot(nx, ny, day + 1));
}
}
}
return result;
}
}
class Dot {
int x;
int y;
int d;
public Dot(int x, int y, int d) {
this.x = x;
this.y = y;
this.d = d;
}
}
|
cs |
- BFS
- Dot 클래스에 day를 같이 넣어서 움직이게 하는 방식으로 풀이
반응형
'PS > boj' 카테고리의 다른 글
boj)2250 - 트리의 높이와 너비 (0) | 2020.12.12 |
---|---|
boj)1991 - 트리 순회 (0) | 2020.12.11 |
boj)11723 - 집합 (0) | 2020.12.09 |
boj)14391 - 종이 조각 (0) | 2020.12.09 |
boj)2529 - 부등호 (0) | 2020.12.08 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- HTTP 완벽가이드
- js api
- Spring Security
- 집 구하기
- 프로그래머스 SQL
- 백기선 스터디
- HTTP 완벽 가이드
- 패스트캠퍼스 컴퓨터공학 완주반
- java
- JPA 연관관계 매핑
- REST API
- BOJ
- 김영한 JPA
- GCP
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- 킹수빈닷컴
- 이펙티브자바 아이템60
- 김영한 http
- 프로그래머스
- 이펙티브자바
- http
- dreamcoding
- 이펙티브자바 아이템59
- JS 딥다이브
- 드림코딩
- 모던자바스크립트
- 이펙티브자바 스터디
- js promise
- js array
- 백준
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함