DFS之迭代加深、双向DFS、IDA*

迭代加深

170. 加成序列 - AcWing题库

剪枝1:优先枚举较大的数

剪枝2:排除等效冗余

import java.util.*;

public class Main{
    static int N = 110, n;
    static int[] path = new int[N];
    static boolean[] st = new boolean[N];
    
    //当前是第几层,最大层数
    public static boolean dfs(int u, int length){
        if(u == length) return path[u - 1] == n;
        
        Arrays.fill(st, false);//每次都要重置,用于冗余性剪枝
        for(int i = u - 1; i >= 0; i --){//从大到小开始枚举,优化搜索顺序
            for(int j = i; j >= 0; j --){
                int s = path[i] + path[j];
                if(s <= n && s > path[u - 1] && !st[s]){
                    st[s] = true;
                    path[u] = s;
                    if(dfs(u + 1, length)) return true;
                    
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(true){
            n = sc.nextInt();
            if(n == 0) break;
            
            path[0] = 1;
            int length = 1;//每次往下的深度
            while(!dfs(1, length)) length ++;
            
            for(int i = 0; i < length; i ++){
                System.out.print(path[i] + " ");
            }
            System.out.println();
        }
    }
}

双向DFS

171. 送礼物 - AcWing题库

import java.util.*;

public class Main{
    //cnt表示前半段的方案数
    static int N = 50, n, m, cnt, k;
    static int[] w = new int[N];
    static int[] weights = new int[1 << 25];//前半段不同的重量组合
    static long ans;//一次能搬运的最大物品重量之和
    
    //翻转数组,经过排序之后,这个函数令从小到大排序变成从大到小排序
    public static void reverse(){
        for(int i = 0; i < n / 2; i ++){
            int temp = w[i];
            w[i] = w[n - 1 - i];
            w[n - 1 - i] = temp;
        }
    }
    
    //数组去重后得到的元素数量
    public static int unique(){
        int t = 0;
        for(int i = 0; i < cnt; i ++){
            if(i == 0 || weights[i] != weights[i - 1]) weights[t ++] = weights[i];
        }
        return t;
    }
    
    //前半段的所有方案:当前枚举到第u个数,总重量为s
    public static void dfs1(int u, int s){
        if(u >= k){
            weights[cnt ++] = s;
            return;
        }
        
        //选当前这个数
        if((long)s + w[u] <= m) dfs1(u + 1, s + w[u]);
        
        //不选当前这个数
        dfs1(u + 1, s);
    }
    
    //遍历第二段,寻找第一段中是否有合适的方案值
    public static void dfs2(int u, int s){
        if(u >= n){
            //二分查找第一段方案与第二段方案加起来小于m的最大值
            int l = 0, r = cnt - 1;
            while(l < r){
                int mid = (l + r + 1) >> 1;
                if((long)weights[mid] + s <= m) l = mid;
                else r = mid - 1;
            }
            if((long)weights[l] + s <= m) ans = Math.max(ans, weights[l] + s);
            return;
        }
        
        //选当前这个数
        if((long)s + w[u] <= m) dfs2(u + 1, s + w[u]);
        
        //不选当前这个数
        dfs2(u + 1, s);
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();//力气最大值
        n = sc.nextInt();//礼物数
        
        for(int i = 0; i < n; i ++){
            w[i] = sc.nextInt();//每件礼物的重量
        }
        
        Arrays.sort(w, 0, n);//排序
        reverse();//翻转
        
        k = n / 2 + 2;//前半段的数
        
        dfs1(0, 0);//找出前半段重量的所有方案,第0个数开始,一开始总重量为0
        
        Arrays.sort(weights, 0, cnt);//排序
        cnt = unique();//前半段方案去重后的数量
        
        dfs2(k, 0);
        System.out.print(ans);   
    }
}

 

IDA*

基本思想就是剪枝:对未来所需要的步数进行一个估计(估计值<=真实值) 

180. 排书 - AcWing题库

 

 java的arrayCopy用法_static void arraycopy用法-CSDN博客

Arrays.copyOf() 用法-CSDN博客 

import java.util.*;

public class Main{
    static int N = 15, n;
    static int[] q = new int[N];//书的摆放顺序
    static int[][] w = new int[5][N];//每层的复原数组
    
    //求出错误后继的个数,然后由于每次调换最多修正3个错误后继
    public static int f(){
        int tot = 0;
        for(int i = 0; i < n - 1; i ++){
            if(q[i + 1] != q[i] + 1) tot ++;
        }
        return (tot + 2) / 3;
    }
    
    public static boolean dfs(int u, int depth){
        if(u + f() > depth) return false;//当前层数加估计层数大于最大层数
        if(f() == 0) return true;//错误后继为0
        
        for(int len = 1; len <= n; len ++){
            for(int l = 0; l + len - 1 < n; l ++){
                int r = l + len - 1;
                for(int k = r + 1; k < n; k ++){//移到k的后面
                    w[u] = Arrays.copyOf(q, N);//拷贝
                    
                    int y = l;
                    for(int x = r + 1; x <= k; x ++) q[y ++] = w[u][x];
                    for(int x = l; x <= r; x ++) q[y ++] = w[u][x];
                    if(dfs(u + 1, depth)) return true;
                    
                    q = Arrays.copyOf(w[u], N);//恢复现场
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while(T -- > 0){
            n = sc.nextInt();
            for(int i = 0; i < n; i ++){
                q[i] = sc.nextInt();
            }
            
            int depth = 0;//这里是因为w数组从0开始
            //迭代加深
            while(depth < 5 && !dfs(0, depth)) depth ++;
            
            if(depth >= 5) System.out.println("5 or more");
            else System.out.println(depth);
        }
    }
}

 

181. 回转游戏 - AcWing题库

 

import java.io.*;

public class Main{
    static int N = 24;
    static int[] q = new int[N];//棋盘状态
    //棋盘每个位置的编号
    static int[][] op = {
        {0, 2, 6, 11, 15, 20, 22},
        {1, 3, 8, 12, 17, 21, 23},
        {10, 9, 8, 7, 6, 5, 4},
        {19, 18, 17, 16, 15, 14, 13},
        {23, 21, 17, 12, 8, 3, 1},
        {22, 20, 15, 11, 6, 2, 0},
        {13, 14, 15, 16, 17, 18, 19},
        {4, 5, 6, 7, 8, 9, 10},
    };
    //中间8个数的编号
    static int[] center = {6, 7, 8, 11, 12, 15, 16, 17};
    //每个操作的反向操作
    static int[] opposite = {5, 4, 7, 6, 1, 0, 3, 2};
    //操作方案
    static int[] path = new int[100];
    
    //估价函数,找到中间八个格子中最多的数
    public static int f(){
        int[] sum = new int[4];
        for(int i = 0; i < 8; i ++) sum[q[center[i]]] ++;
        
        int s = 0;
        for(int i = 1; i <= 3; i ++) s = Math.max(s, sum[i]);
        return 8 - s;//最少还要进行的操作的数量
    }
    
    //移动函数
    public static void operation(int x){//x表示第几个操作
        int t = q[op[x][0]];
        for(int i = 0; i < 6; i ++) q[op[x][i]] = q[op[x][i + 1]];
        q[op[x][6]] = t;
    }
    
    public static boolean dfs(int u, int depth, int last){
        if(u + f() > depth) return false;
        if(f() == 0) return true;
        
        for(int i = 0; i < 8; i ++){
            if(opposite[i] == last) continue;//如果是反向操作,立即停止
            operation(i);
            path[u] = i;
            if(dfs(u + 1, depth, i)) return true;
            operation(opposite[i]);
        }
        return false;
    }
    
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String cur = "";
        while(!(cur = br.readLine()).equals("0")){
            //题目中说了每个测试用例占一行
            String[] arr = cur.split(" ");
            for(int i = 0; i < 24; i ++) q[i] = Integer.parseInt(arr[i]);
            
            int depth = 0;
            int last = -1;//上一次的操作
            while(!dfs(0, depth, last)) depth ++;
            
            if(depth == 0) System.out.println("No moves needed");
            else{
                for(int i = 0; i < depth; i ++) System.out.print((char)(path[i] + 'A'));
                System.out.println();
            }
            //无论哪种情况都要输出中间格子的数
            System.out.println(q[center[0]]);
        }
    }
}

 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值