POJ1562_Oil Deposits(JAVA语言)
思路: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
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语言)的更多相关文章
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
- Atitit onvif协议获取rtsp地址播放java语言 attilx总结
Atitit onvif协议获取rtsp地址播放java语言 attilx总结 1.1. 获取rtsp地址的算法与流程1 1.2. Onvif摄像头的发现,ws的发现机制,使用xcf类库1 2. 调用 ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- Java语言中的面向对象特性总结
Java语言中的面向对象特性 (总结得不错) [课前思考] 1. 什么是对象?什么是类?什么是包?什么是接口?什么是内部类? 2. 面向对象编程的特性有哪三个?它们各自又有哪些特性? 3. 你知 ...
- JAVA语言搭建白盒静态代码、黑盒网站插件式自动化安全审计平台
近期打算做一个插件化的白盒静态代码安全审计自动化平台和黑盒网站安全审计自动化平台.现在开源或半开源做黑盒网站安全扫描的平台,大多是基于python脚本,安全人员贡献python脚本插件增强平台功能.对 ...
- 关于Java语言和面向对象记录
本科时常用的c语言是面向过程的语言,而Java是面向对象的语言 Java语言的11个关键术语 简单性.可移植性.面向对象.分布式.高性能.解释型.健壮性.多线程.安全性.动态性.体系结构中立 面向对象 ...
- 用Java语言编写一个简易画板
讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...
- 【百度文库课程】Java语言基础与OOP入门学习笔记一
一. Java的历史与由来 原名Oak,针对嵌入式系统开发设计,语法与C/C++基本一致 二. Java语言特点 Java由四方面组成:Java编程语言.Java类文件格式.Java虚拟机和Java应 ...
- 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词
第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...
随机推荐
- 从.NET看微软的焦虑
节日没事,就像聊聊微软的NET. 1.孩子静悄悄,必定在作妖 截止目前,微软的市值达到1.85万亿美元,按说,这样一个宙斯级的巨无霸应该过的非常舒坦, 但是,和微软市值成鲜明的反差,我们从.NET的发 ...
- Self-XSS All In One
Self-XSS All In One Self-XSS(自跨站脚本)攻击 警告! 使用此控制台可能会给攻击者可乘之机,让其利用 Self-XSS(自跨站脚本)攻击来冒充您并窃取您的信息.请勿输入或粘 ...
- css infinite loop animation
css infinite loop animation @keyframes loop { 0% { transform: translateX(0%); } constructed styleshe ...
- Kotlin & Android & Swift & Flutter & React Native
Kotlin & Android https://www.runoob.com/kotlin/kotlin-tutorial.html Swift 5 & iOS 12 https:/ ...
- input number step
input number step <!DOCTYPE html> <html> <body> <h1>The input step attribute ...
- .gitignore规则不生效
.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的. 解决方法就是先把本地缓存删除(改变成未track状态),然后再提交 ...
- NGK.IO的智能合约是炒作还是未来商业的主流?
随着NGK主网的上线,NGK市场也备受关注.目前,NGK代币价格已经由初始价格0.0215美元涨到现在的0.86美元,代币价格上涨40倍!数字货币市场也已经将重点目光放到了NGK代币上,相信在不久的将 ...
- 离场定高转弯DF与CF的对比
也许是刚学会CAD的缘故,配合风螺旋插件,画图的感觉真是蛮爽的,忍不住画了一张又一张. 接着昨天的离场保护区,我们来聊一下PBN指定高度转弯保护区的画法.指定高度转弯的计算本身没有太多复杂的地方,真正 ...
- c# winform中窗体切换后释放及防止重复生成
问题1:窗体切换后如何关闭,并释放资? c# winform中,2个窗体,form1和form2,互相切换的时候执行 this.Hide(); Form2 form2 = new Form2(); f ...
- list 打乱排序
public IList<T> RandomSortList<T>(List<T> ListT) { Random random = new Random(); L ...