思路:bfs。水题,标记下计数就完了。

Oil Deposits

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22928   Accepted: 11883

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

Source

Mid-Central USA 1997

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; class Point{ public int x;
public int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public Point(){}
}
public class Main {
static int[][] dir={{0,1},{0,-1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1}};
public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int m=in.nextInt();
int n=in.nextInt();
Queue<Point> q=new LinkedList<Point>();
while(m!=0&&n!=0){
int ans=0;
char gra[][]=new char[m][n];
boolean vis[][]=new boolean[m][n];
for(int i=0;i<m;i++){
String s=in.next();
for(int j=0;j<s.length();j++){
gra[i][j]=s.charAt(j);
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(gra[i][j]=='@'&&vis[i][j]==false){
Point p=new Point(i,j);
bfs(gra,vis,p,m,n,q);
ans++;
}
}
}
System.out.println(ans);
ans=0;
q.clear();
m=in.nextInt();
n=in.nextInt();
}
}
private static void bfs(char[][] gra, boolean[][] vis, Point p, int m,
int n, Queue<Point> q)
{
q.add(p);
vis[p.x][p.y]=true;
while(!q.isEmpty()){
Point qq=q.remove();
for(int i=0;i<8;i++){ int xx=qq.x+dir[i][0];
int yy=qq.y+dir[i][1];
if((!(xx<0||yy<0||xx>=m||yy>=n))&&vis[xx][yy]==false&&gra[xx][yy]=='@'){
vis[xx][yy]=true;
q.add(new Point(xx,yy));
}
}
}
} }

POJ1562_Oil Deposits(JAVA语言)的更多相关文章

  1. JAVA语言中的修饰符

    JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...

  2. Atitit onvif协议获取rtsp地址播放java语言 attilx总结

    Atitit onvif协议获取rtsp地址播放java语言 attilx总结 1.1. 获取rtsp地址的算法与流程1 1.2. Onvif摄像头的发现,ws的发现机制,使用xcf类库1 2. 调用 ...

  3. AVL树原理及实现(C语言实现以及Java语言实现)

    欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...

  4. Java语言中的面向对象特性总结

    Java语言中的面向对象特性 (总结得不错) [课前思考]  1. 什么是对象?什么是类?什么是包?什么是接口?什么是内部类?  2. 面向对象编程的特性有哪三个?它们各自又有哪些特性?  3. 你知 ...

  5. JAVA语言搭建白盒静态代码、黑盒网站插件式自动化安全审计平台

    近期打算做一个插件化的白盒静态代码安全审计自动化平台和黑盒网站安全审计自动化平台.现在开源或半开源做黑盒网站安全扫描的平台,大多是基于python脚本,安全人员贡献python脚本插件增强平台功能.对 ...

  6. 关于Java语言和面向对象记录

    本科时常用的c语言是面向过程的语言,而Java是面向对象的语言 Java语言的11个关键术语 简单性.可移植性.面向对象.分布式.高性能.解释型.健壮性.多线程.安全性.动态性.体系结构中立 面向对象 ...

  7. 用Java语言编写一个简易画板

    讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...

  8. 【百度文库课程】Java语言基础与OOP入门学习笔记一

    一. Java的历史与由来 原名Oak,针对嵌入式系统开发设计,语法与C/C++基本一致 二. Java语言特点 Java由四方面组成:Java编程语言.Java类文件格式.Java虚拟机和Java应 ...

  9. 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词

    第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...

随机推荐

  1. HDU 4675 GCD of Sequence(莫比乌斯反演 + 打表注意事项)题解

    题意: 给出\(M\)和\(a数组\),询问每一个\(d\in[1,M]\),有多少组数组满足:正好修改\(k\)个\(a\)数组里的数使得和原来不同,并且要\(\leq M\),并且\(gcd(a_ ...

  2. 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解

    题意: 有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形.棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\). 思路: 由于不构成 ...

  3. In_array()函数弱比较

    0x01 定义 (PHP 4, PHP 5, PHP 7) in_array - 检查数组中是否存在某个值 说明 in_array ( mixed $needle , array $haystack ...

  4. 可迭代对象&迭代器&生成器

    在python中,可迭代对象&迭代器&生成器的关系如下图: 即:生成器是一种特殊的迭代器,迭代器是一种特殊的可迭代对象. 可迭代对象 如上图,这里x是一个列表(可迭代对象),其实正如第 ...

  5. 在利用手背扫描图像+K因子 对室内温度进行回归预测时碰到的问题

    1. 关于多输入流: 由于本Mission是双输入, 导师要求尽量能使用Inception之诸, 于是输入便成了问题. 思考: 在Github上找到了keras-inceptionV4进行对网络头尾的 ...

  6. Apple Watch Series 6 无法使用截屏问题和解决方案

    Apple Watch Series 6 无法使用截屏问题和解决方案 shit Apple,无法使用截屏, TMD 根本就不存在 相机胶卷 ! 不好使 解决方案 ??? https://support ...

  7. 使用 js 实现一个简易版的动画库

    使用 js 实现一个简易版的动画库 具有挑战性的前端面试题 animation css refs https://www.infoq.cn/article/0NUjpxGrqRX6Ss01BLLE x ...

  8. React 组件之间通信 All in One

    React 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 props 2. 兄弟组件之间通信 3. 跨多层级的组件之间通信 Context API https://react ...

  9. Taro 开发踩坑指南 (小程序,H5, RN)

    Taro 开发踩坑指南 (小程序,H5, RN) css taro 如何展示多行文本省略号 https://www.cnblogs.com/xgqfrms/p/12569057.html UI 设计稿 ...

  10. Apple & HTML5 app

    Apple & HTML5 app https://developer.apple.com/cn/news/?id=09062019b https://developer.apple.com/ ...