bfs 广度优先遍历的应用 --CountHomes

来源:互联网 发布:淘宝手机详情视频时间 编辑:程序博客网 时间:2024/06/16 17:43

题目:

为了进行城市规划,需要计算一个居住区的住宅数目。该居住聚集区俯视图已经制好,并规划为n x m个网格。如果网格单元具有屋顶定的一部分,则向其赋值1,如果是空地,则赋值0.由值为1的相邻网格单元组成的簇认定为一个单独建筑。对角放置的值为1的网格则不被视为同一住宅(屋顶)。

类Homes的方法countHomes的输入应包括一个n x m阶的二维整数数组grid,其中n和m分别表示输入数组grid的行数和列数。

该方法应该返回一个表示住宅总数目的整数,grid只包含0和1.

 测试用例1:

输入:

0,0,0,0,0

0,1,1,0,0

0,0,1,1,0

0,0,0,0,0

0,0,0,0,0,

输出:

1

测试用例2:

输入:

0,0,0,0

0,1,0,0

0,0,1,0

0,0,0,0

输出:

2

代码:

import java.util.ArrayDeque;import java.util.Queue;/** * @author Administrator * */public class Homes {public static int counthHomes(int grid[][]) {// 特殊情况处理if (grid == null || grid[0].length == 0 || grid.length == 0) {return 0;}// 一般情况int rows = grid.length;int cols = grid[0].length;// 访问标志数组boolean visited[] = new boolean[rows * cols];int home_count = 0;for (int row = 0; row < rows; row++) {for (int col = 0; col < cols; col++) {if (count_home(grid, rows, cols, row, col, visited)) {home_count++;}}}return home_count;}private static boolean count_home(int[][] grid, int rows, int cols, int row, int col, boolean[] visited) {if (row >= 0 && row < rows && col >= 0 && col < cols && visited[row * cols + col] == false&& grid[row][col] == 1) {mark_neighbor_4(grid, rows, cols, row, col, visited);return true;}return false;}// 标记4连通区域,广度优先遍历// 使用队列辅助private static void mark_neighbor_4(int[][] grid, int rows, int cols, int row, int col, boolean[] visited) {if (row < 0 || row >= rows || col < 0 && col >= cols) {return;}Queue<Integer> indexQueue = new ArrayDeque<Integer>();indexQueue.offer(row);indexQueue.offer(col);int curRow;int curCol;while (!indexQueue.isEmpty()) {curRow = indexQueue.poll();curCol = indexQueue.poll();if (curRow - 1 >= 0 && curRow < rows && curCol >= 0 && curCol < cols && grid[curRow - 1][curCol] == 1&& visited[(curRow - 1) * cols + curCol] == false) {visited[(curRow - 1) * cols + curCol] = true;indexQueue.offer(curRow - 1);indexQueue.offer(curCol);}if (curCol - 1 >= 0 && curRow >= 0 && curRow < rows && curCol < cols && grid[curRow][curCol - 1] == 1&& visited[(curRow) * cols + curCol - 1] == false) {visited[(curRow) * cols + curCol - 1] = true;indexQueue.offer(curRow);indexQueue.offer(curCol - 1);}if (curRow + 1 < rows && curRow >= 0 && curCol >= 0 && curCol < cols && grid[curRow + 1][curCol] == 1&& visited[(curRow + 1) * cols + curCol] == false) {visited[(curRow + 1) * cols + curCol] = true;indexQueue.offer(curRow + 1);indexQueue.offer(curCol);}if (curCol + 1 < cols && curRow >= 0 && curRow < rows && grid[curRow][curCol + 1] == 1&& visited[(curRow) * cols + curCol + 1] == false) {visited[(curRow) * cols + curCol + 1] = true;indexQueue.offer(curRow);indexQueue.offer(curCol + 1);}}return;}/** * @param args */public static void main(String[] args) {int grid1[][] = new int[4][4];System.out.println(grid1.length + "行" + grid1[0].length + "列");for (int i = 0; i < grid1.length; i++) {for (int j = 0; j < grid1[0].length; j++) {grid1[i][j] = 1;}}System.out.println(counthHomes(grid1));int grid2[][] = new int[4][4];System.out.println(grid2.length + "行" + grid2[0].length + "列");for (int i = 0; i < grid2.length; i++) {for (int j = 0; j < grid2[0].length; j++) {if (i == j) {grid2[i][j] = 1;}}}System.out.println(counthHomes(grid2));int grid3[][] = new int[5][5];System.out.println(grid3.length + "行" + grid3[0].length + "列");grid3[1][1] = 1;grid3[1][2] = 1;grid3[2][2] = 1;grid3[2][3] = 1;grid3[4][3] = 1;System.out.println(counthHomes(grid3));}}




0 0
原创粉丝点击