DFS中的连通性和搜索顺序

news/2024/7/20 20:04:58 标签: 深度优先, 算法

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

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);
    }
}

 


http://www.niftyadmin.cn/n/5398212.html

相关文章

Chapter 8 - 19. Congestion Management in TCP Storage Networks

Queue Depth Monitoring and Microburst Detection Queue depth monitoring and microburst detection capture the events that may cause congestion at a lower granularity but are unnoticed by other means due to long polling intervals. 队列深度监控和微爆检测可捕捉…

【探索AI】十一 深度学习之第1周:深度学习概述与基础

深度学习概述与基础 深度学习的发展历史与现状神经网络的基本原理前向传播与反向传播算法常见的激活函数与优化算法深度学习框架&#xff08;如TensorFlow或PyTorch&#xff09;进行基础操作 深度学习的发展历史与现状 深度学习的发展历史可以追溯到上世纪40年代&#xff0c;当…

android开发书籍推荐,android面试复习

笼统来说&#xff0c;中年程序员容易被淘汰的原因其实不外乎三点。 1、输出能力已到顶点。这个人奋斗十来年了&#xff0c;依旧碌碌无为&#xff0c;很明显这人的天花板就这样了&#xff0c;说白了&#xff0c;天赋就这样。 2、适应能力越来越差。年纪大&#xff0c;有家庭&…

高校水电预付费管控系统

高校水电预付费管控系统在现代高校管理中扮演着重要角色。这一系统通过整合先进的科技手段&#xff0c;如智能计量设备和互联网技术&#xff0c;实现对校园水电消费的精准监控和管理。首先&#xff0c;高校水电预付费管控系统能够有效监测学生宿舍、教学楼等区域的实时用水用电…

定制开发一款家政小程序,应知应会

引言 在这个快节奏的现代生活中&#xff0c;人们对高效、便捷的家政服务的需求日益增加。随着社会结构的变化和职业生活的繁忙&#xff0c;许多家庭面临着时间不足、精力不济的挑战。在这种情况下&#xff0c;家政服务成为解决问题的有效途径。然而&#xff0c;传统的家政服务…

vue中使用echarts刷新可以正常渲染,路由跳转不显示

在 Vue 中使用 ECharts 组件时&#xff0c;遇到路由跳转后图表不显示的问题可能是因为组件销毁和重新创建的原因。当你从一个页面切换到另一个页面时&#xff0c;旧页面上的组件会被销毁&#xff0c;并在新页面上重新创建。 要解决这个问题&#xff0c;你可以尝试以下方法&…

【服务器磁盘清理技巧】

服务器磁盘空间的管理是一个非常重要的系统管理员工作。大量的日志文件、临时文件、备份文件等可能会迅速填满你的磁盘空间&#xff0c;导致应用无法正常运行。因此&#xff0c;磁盘空间的清理和管理就显得非常重要。 本文将介绍一些在Linux服务器上清理磁盘空间的基本技巧。 …

手机如何使用NFC卡模拟门禁刷卡

部分手机具备NFC卡刷卡功能&#xff0c;理论上也可模拟门禁卡。 一个功能强大且免费的NFC卡模拟器&#xff0c;可模拟各类门禁卡、电梯卡、部分公司&#xff08;工厂&#xff09;工卡或饭卡、部分学校饭卡、部分图书馆借书卡等各类IC卡&#xff0c;用手机替代卡片去刷门禁、刷…