【蓝桥杯】最大连通块——dfs+bfs题解

问题描述


小蓝有一个 30 行 60 列的数字矩阵,矩阵中的每个数都是 0 或 1 。
110010000011111110101001001001101010111011011011101001111110
010000000001010001101100000010010110001111100010101100011110
001011101000100011111111111010000010010101010111001000010100
101100001101011101101011011001000110111111010000000110110000
010101100100010000111000100111100110001110111101010011001011
010011011010011110111101111001001001010111110001101000100011
101001011000110100001101011000000110110110100100110111101011
101111000000101000111001100010110000100110001001000101011001
001110111010001011110000001111100001010101001110011010101110
001010101000110001011111001010111111100110000011011111101010
011111100011001110100101001011110011000101011000100111001011
011010001101011110011011111010111110010100101000110111010110
001110000111100100101110001011101010001100010111110111011011
111100001000001100010110101100111001001111100100110000001101
001110010000000111011110000011000010101000111000000110101101
100100011101011111001101001010011111110010111101000010000111
110010100110101100001101111101010011000110101100000110001010
110101101100001110000100010001001010100010110100100001000011
100100000100001101010101001101000101101000000101111110001010
101101011010101000111110110000110100000010011111111100110010
101111000100000100011000010001011111001010010001010110001010
001010001110101010000100010011101001010101101101010111100101
001111110000101100010111111100000100101010000001011101100001
101011110010000010010110000100001010011111100011011000110010
011110010100011101100101111101000001011100001011010001110011
000101000101000010010010110111000010101111001101100110011100
100011100110011111000110011001111100001110110111001001000111
111011000110001000110111011001011110010010010110101000011111
011110011110110110011011001011010000100100101010110000010011
010011110011100101010101111010001001001111101111101110011101
  如果从一个标为 1 的位置可以通过上下左右走到另一个标为 1 的位置,则称两个位置连通。与某一个标为 1 的位置连通的所有位置(包括自己)组成一个连通分块。
  请问矩阵中最大的连通分块有多大?
 

题目代码——dfs 

public class 最大连通快_dfs {
    static int dx[] = {1, -1, 0, 0};
    static int dy[] = {0, 0, 1, -1};
    static int map[][] = new int[30][60];
    static boolean st[][] = new boolean[30][60];
    static String[] s;
    static int count = 0, max = 0;

    public static void main(String[] args) {
        s = new String[]{
                "110010000011111110101001001001101010111011011011101001111110",
                "010000000001010001101100000010010110001111100010101100011110",
                "001011101000100011111111111010000010010101010111001000010100",
                "101100001101011101101011011001000110111111010000000110110000",
                "010101100100010000111000100111100110001110111101010011001011",
                "010011011010011110111101111001001001010111110001101000100011",
                "101001011000110100001101011000000110110110100100110111101011",
                "101111000000101000111001100010110000100110001001000101011001",
                "001110111010001011110000001111100001010101001110011010101110",
                "001010101000110001011111001010111111100110000011011111101010",
                "011111100011001110100101001011110011000101011000100111001011",
                "011010001101011110011011111010111110010100101000110111010110",
                "001110000111100100101110001011101010001100010111110111011011",
                "111100001000001100010110101100111001001111100100110000001101",
                "001110010000000111011110000011000010101000111000000110101101",
                "100100011101011111001101001010011111110010111101000010000111",
                "110010100110101100001101111101010011000110101100000110001010",
                "110101101100001110000100010001001010100010110100100001000011",
                "100100000100001101010101001101000101101000000101111110001010",
                "101101011010101000111110110000110100000010011111111100110010",
                "101111000100000100011000010001011111001010010001010110001010",
                "001010001110101010000100010011101001010101101101010111100101",
                "001111110000101100010111111100000100101010000001011101100001",
                "101011110010000010010110000100001010011111100011011000110010",
                "011110010100011101100101111101000001011100001011010001110011",
                "000101000101000010010010110111000010101111001101100110011100",
                "100011100110011111000110011001111100001110110111001001000111",
                "111011000110001000110111011001011110010010010110101000011111",
                "011110011110110110011011001011010000100100101010110000010011",
                "010011110011100101010101111010001001001111101111101110011101"};
        for (int i = 0; i < 30; i++) {
            char[] chars = s[i].toCharArray();
            for (int j = 0; j < 60; j++) {
                map[i][j] = chars[j] - '0';
            }
        }
        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 60; j++) {
                if (map[i][j] == 1 && !st[i][j]) {
                    st[i][j] = true;
                    count = 0;
                    count++;
                    dfs(i, j);
                    max = Math.max(count,max);
                }
            }
        }
        System.out.println(max);

    }

    static void dfs(int x, int y) {
        if (x >= 30 || y >= 60) return;

        for (int i = 0; i < 4; i++) {
            int a = x + dx[i], b = y + dy[i];

            if (a < 0 || a >= 30 || b < 0 || b >= 60) continue;
            if (map[a][b] != 1 || st[a][b]) continue;

            st[a][b] = true;
            count++;
            dfs(a,b);
        }
    }
}

题目代码——bfs

 

import java.util.LinkedList;

public class 最大连通快_bfs {
    static int dx[] = {1, -1, 0, 0};
    static int dy[] = {0, 0, 1, -1};
    static int map[][] = new int[30][60];
    static boolean st[][] = new boolean[30][60];
    static String[] s;
    static int count = 0, max = 0;

    public static void main(String[] args) {
        s = new String[]{
                "110010000011111110101001001001101010111011011011101001111110",
                "010000000001010001101100000010010110001111100010101100011110",
                "001011101000100011111111111010000010010101010111001000010100",
                "101100001101011101101011011001000110111111010000000110110000",
                "010101100100010000111000100111100110001110111101010011001011",
                "010011011010011110111101111001001001010111110001101000100011",
                "101001011000110100001101011000000110110110100100110111101011",
                "101111000000101000111001100010110000100110001001000101011001",
                "001110111010001011110000001111100001010101001110011010101110",
                "001010101000110001011111001010111111100110000011011111101010",
                "011111100011001110100101001011110011000101011000100111001011",
                "011010001101011110011011111010111110010100101000110111010110",
                "001110000111100100101110001011101010001100010111110111011011",
                "111100001000001100010110101100111001001111100100110000001101",
                "001110010000000111011110000011000010101000111000000110101101",
                "100100011101011111001101001010011111110010111101000010000111",
                "110010100110101100001101111101010011000110101100000110001010",
                "110101101100001110000100010001001010100010110100100001000011",
                "100100000100001101010101001101000101101000000101111110001010",
                "101101011010101000111110110000110100000010011111111100110010",
                "101111000100000100011000010001011111001010010001010110001010",
                "001010001110101010000100010011101001010101101101010111100101",
                "001111110000101100010111111100000100101010000001011101100001",
                "101011110010000010010110000100001010011111100011011000110010",
                "011110010100011101100101111101000001011100001011010001110011",
                "000101000101000010010010110111000010101111001101100110011100",
                "100011100110011111000110011001111100001110110111001001000111",
                "111011000110001000110111011001011110010010010110101000011111",
                "011110011110110110011011001011010000100100101010110000010011",
                "010011110011100101010101111010001001001111101111101110011101"};
        for (int i = 0; i < 30; i++) {
            char[] chars = s[i].toCharArray();
            for (int j = 0; j < 60; j++) {
                map[i][j] = chars[j] - '0';
            }
        }
        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 60; j++) {
                if (map[i][j] == 1 && !st[i][j]) {
                    st[i][j] = true;
                    count = 0;
                    count++;
                    bfs(i, j);
                    max = Math.max(count, max);
                }
            }
        }
        System.out.println(max);
    }

    static void bfs(int x, int y) {
        LinkedList<Pair> q = new LinkedList<>();
        q.offer(new Pair(x, y));

        while (!q.isEmpty()) {
            Pair pair = q.poll();

            for (int i = 0; i < 4; i++) {
                int a = pair.x + dx[i], b = pair.y + dy[i];
                if (a < 0 || a >= 30 || b < 0 || b >= 60) continue;
                if (map[a][b] != 1 || st[a][b]) continue;

                st[a][b] = true;
                count++;
                q.offer(new Pair(a, b));
            }
        }
    }
}
class Pair2 {
    int x, y;

    public Pair2(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_61082895/article/details/129979666