【笔试真题记录】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;
            }
        }
    }
}
//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();
    }
}

你可能感兴趣的:(笔试真题,深度优先,宽度优先)