【笔试真题记录】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

java 复制代码
//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;
            }
        }
    }
}
java 复制代码
//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();
    }
}
相关推荐
Paper Clouds1 天前
代码随想录|图论|15并查集理论基础
数据结构·算法·leetcode·深度优先·图论
Paper Clouds1 天前
代码随想录|图论|14有向图的完全可达性
数据结构·算法·深度优先·图论·宽度优先
Y1nhl2 天前
力扣_二叉树的BFS_python版本
python·算法·leetcode·职场和发展·宽度优先
课堂剪切板3 天前
ch07 题解
算法·深度优先
程序员Xu4 天前
【OD机试题解法笔记】连续出牌数量
笔记·算法·深度优先
闻缺陷则喜何志丹4 天前
【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
数据结构·c++·前缀和·宽度优先·洛谷·并集查找
浩瀚星辰20245 天前
图论基础算法:DFS、BFS、并查集与拓扑排序的Java实现
java·算法·深度优先·图论
FirstFrost --sy7 天前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
闻缺陷则喜何志丹7 天前
【BFS】 P10864 [HBCPC2024] Genshin Impact Startup Forbidden II|普及+
c++·算法·宽度优先·洛谷
Two_brushes.8 天前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先