Advertisement
a_chn

Untitled

Feb 14th, 2024
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. // Question 2
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class a {
  6.  
  7.     private static boolean[][] visited;
  8.     private static ArrayList<HashSet<Integer>> bootsAccessible;
  9.  
  10.     private static class Boot {
  11.         private int s;
  12.         private int d;
  13.  
  14.         public Boot(int s, int d) {
  15.             this.s = s;
  16.             this.d = d;
  17.         }
  18.     }
  19.  
  20.     public static void main(String[] args) throws IOException {
  21.         BufferedReader br = new BufferedReader(new FileReader("snowboots.in"));
  22.         PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("snowboots.out")));
  23.         StringTokenizer st = new StringTokenizer(br.readLine());
  24.  
  25.         int n = Integer.parseInt(st.nextToken());
  26.         int b = Integer.parseInt(st.nextToken());
  27.  
  28.         int[] path = new int[n];
  29.         st = new StringTokenizer(br.readLine());
  30.  
  31.         for(int i = 0; i < n; i++) {
  32.             path[i] = Integer.parseInt(st.nextToken());
  33.         }
  34.  
  35.         Boot[] boots = new Boot[b];
  36.        
  37.         for(int i = 0; i < b; i++) {
  38.             st = new StringTokenizer(br.readLine());
  39.             int s = Integer.parseInt(st.nextToken());
  40.             int d = Integer.parseInt(st.nextToken());
  41.             boots[i] = new Boot(s, d);
  42.         }
  43.  
  44.         br.close();
  45.  
  46.         bootsAccessible = new ArrayList<HashSet<Integer>>();
  47.  
  48.         for(int i = 0; i < n; i++) {
  49.             bootsAccessible.add(new HashSet<Integer>());
  50.             for(int j = 0; j < b; j++) {
  51.                 if(boots[j].s >= path[i]) {
  52.                     bootsAccessible.get(i).add(j);
  53.                 }
  54.             }
  55.         }
  56.  
  57.         visited = new boolean[n][b];
  58.  
  59.         floodfill(0, 0, boots, n);
  60.  
  61.         int minBoots = 0;
  62.  
  63.         for(int i = 0; i < b; i++) {
  64.             if(visited[n-1][i]) {
  65.                 minBoots = i;
  66.                 break;
  67.             }
  68.         }
  69.  
  70.         pw.print(minBoots);
  71.         pw.close();
  72.     }
  73.  
  74.     public static void floodfill(int tileInd, int bootInd, Boot[] boots, int n) {
  75.         if(tileInd >= n || bootInd >= boots.length || visited[tileInd][bootInd]) {
  76.             return;
  77.         }
  78.         visited[tileInd][bootInd] = true;
  79.         for(int i = 1; i <= boots[bootInd].d; i++) {
  80.             if(tileInd + i < n && bootsAccessible.get(tileInd + i).contains(bootInd)) {
  81.                 floodfill(tileInd + i, bootInd, boots, n);
  82.             }
  83.         }
  84.         for(int i = bootInd + 1; i < boots.length; ++i) {
  85.             if(bootsAccessible.get(tileInd).contains(i)) {
  86.                 floodfill(tileInd, i, boots, n);
  87.             }
  88.         }
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement