• 最优性减枝


    我们在搜索时常常会进行减枝操作,通常我们的减枝分为两种,一种是我们判断是否越界的可达性减枝,一种是能够抵达最终的答案,但是耗费的代价太过于高昂,我们将其进行最优性减枝。

    举个例子,我们要从乳山走到青岛,你如果乘坐汽车去,结果出了青岛,这是否就是远离我们得到目的地了,这个时候我们就将这条路线舍去,因为它超出我们的目的地了,这就是无法抵达目的地的可达性减枝。另一种情况是,我们到青岛一号路线是走500公里,二号路线是700公里,如果你选择二号路线当走到501公里时,你就会发现不合适,因为已经超出一号路线的花费了。所以这就是最优性减枝

    来个题把,考虑一下最优性的减枝。

    注意本题最优性减枝会在第一个点超时,我们设置ww标记点,一旦超过我们设定的次数就会结束程序。这题的最优其实是dfs

    P1135 奇怪的电梯 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStreamReader;
    4. import java.io.PrintWriter;
    5. import java.math.BigInteger;
    6. import java.math.MathContext;
    7. import java.util.ArrayList;
    8. import java.util.Arrays;
    9. import java.util.Collections;
    10. import java.util.Comparator;
    11. import java.util.Iterator;
    12. import java.util.LinkedList;
    13. import java.util.PriorityQueue;
    14. import java.util.Scanner;
    15. import java.util.TreeMap;
    16. import java.util.TreeSet;
    17. public class Main {
    18. public static void main(String[] args) throws NumberFormatException, IOException {
    19. Scanner sc=new Scanner(System.in);
    20. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    21. String[] aStrings=br.readLine().split(" ");
    22. bb=Integer.parseInt(aStrings[0]);
    23. start=Integer.parseInt(aStrings[1]);
    24. end=Integer.parseInt(aStrings[2]);
    25. String[] bStrings=br.readLine().split(" ");
    26. aa=new int[bb+1];
    27. check=new int[bb+1];
    28. int a;
    29. for(a=1;a<=bb;a++) {
    30. aa[a]=Integer.parseInt(bStrings[a-1]);
    31. }
    32. dfs(start, 0);
    33. check[start]=1;
    34. if(answer==Integer.MAX_VALUE){
    35. System.out.println("-1");
    36. return;
    37. }
    38. System.out.println(answer);
    39. }
    40. public static int answer=Integer.MAX_VALUE;//设为最大值
    41. public static int[] aa;//记录电梯的上升下降几楼
    42. public static int bb;//电梯的最大楼层
    43. public static int[] check;//这个层是否被到达过
    44. public static int start,end;
    45. public static int www=0;//设计dfs跑几次
    46. public static void dfs(int louceng,int a) {
    47. www++;
    48. if(www>=10000000) {
    49. return;//大于设定次数,结束dfs
    50. }
    51. if(louceng==end) {
    52. answer=Math.min(answer, a);//到达指定楼层记录
    53. }
    54. if(louceng<=0||louceng>bb) {//到达楼层不合理推退出
    55. return;
    56. }
    57. if(a>answer) {//当前答案大于已经搜到的的答案,推出
    58. return;
    59. }
    60. //System.out.println(a+" "+louceng);
    61. if(louceng+aa[louceng]>=1&&louceng+aa[louceng]<=bb&&check[louceng+aa[louceng]]!=1) {
    62. check[louceng+aa[louceng]]=1;//往上探索
    63. //System.out.println("AAA"+" "+bb);
    64. dfs(louceng+aa[louceng] ,a+1);
    65. check[louceng+aa[louceng]]=0;
    66. }
    67. if(louceng-aa[louceng]>=1&&louceng-aa[louceng]<=bb&&check[louceng-aa[louceng]]!=1) {
    68. check[louceng-aa[louceng]]=1;//往下探索
    69. //System.out.println("BB");
    70. dfs(louceng-aa[louceng], a+1);
    71. check[louceng-aa[louceng]]=0;
    72. }
    73. }
    74. }

  • 相关阅读:
    Spring Security认证之登录用户数据获取
    【JavaSE】继承和多态
    pinia 笔记
    uiautomator2遍历子元素.all()
    core-site.xml,yarn-site.xml,hdfs-site.xml,mapred-site.xml配置
    机器学习-特征选择:如何使用交叉验证精准选择最优特征?
    Netty(11)序列化/反序列化、Netty参数
    Hadoop面试重点
    Java缓存理解
    list(链表)
  • 原文地址:https://blog.csdn.net/m0_73862548/article/details/133095561