博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1562 Oil Deposits(dfs)
阅读量:5106 次
发布时间:2019-06-13

本文共 2977 字,大约阅读时间需要 9 分钟。

Oil Deposits
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15557   Accepted: 8431

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

0122

Java AC 代码

import java.util.Scanner;public class Main {    static String field[][];        static int rows;    static int columns;        static int pockets;        static boolean marked[][];        static int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};    static int dy[] = {1, 1, 1, 0, 0, -1, -1, -1};        public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        while((rows =sc.nextInt()) != 0 && (columns = sc.nextInt()) != 0) {            pockets = 0;            field = new String[rows][columns];            marked = new boolean[rows][columns];            for(int i = 0; i < rows; i++) {                String line = sc.next();                for(int j = 0; j < columns; j++) {                    field[i][j] = line.substring(j, j + 1);                }            }                        for(int i = 0; i < rows; i++)                for(int j = 0; j < columns; j++) {                    if(!marked[i][j] && field[i][j].equals("@")) {                        dfs(i, j);                        pockets++;                    }                }            System.out.println(pockets);        }    }        public static void dfs(int row, int col) {                for(int i = 0; i < 8; i++) {            int _row = row + dy[i];            int _col = col + dx[i];            if(_row < rows && _row >= 0 && _col < columns && _col >= 0 && !marked[_row][_col] && field[_row][_col].equals("@")) {                marked[_row][_col] = true;                dfs(_row, _col);            }        }    }}

 

转载于:https://www.cnblogs.com/kkkkkk/p/5539909.html

你可能感兴趣的文章
[转]: 视图和表的区别和联系
查看>>
Regular Experssion
查看>>
python中的字符编码
查看>>
图论例题1——NOIP2015信息传递
查看>>
uCOS-II中的任务切换-图解多种任务调度时机与问题
查看>>
CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
查看>>
Android 官方新手指导教程
查看>>
幸运转盘v1.0 【附视频】我的Android原创处女作,请支持!
查看>>
UseIIS
查看>>
为什么int型最大的数是2147483647
查看>>
数据库连接的三层架构
查看>>
集合体系
查看>>
vi命令提示:Terminal too wide
查看>>
nyoj 5 Binary String Matching(string)
查看>>
引用 移植Linux到s3c2410上
查看>>
BizTalk 2010 单机安装
查看>>
人与人之间的差距是从大学开始的
查看>>
vue 开发过程中遇到的问题
查看>>
[Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator
查看>>
[Swift]LeetCode223. 矩形面积 | Rectangle Area
查看>>