• 【笔试真题记录】2023华为9.20机试第二题(DFS和BFS)


    题目:
    班级组织传球活动,男女同学随机排成m行n列队伍,第一列中的任意一个男同学都可以作为传球的起点,要求最终将球传到最后一列的任意一个男同学手里,求所有能够完成任务的传球路线中的最优路线(传球次数最少的路线)的传球次数。

    传球规则:
    1.男同学只能将球传给男同学,不能传给女同学。
    2.球只能传给身边前后左右相邻的同学。
    3.如果游戏不能完成,返回-1。

    说明:
    1.传球次数最少的路线为最优路线。
    2.最优路线可能不唯一,不同最优路线都为最少传球次数。

    解答要求:
    时间限制:C/C++100ms其他语言: 200ms内存限制: C/C++256MB,其他语言: 512MB

    输入:
    班级同学随机排成的m行n列队伍,1代表男同学,0代表女同学。
    输入第一行包含两个用空格分开的整数m[1,30]和n [1,30],表示m行n列的队伍;
    接下来是m行每行包含n个用空格分开的整数1或0。

    输出:
    最优路线的传球次数(最少传球次数)

    样例
    输入:
    4 4
    1 1 1 0
    1 1 1 0
    0 0 1 0
    0 1 1 1
    输出:
    5

    //DFS
    import java.util.Scanner;
    
    public class Huawei {
        private static int minPasses = Integer.MAX_VALUE;
        private static int[] dx = {-1, 1, 0, 0};
        private static int[] dy = {0, 0, -1, 1};
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int m = scanner.nextInt();
            int n = scanner.nextInt();
            int[][] grid = new int[m][n];
    
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    grid[i][j] = scanner.nextInt();
                }
            }
    
            for (int i = 0; i < m; i++) {
                if (grid[i][0] == 1) {
                    dfs(i, 0, 0, m, n, grid);
                }
            }
    
            if (minPasses == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
                System.out.println(minPasses);
            }
        }
    
        private static void dfs(int x, int y, int passes, int m, int n, int[][] grid) {
            if (y == n - 1) {
                minPasses = Math.min(minPasses, passes);
                return;
            }
    
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
    
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == 1) {
                    grid[x][y] = 0;
                    dfs(nx, ny, passes + 1, m, n, grid);
                    grid[x][y] = 1;
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    //BFS,代码来源万诺coding
    import java.io.*;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.StringTokenizer;
    
    public class Main {
    
        private static  class FastScanner {
            BufferedReader br;
            StringTokenizer st;
            public FastScanner(InputStream stream){
                br =  new BufferedReader(new InputStreamReader(stream), 32768);
                st = null;
            }
            String next() {
                while (st == null ||  !st.hasMoreTokens())
                    try {
                        st=new StringTokenizer(br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                return st.nextToken();
            }
    
            int nextInt() {
                return Integer.parseInt(next());
            }
        }
    
    
        public static void main(String[] args) {
            new Main().solve();
        }
        int m,n;
        int[][] grid;
        void solve() {
            PrintWriter pwin = new PrintWriter(new OutputStreamWriter(System.out));
            FastScanner fsc = new FastScanner(System.in);
            m = fsc.nextInt();;
            n = fsc.nextInt();
            grid = new int[m][n];
    
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    grid[i][j] = fsc.nextInt();
                }
            }
            int[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};
            int res = 10000001;
            for (int i = 0; i < m; i++) {
                if (grid[i][0] == 1) {
                    Deque<int[]> dq = new LinkedList<>();
                    dq.add(new int[]{0,i,0});
                    boolean[][] used = new boolean[m][n];
                    used[i][0] = true;
                    while (!dq.isEmpty()) {
                        int[] a = dq.poll();
                        int d = a[0], x = a[1], y = a[2];
                        if (y == n-1) {
                            res = Math.min(res, d);
                        }
                        for (int[] dir : dirs) {
                            int nx = x + dir[0],  ny = y + dir[1];
                            if (nx < 0 || ny < 0 || nx >= m || ny >= n || used[nx][ny] || grid[nx][ny] != 1) continue;
                            used[nx][ny] = true;
                            dq.add(new int[]{d+1, nx,ny});
                        }
                    }
                }
            }
            if (res != 10000001) pwin.println(res);
            else pwin.println(-1);
            pwin.flush();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    Knowledge related to the enterprise
    Markem imaje马肯依玛士喷码机维修9450E打码机维修
    数据集笔记:分析OpenCellID 不同radio/ create_time update_time可视化
    使用Kohya_ss训练Stable Diffusion Lora
    TP6 TP8 使用阿里官方OSS SDK方法
    [源码解析] TensorFlow 分布式之 ParameterServerStrategy V2
    CLICKHOUSE
    【Javascript】不满意网上的Token无感知刷新方案,自己琢磨了个感觉还不错~
    Docker:容器网络互联
    1-辛基-3-甲基咪唑六氟磷酸盐([OMIM] PF6)负载修饰四氧化三铁( Fe3O4)为载体包覆二氧化硅材料
  • 原文地址:https://blog.csdn.net/2301_77450803/article/details/133103170