当前位置 博文首页 > 用心编码:poj 1562 DFS(深度遍历) - 算法

    用心编码:poj 1562 DFS(深度遍历) - 算法

    作者:[db:作者] 时间:2021-09-07 13:29

    题目要求
    Description
    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

    Input
    The input contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or@’, representing an oil pocket.

    Output
    are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

    Sample Input
    1 1
    *
    3 5
    @@*
    @
    @@*
    1 8
    @@**@*
    5 5
    **@
    @@@
    @*@
    @@@*@
    @@**@
    0 0

    Sample Output
    0
    1
    2
    2

    代码实现

    package com.daxiong.day5;
    
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (true) {
                int row = in.nextInt(); // 读取行数
                int col = in.nextInt(); // 读取列数
                if (row == 0 && col == 0)
                    break;
                int count = 0; // 记录总数
                int[][] arr = new int[row + 2][col + 2];
                for (int i = 1; i <= row; i++) {
                    String s = in.next();
                    for (int j = 1; j <= col; j++) {
                        arr[i][j] = s.charAt(j - 1);    // 拆分获取字符
                    }
                }
    
                /*
                 * for (int i = 1; i <= row; i++) { for (int j = 1; j <= col; j++){
                 * System.out.println("arr[][]" + arr[i][j]); } }
                 */
    
                for (int i = 1; i <= row; i++)
                    for (int j = 1; j <= col; j++)
                        if (arr[i][j] == 64) {
                            find(arr, i, j);
                            count++;
                        }
                System.out.println(count);
            }
            in.close();
        }
    
        public static void find(int[][] a, int i, int j) {
            a[i][j] = 65;
            for (int k = i - 1; k <= i + 1; k++)
                for (int w = j - 1; w <= j + 1; w++)
                    if (a[k][w] == 64)
                        find(a, k, w);
        }
    }
    
    cs