Advertisement
vov44k

t1748

Jan 26th, 2023 (edited)
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3.  
  4. public class t1748 {
  5.     static Scanner in = new Scanner(System.in);
  6.     static int start = in.nextInt();
  7.     static int end = in.nextInt();
  8.     static int [][] a = new int [end+1][end+1];
  9.     static int inf = Integer.MIN_VALUE;
  10.     static int [] dist = new int [end+1];
  11.     static LinkedList<Integer> queue = new LinkedList<Integer>();
  12.    
  13.     public static void array(int start, int end) {
  14.         for (int i = 1; i < end+1; i++) {
  15.             for (int j = 1; j < end+1; j++) {
  16.                 if(j == i+3 || j == i*4) {
  17.                     a[i][j] = 1;
  18.                 }
  19.             }
  20.         }
  21.     }
  22.    
  23.     public static void bfs(int s) {
  24.         dist[s]=0;
  25.         queue.addLast(s);
  26.         int v;
  27.        
  28.         while(queue.isEmpty() == false) {
  29.             v = queue.pollFirst();
  30.             for (int i = 1; i < end+1; i++) {
  31.                 if(a[v][i] == 1 && dist[i] == inf) {
  32.                     dist[i] = dist[v] + 1;
  33.                     queue.addLast(i);
  34.                 }
  35.             }
  36.            
  37.         }
  38.     }
  39.    
  40.     public static void main(String[] args) {
  41.         if(start > end)
  42.             System.out.println(-1);
  43.         else {
  44.         for (int i = 1; i < end+1; i++) {
  45.             dist[i] = inf;
  46.         }
  47.        
  48.         array(start, end);
  49.         bfs(start);
  50.        
  51.        
  52.         if(dist[end] != inf)
  53.         System.out.println(dist[end]);
  54.         else
  55.             System.out.println(-1);
  56.         }
  57. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement