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 /ə ...
随机推荐
- URAL 1132 Square Root(二次剩余定理)题解
题意: 求\(x^2 \equiv a \mod p\) 的所有整数解 思路: 二次剩余定理求解. 参考: 二次剩余Cipolla's algorithm学习笔记 板子: //二次剩余,p是奇质数 l ...
- Battery API All In One
Battery API All In One https://caniuse.com/?search=Battery navigator.getBattery() /* Promise {<pe ...
- VSCode & outline & source code
VSCode & outline & source code Dart 源码学习 outline 速览 dart-core List class instance-methods ht ...
- js spider
js spider https://gist.github.com/xgqfrms-GitHub/0bf82ff06037a0d1776c9f30033cbfd1 https://www.cnblog ...
- c++ 去掉字符串首尾空格
http://www.cplusplus.com/reference/regex/regex_replace/ #include <iostream> #include <regex ...
- Baccarat凭什么能成为DeFi后时代火爆新趋势?
在各币种经历涨涨跌跌以后,DeFi后时代已然来临.那么,当前DeFi市场中哪个项目更被市场生态建设者看好呢?毫无疑问,Baccarat会成为最被看好的DeFi项目. Baccarat采用了独特的共识算 ...
- NGK DeFi Baccarat怎么玩能赚钱?
市面上大多数DeFi项目都是基于以太坊来开发的,除了吞吐量低.存储量小以及交易速度慢等问题以外,高额的Gas手续费将不少终端用户拒之门外. 基于此NGK.IO推出了低门槛的DeFi项目-- Bacca ...
- list 打乱排序
public IList<T> RandomSortList<T>(List<T> ListT) { Random random = new Random(); L ...
- 图文详解:Kafka到底有哪些秘密让我对它情有独钟呢?
- Java多线程并发编程/锁的理解
一.前言 最近项目遇到多线程并发的情景(并发抢单&恢复库存并行),代码在正常情况下运行没有什么问题,在高并发压测下会出现:库存超发/总库存与sku库存对不上等各种问题. 在运用了 限流/加锁等 ...