题目1460:Oil Deposit(Flood Fill)

来源:互联网 发布:网络棋牌论坛推广途径 编辑:程序博客网 时间:2024/05/16 17:38
题目1460:Oil Deposit

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:539

解决:265

题目描述:

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.

输入:

The input file 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.

输出:

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.

样例输入:
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0
样例输出:
0122
import java.util.Scanner; public class Main{     static int go[][] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}};    static boolean hash[][];    static char maze[][];    static int n,m;    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);                 while( scanner.hasNext() ){            n = scanner.nextInt();            m = scanner.nextInt();            maze = new char[n][m];            hash = new boolean[n][m];                         if(n==0 && m==0) break;            for (int i = 0; i < n; i++) {                String s = scanner.next();                for (int j = 0; j < m; j++) {                    hash[i][j] = false;                    maze[i][j] = s.charAt(j);                }            }                         int ans = 0;                                      for (int i = 0; i < n; i++) {                for (int j = 0; j < m; j++) {                    if(hash[i][j] == true) continue;                    if(maze[i][j] == '*') continue;                    DFS(i,j);                    ans++;                }            }                         System.out.println(ans);                     }    }    private static void DFS(int x, int y) {                 for (int i = 0; i < 8; i++) {            int nx = x+go[i][0];            int ny = y+go[i][1];             if(nx < 0 || ny < 0 || nx >=n || ny >= m) continue;            if(maze[nx][ny] == '*') continue;            if(hash[nx][ny] == true) continue;                         hash[nx][ny] = true;            DFS(nx,ny);        }                 return;    } } /**************************************************************    Problem: 1460    User: yihukurama    Language: Java    Result: Accepted    Time:290 ms    Memory:25844 kb****************************************************************/


0 0
原创粉丝点击