fork download
  1. import java.util.*;
  2. import java.util.stream.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. // 读取输入
  8. String[] nm = sc.nextLine().split(",");
  9. int n = Integer.parseInt(nm[0]);
  10. int m = Integer.parseInt(nm[1]);
  11. int[] hams = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
  12. int[] costs = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
  13. int k = Integer.parseInt(sc.nextLine());
  14.  
  15. // 计算每秒法力恢复值
  16. int recover = Math.max(1, m / 10);
  17.  
  18. // BFS队列:存储状态 [时间t, 当前法力currentM, Boss剩余血量currentK]
  19. Queue<int[]> queue = new LinkedList<>();
  20. queue.offer(new int[]{0, m, k});
  21. // 记录已访问的状态(避免重复计算)
  22. Set<String> visited = new HashSet<>();
  23. visited.add(m + "," + k);
  24.  
  25. while (!queue.isEmpty()) {
  26. int[] curr = queue.poll();
  27. int t = curr[0];
  28. int currentM = curr[1];
  29. int currentK = curr[2];
  30.  
  31. // 尝试释放每个技能
  32. for (int i = 0; i < n; i++) {
  33. int cost = costs[i];
  34. int harm = hams[i];
  35. if (currentM >= cost) {
  36. int newK = currentK - harm;
  37. // Boss血量≤0,击杀成功
  38. if (newK <= 0) {
  39. System.out.println(t + 1);
  40. return;
  41. }
  42. // 修正:释放技能后先消耗法力,经过1秒后再恢复法力
  43. // 先计算释放技能后的法力(不立即恢复)
  44. int newMAfterCast = currentM - cost;
  45. // 再计算1秒后的恢复(符合时间流逝逻辑)
  46. int newM = newMAfterCast + recover;
  47. newM = Math.min(newM, m); // 不超过最大法力
  48. String key = newM + "," + newK;
  49. if (!visited.contains(key)) {
  50. visited.add(key);
  51. queue.offer(new int[]{t + 1, newM, newK});
  52. }
  53. }
  54. }
  55.  
  56. // 尝试等待(耗时1秒,恢复法力)
  57. int newMWait = currentM + recover;
  58. newMWait = Math.min(newMWait, m);
  59. String keyWait = newMWait + "," + currentK;
  60. if (!visited.contains(keyWait)) {
  61. visited.add(keyWait);
  62. queue.offer(new int[]{t + 1, newMWait, currentK});
  63. }
  64. }
  65.  
  66. // 无法击杀Boss
  67. System.out.println(-1);
  68. }
  69. }
  70.  
Success #stdin #stdout 0.16s 59052KB
stdin
2,25
10,15
10,15
25
stdout
2