【笔试真题记录】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();
    }
}
相关推荐
茉莉玫瑰花茶15 小时前
floodfill 算法(dfs)
算法·深度优先
电子_咸鱼1 天前
LeetCode——Hot 100【电话号码的字母组合】
数据结构·算法·leetcode·链表·职场和发展·贪心算法·深度优先
一只鱼^_2 天前
第 167 场双周赛 / 第 471 场周赛
数据结构·b树·算法·leetcode·深度优先·近邻算法·迭代加深
2401_840105202 天前
P1049 装箱问题 题解(四种方法)附DP和DFS的对比
c++·算法·深度优先·动态规划
微笑尅乐3 天前
BFS 与 DFS——力扣102.二叉树的层序遍历
leetcode·深度优先·宽度优先
知星小度S3 天前
算法训练之多源BFS
算法·宽度优先
Codeking__4 天前
DFS算法原理及其模板
算法·深度优先·图论
Pluchon4 天前
硅基计划4.0 算法 二叉树深搜(DFS)
java·数据结构·算法·leetcode·深度优先·剪枝
Miraitowa_cheems4 天前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
熬了夜的程序员5 天前
【LeetCode】74. 搜索二维矩阵
线性代数·算法·leetcode·职场和发展·矩阵·深度优先·动态规划