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
- 자바
- 프로그래머스 연속된 수의 합 java
- java 내림
- Codility
- 백준 4375
- sort
- java 올림
- 백준 17425
- Arrays
- 네트워크
- java 반올림
- 백준 15661
- 알고리즘
- Math.floor()
- 백준 14391
- 백준 16927
- Algorithm
- 프로그래머스 옹알이 java
- 프로그래머스 도둑질 java
- 코딩테스트
- Math.ceil()
- 백준 16935
- 백준 18290
- 프로그래머스 네트워크 java
- time complexity
- 백준 11723
- java
- 프로그래머스 숫자의 표현 java
- mysql
- 0으로 채우기
Archives
- Today
- Total
취미처럼
[백준] 13549번 숨바꼭질3 본문
https://www.acmicpc.net/problem/13549
13549번: 숨바꼭질 3
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일
www.acmicpc.net
* 2 의 경우 이동 시간이 0초라서 증가하지 않으므로 다른 이동 보다 먼저 계산되어야 함
import java.util.*;
import java.io.*;
class Node {
int x;
int time;
public Node(int x, int time){
this.x = x;
this.time = time;
}
}
public class Main {
static int N, K;
static boolean[] visit = new boolean[100001];
static int time = Integer.MAX_VALUE;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
if(N == K) {
System.out.println(0);
} else {
bfs();
System.out.println(time);
}
}
public static void bfs() {
Queue<Node> q = new LinkedList<>();
q.offer(new Node(N, 0));
visit[N] = true;
while(!q.isEmpty()) {
Node node = q.poll();
if(node.x == K) {
time = Math.min(time, node.time);
}
int next;
next = node.x * 2;
if(next < 100001 && !visit[next]) {
q.offer(new Node(next, node.time));
visit[next] = true;
}
next = node.x -1;
if(next >= 0 && !visit[next]) {
q.offer(new Node(next, node.time + 1));
visit[next] = true;
}
next = node.x + 1;
if(next < 100001 && !visit[next]) {
q.offer(new Node(next, node.time + 1));
visit[next] = true;
}
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 16927번 배열 돌리기 2 (0) | 2021.03.15 |
---|---|
[백준] 1261번 알고스팟 (0) | 2021.03.15 |
[백준] 14226번 이모티콘 (0) | 2021.03.11 |
[백준] 13913번 숨바꼭질 4 (0) | 2021.03.11 |
[백준] 1697번 숨바꼭질 (0) | 2021.03.11 |
Comments