12851번: 숨바꼭질 2
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 때
www.acmicpc.net
package 백준.자바.숨바꼭질2_12851;
import java.io.*;
import java.util.*;
public class Main {
public void solution() throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] visited = new int[100001];
Queue<Pair> q = new LinkedList<>();
q.offer(new Pair(n, 0));
int min = Integer.MAX_VALUE;
int ways = 0;
if(n >= m) {
min = n-m;
ways = 1;
}
else {
while(!q.isEmpty()){
Pair p = q.poll();
if(p.pos < 0 || p.pos > 100000 || p.pos > m+(m-n)) continue;
if(p.pos == m){
if(p.count < min){
ways = 1;
min = p.count;
}
else if(p.count==min){
ways+=1;
}
}
if(visited[p.pos] == 0 || visited[p.pos] == p.count) {
visited[p.pos] = p.count;
q.add(new Pair(p.pos * 2, p.count + 1));
q.add(new Pair(p.pos + 1, p.count + 1));
q.add(new Pair(p.pos - 1, p.count + 1));
}
}
}
System.out.println(min);
System.out.println(ways);
}
class Pair{
int pos;
int count;
public Pair(int pos, int count){
this.pos = pos;
this.count = count;
}
}
public static void main(String[] args) throws Exception{
new Main().solution();
}
}
'문제풀이 > 백준' 카테고리의 다른 글
기타레슨 2343 Java (0) | 2024.01.25 |
---|---|
탈출 3055 Java (0) | 2024.01.22 |
치킨배달 15686 Java (1) | 2024.01.21 |
치즈 2638 Java (1) | 2024.01.21 |
수들의 합 1789 Java (0) | 2024.01.19 |