• 洛谷 P1135 奇怪的电梯 P1135 Java


    提供两种思路

    第一种DFS

    超时第九和第十点

    1. import java.util.*;
    2. import java.io.*;
    3. public class Main{
    4. static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    5. static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    6. static int N,A,B,INF;
    7. static int[] lift;
    8. static int[] distance;
    9. static boolean[] judge;
    10. public static void main(String[] args) throws IOException {
    11. String[] S = br.readLine().split(" ");
    12. N = Integer.parseInt(S[0]);
    13. A = Integer.parseInt(S[1]);
    14. B = Integer.parseInt(S[2]);
    15. INF = 0x3f3f3f3f;
    16. lift = new int[N+1];
    17. judge = new boolean[N+1];
    18. distance = new int[N+1];
    19. Arrays.fill(distance,INF);
    20. S = br.readLine().split(" ");
    21. for(int i = 0 ; i < S.length ; i++) {
    22. lift[i+1] = Integer.parseInt(S[i]);
    23. }
    24. DFS(A , 0);
    25. if(distance[B] == 0x3f3f3f3f) {
    26. out.write("-1");
    27. }
    28. else {
    29. out.write(distance[B] + "");
    30. }
    31. out.flush();
    32. out.close();
    33. br.close();
    34. }
    35. private static void DFS(int layer , int count) {
    36. if(layer == B) {
    37. distance[B] = Math.min(distance[B], count);
    38. return ;
    39. }
    40. if(layer + lift[layer] <= N && !judge[layer+lift[layer]]) {
    41. judge[layer+lift[layer]] = true;
    42. count++;
    43. layer += lift[layer];
    44. DFS(layer , count);
    45. count--;
    46. layer -= lift[layer];
    47. judge[layer+lift[layer]] = false;
    48. }
    49. if(layer - lift[layer] >= 1 && !judge[layer - lift[layer]]) {
    50. judge[layer-lift[layer]] = true;
    51. count++;
    52. layer -= lift[layer];
    53. DFS(layer , count);
    54. count--;
    55. layer += lift[layer];
    56. judge[layer-lift[layer]] = false;
    57. }
    58. }
    59. }

    第二种BFS

    AC

    1. import java.util.*;
    2. import java.io.*;
    3. public class Main{
    4. static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    5. static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    6. static int N,A,B;
    7. static int[] lift;
    8. static boolean[] judge;
    9. public static void main(String[] args) throws IOException {
    10. String[] S = br.readLine().split(" ");
    11. N = Integer.parseInt(S[0]);
    12. A = Integer.parseInt(S[1]);
    13. B = Integer.parseInt(S[2]);
    14. lift = new int[N+1];
    15. judge = new boolean[N+1];
    16. S = br.readLine().split(" ");
    17. for(int i = 0 ; i < S.length ; i++) {
    18. lift[i+1] = Integer.parseInt(S[i]);
    19. }
    20. BFS();
    21. out.flush();
    22. out.close();
    23. br.close();
    24. }
    25. private static void BFS() throws IOException {
    26. Point first = new Point();
    27. first.index = A;
    28. first.layer = 0;
    29. Deque deque = new LinkedList<>();
    30. deque.offer(first);
    31. Point result = first;
    32. while(!deque.isEmpty()) {
    33. result = deque.poll();
    34. if(result.index == B) {
    35. break;
    36. }
    37. if(result.index + lift[result.index] <= N && !judge[result.index + lift[result.index]]) {
    38. Point node = new Point();
    39. node.layer = result.layer + 1;
    40. node.index = result.index + lift[result.index];
    41. judge[result.index + lift[result.index]] = true;
    42. deque.offer(node);
    43. }
    44. if(result.index - lift[result.index] >= 1 && !judge[result.index - lift[result.index]]) {
    45. Point node = new Point();
    46. node.layer = result.layer + 1;
    47. node.index = result.index - lift[result.index];
    48. judge[result.index - lift[result.index]] = true;
    49. deque.offer(node);
    50. }
    51. }
    52. if(result.index == B) {
    53. out.write(result.layer + "");
    54. }
    55. else {
    56. out.write("-1");
    57. }
    58. }
    59. }
    60. class Point{
    61. int layer;
    62. int index;
    63. }

  • 相关阅读:
    学习记录:js算法(三十五):K 个一组翻转链表
    Windows下Mosquitto服务配置监听任何IP,搭配使用MQTTX
    Android 自定义view 圆形进度条
    通过通达信l2行情接口的逐笔委托中挖掘主力踪迹
    团队的效率在于规范和沟通,而不仅仅在于技术
    windows 驱动与内核调试 学习3
    十大字体设计网站年终盘点:顶级设计师独家推荐
    Transformers in Remote Sensing: A Survey
    1027 Colors in Mars
    uni-app 5小时快速入门 14 uni-app语法(上)
  • 原文地址:https://blog.csdn.net/Gengar021127/article/details/133844183