UVA 572

来源:互联网 发布:京东购物流程淘宝 编辑:程序博客网 时间:2024/06/18 08:08

 

 

Oil Deposits

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 file contains one or more grids. Each grid begins with a line containingm and n, the number of rows and columns in the grid, separated by a single space. Ifm = 0 it signals the end of the input; otherwise$1 \le m \le 100$and$1 \le n \le 100$. Following this arem 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

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they 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

0122

 

 

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package one;

import java.util.Scanner;

public class a572{

    static int count;
    static char grid[][] = new char[101][101];
    static int m, n;
    static int dir[][] = {{1, 1}, {1, -1}, {0, 1}, {1, 0}, {0, -1}, {-1, 0}, {-1, -1}, {-1, 1}};
    static int flag;

    static void DFS(int x, int y) {
        int i, xx, yy;
        if (grid[x][y] == '@') {
            grid[x][y] = '*';
            for (i = 0; i < 8; i++) {
                xx = x + dir[i][0];
                yy = y + dir[i][1];
                if (xx < 0 || yy < 0 || xx >= m || yy >= n) {
                    continue;
                } else if (grid[xx][yy] == '@') {
                    DFS(xx, yy);
                } else {
                    flag = 1;
                }
            }
        }
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        while (true) {
            int i, j;//循环变量
            //统计油田数量
            m = in.nextInt();
            n = in.nextInt();
            if (m == 1 && n == 1) {
                String myString = in.next();
                for (j = 0; j < myString.length(); j++) {
                    grid[0][j] = myString.charAt(j);
                }
                if(grid[0][0]=='@'){
                    System.out.println("1");
                }else{
                    System.out.println("0");
                }

            } else if (m == 0) {
                break;
            } else {
                for (i = 0; i < m; i++) {
                    String myString = in.next();
                    for (j = 0; j < myString.length(); j++) {
                        grid[i][j] = myString.charAt(j);
                    }
                }
                count = 0;
                for (i = 0; i < m; i++) {
                    for (j = 0; j < n; j++) {
                        flag = 0;
                        DFS(i, j);
                        if (flag == 1) {
                            count++;
                        }
                    }
                }
                System.out.println(count);
            }
        }
    }
}

原创粉丝点击