DFS中的连通性和搜索顺序

文章详细介绍了在AcWing题库中,如何使用深度优先搜索(DFS)解决迷宫路径、红与黑状态判断、马走日游戏、单词接龙以及将数字分成互质组等问题,展示了DFS算法在不同场景的应用和实现方式。
摘要由CSDN通过智能技术生成

宽搜一般要手写一个队列,深搜一般是用系统栈来实现的。

DFS之连通性模型

 

1112. 迷宫 - AcWing题库 

import java.util.*;

public class Main{
    static int N = 110, ha, la, hb, lb, n;
    static char[][] g = new char[N][N];
    static boolean[][] st = new boolean[N][N];
    static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
    
    public static boolean dfs(int start, int end){
        
        if(g[start][end] == '#') return false;//如果一开始起点就是障碍物
        if(start == hb && end == lb) return true;//如果遍历到终点
        
        st[start][end] = true;//标记为已搜过
        
        for(int i = 0; i < 4; i ++){
            int a = start + dx[i];
            int b = end + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= n) continue;//越界
            if(!st[a][b]){//如果这个点没走过
                if(dfs(a, b)) return true;
            }
        }
        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 ++){
                String s = sc.next();
                for(int j = 0; j < n; j ++){
                    g[i][j] = s.charAt(j);
                    st[i][j] = false;//由于有多组数组,所以每一次都要重置为false
                }
            }
            
            ha = sc.nextInt();
            la = sc.nextInt();
            hb = sc.nextInt();
            lb = sc.nextInt();
            
            if(dfs(ha, la)) System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

 

1113. 红与黑 - AcWing题库

import java.util.*;

public class Main{
    static int N = 25;
    static int n, m, cnt;
    static char[][] g = new char[N][N];
    static boolean[][] st = new boolean[N][N];
    static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};

    public static void dfs(int x, int y){
        st[x][y] = true;
        cnt ++;

        for(int i = 0; i < 4; i ++){
            int a = x + dx[i];
            int b = y + dy[i];
            if(a < 0 || b < 0 || a >= n || b >= m) continue;
            if(st[a][b]) continue;
            if(g[a][b] == '#') continue;

            dfs(a, b);
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(true){
            m = sc.nextInt();
            n = sc.nextInt();
            if(m == 0 && n == 0) break;

            int x = 0;
            int y = 0;
            cnt = 0;
            for(int i = 0; i < n; i ++){
                String s = sc.next();
                for(int j = 0; j < m; j ++){
                    g[i][j] = s.charAt(j);
                    st[i][j] = false;//由于有多组测试数组,所以每次要重置st数组
                    if(g[i][j] == '@'){//用来确定起点的位置
                        x = i;
                        y = j;
                    }
                }
            }

            dfs(x, y);
            System.out.println(cnt);
        }
    }
}

 

DFS之搜索顺序

1116. 马走日 - AcWing题库

import java.util.*;

public class Main{
    static int N = 15;
    static int n, m, res, sx, sy;
    static int[] dx = {-2, -1, 1, 2, 2, 1, -1, -2};
    static int[] dy = {1, 2, 2, 1, -1, -2, -2, -1};
    static boolean[][] st = new boolean[N][N];
    public static void dfs(int x, int y, int cnt){
        if(cnt == n * m){
            res ++;//方案数加1
            return;//每次要记得返回
        }
        
        st[x][y] = true;
        for(int i = 0; i < 8; i ++){
            int a = x + dx[i];
            int b = y + dy[i];
            if(a < 0 || b < 0 || a >= n || b >= m) continue;
            if(st[a][b]) continue;
            dfs(a, b, cnt + 1);
        }
        
        st[x][y] = false;//回溯
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t -- > 0){
            n = sc.nextInt();
            m = sc.nextInt();
            sx = sc.nextInt();
            sy = sc.nextInt();
            
            res = 0;//因为有多组测试数据,所以每次答案要重置
            dfs(sx, sy, 1);//1表示已经遍历了几个点
            System.out.println(res);
        }
    }
}

1117. 单词接龙 - AcWing题库

import java.util.*;

public class Main{
    static int N = 25, n, res;
    static int[][] g = new int[N][N];//g[n][m]表示编号为n和m的单词的最短重合的长度
    static int[] used = new int[N];//表示这个编号的单词用了多少次
    static String[] word = new String[N];//给出的单词
    static char lead;
    
    public static void dfs(String dragon, int last){
        res = Math.max(res, (int)dragon.length());//每次都比较一下
        used[last] ++;//每次使用都加一下
        
        for(int i = 0; i < n; i ++){
            if(g[last][i] != 0 && used[i] < 2){
                dfs(dragon + word[i].substring(g[last][i]), i);
            }
        }
        
        used[last] --;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        
        for(int i = 0; i < n; i ++){
            word[i] = sc.next();
        }
        lead = sc.next().charAt(0);
        
        //预处理出g数组
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                String a = word[i], b = word[j];
                //由于要想龙的长度最长,所以要使得两个单词重叠部分最短,至少重叠一个字母
                for(int k = 1; k < Math.min(a.length(), b.length()); k ++){
                    if(a.substring(a.length() - k).equals(b.substring(0, k))){
                        g[i][j] = k;
                        break;
                    }
                }
            }
        }
        
        //遍历所有是从龙头开始的单词
        for(int i = 0; i < n; i ++){
            if(word[i].charAt(0) == lead) dfs(word[i], i);//i表示单词的编号
        }
        
        System.out.print(res);
    }
}

 

1118. 分成互质组 - AcWing题库

import java.util.*;

public class Main{
    static int N = 15;
    static int[] q = new int[N];
    static int[][] groud = new int[N][N];//每一组中第几个数的下标
    static boolean[] st = new boolean[N];//用来判断是否用过了
    static int n, res = N;
    
    //求最大公因数
    public static int gcd(int a, int b){
        return (b != 0 ? gcd(b, a % b) : a);
    }
    
    //判断一个数与这个组内的数是否都互质
    public static boolean check(int[] groud, int gc, int t){//第几组,这一组有几个数,要比较的数的下标
        for(int i = 0; i < gc; i ++){
            if(gcd(q[groud[i]], q[t]) > 1) return false;//公因数大于1,不互质
        }
        return true;
    }
    
    public static void dfs(int g, int gc, int tc, int start){
        if(g >= res) return;//如果组数已经大于等于我们最小组数,就直接返回
        if(tc == n) res = g;
        
        //用来判断是否要开一个新的组
        boolean flag = true;
        for(int i = start; i < n; i ++){
            if(!st[i] && check(groud[g], gc, i)){
                flag = false;//有数组可以放就不用开新的数组
                
                st[i] = true;
                groud[g][gc] = i;
                dfs(g, gc + 1, tc + 1, i);
                st[i] = false;//回溯
            }
        }
        
        if(flag) dfs(g + 1, 0, tc, 0);//重开一个组,组内的数一开始应该为0,tc总数不变,应该从0开始搜
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for(int i = 0; i < n; i ++){
            q[i] = sc.nextInt();
        }
        
        dfs(1, 0, 0, 0);//第一组,当前第一组中有0个数,一共已经搜索了0个数,从第0个数开始搜
        
        System.out.print(res);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值