房屋群

来源:互联网 发布:acm网络赛 编辑:程序博客网 时间:2024/04/25 16:53
//返回村庄的总数import java.util.LinkedList;import java.util.Queue;public class Test {    public static void main(String[] args) {        int[][] map = {{0, 1, 0,1}, {0, 1, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}};        System.out.println(solve(map));    }    static int solve(int[][] grid) {        int cnt = 0;        int row = grid.length;        int col = grid[0].length;        used = new int[row][col];        for (int i = 0; i < row; i++) {            for (int j = 0; j < col; j++) {                if (grid[i][j] == 1 && used[i][j] == 0) {                    bfs(grid, i, j, row, col);                    cnt++;                }            }        }        return cnt;    }    static class Node {        int x;        int y;        public Node(int x, int y) {            this.x = x;            this.y = y;        }    }    static int[][] used;    static int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};    static void bfs(int[][] map, int x, int y, int n, int m) {        Queue<Node> q = new LinkedList<Node>();        Node first = new Node(x, y);        q.add(first);        used[x][y] = 1;        while (!q.isEmpty()) {            Node head = q.poll();            int xx = head.x;            int yy = head.y;            for (int i = 0; i < 4; i++) {                int px = xx + d[i][0];                int py = yy + d[i][1];                if (px < 0 || px >= n || py < 0 || py >= m || used[px][py] == 1 || map[px][py] == 0)                    continue;                q.add(new Node(px, py));                used[px][py] = 1;            }        }    }}//返回最大的村庄数。。。操,这个笔试的时候没有ac,差一点import java.util.LinkedList;import java.util.Queue;public class Test {    public static void main(String[] args) {        int[][] map = {{1, 1, 0,0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}};        System.out.println(solve(map));    }    static int solve(int[][] grid) {        int max=0;        int cnt = 0;        int row = grid.length;        int col = grid[0].length;        used = new int[row][col];        for (int i = 0; i < row; i++) {            for (int j = 0; j < col; j++) {                if (grid[i][j] == 1 && used[i][j] == 0) {                  int cou=  bfs(grid, i, j, row, col,1);                 if(max<=cou){                     max=cou;                 }                }            }        }        return max;    }    static class Node {        int x;        int y;        public Node(int x, int y) {            this.x = x;            this.y = y;        }    }    static int[][] used;    static int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};    static int bfs(int[][] map, int x, int y, int n, int m,int count) {        Queue<Node> q = new LinkedList<Node>();        Node first = new Node(x, y);        q.add(first);        used[x][y] = 1;        while (!q.isEmpty()) {            Node head = q.poll();            int xx = head.x;            int yy = head.y;            for (int i = 0; i < 4; i++) {                int px = xx + d[i][0];                int py = yy + d[i][1];                if (px < 0 || px >= n || py < 0 || py >= m || used[px][py] == 1 || map[px][py] == 0)                    continue;                q.add(new Node(px, py));                used[px][py] = 1;                count++;            }        }        return count;    }}


0 0
原创粉丝点击