Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- class Nodo {
- int x, nivel;
- Nodo(int x, int nivel) {
- this.x = x;
- this.nivel = nivel;
- }
- }
- public class AER319 {
- public static int bfs(Nodo root, int fin) {
- Nodo temp = root;
- Queue<Nodo> cola = new LinkedList<>();
- cola.add(temp);
- HashSet<Integer> respuestas = new HashSet<>();
- respuestas.add(temp.x);
- while(temp.x != fin) {
- temp = cola.remove();
- Nodo h1 = new Nodo((temp.x+1) % 10000, temp.nivel+1);
- if (!respuestas.contains(h1.x)){
- cola.add(h1);
- respuestas.add(h1.x);
- }
- Nodo h2 = new Nodo((temp.x*2) % 10000, temp.nivel+1);
- if (!respuestas.contains(h2.x)){
- cola.add(h2);
- respuestas.add(h2.x);
- }
- Nodo h3 = new Nodo((temp.x/3) % 10000, temp.nivel+1);
- if (!respuestas.contains(h3.x)){
- cola.add(h3);
- respuestas.add(h3.x);
- }
- }
- return temp.nivel;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (sc.hasNext()){
- int inicio = sc.nextInt();
- int fin = sc.nextInt();
- Nodo padre = new Nodo(inicio, 0);
- int sol = bfs(padre, fin);
- System.out.println(sol);
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement