思路: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 1
  2. *
  3. 3 5
  4. *@*@*
  5. **@**
  6. *@*@*
  7. 1 8
  8. @@****@*
  9. 5 5
  10. ****@
  11. *@@*@
  12. *@**@
  13. @@@*@
  14. @@**@
  15. 0 0

Sample Output

  1. 0
  2. 1
  3. 2
  4. 2

Source

Mid-Central USA 1997

  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4. class Point{
  5. public int x;
  6. public int y;
  7. public Point(int x,int y){
  8. this.x=x;
  9. this.y=y;
  10. }
  11. public Point(){}
  12. }
  13. public class Main {
  14. static int[][] dir={{0,1},{0,-1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1}};
  15. public static void main(String[] args) {
  16. // TODO Auto-generated method stub
  17. // TODO Auto-generated method stub
  18. Scanner in=new Scanner(System.in);
  19. int m=in.nextInt();
  20. int n=in.nextInt();
  21. Queue<Point> q=new LinkedList<Point>();
  22. while(m!=0&&n!=0){
  23. int ans=0;
  24. char gra[][]=new char[m][n];
  25. boolean vis[][]=new boolean[m][n];
  26. for(int i=0;i<m;i++){
  27. String s=in.next();
  28. for(int j=0;j<s.length();j++){
  29. gra[i][j]=s.charAt(j);
  30. }
  31. }
  32. for(int i=0;i<m;i++){
  33. for(int j=0;j<n;j++){
  34. if(gra[i][j]=='@'&&vis[i][j]==false){
  35. Point p=new Point(i,j);
  36. bfs(gra,vis,p,m,n,q);
  37. ans++;
  38. }
  39. }
  40. }
  41. System.out.println(ans);
  42. ans=0;
  43. q.clear();
  44. m=in.nextInt();
  45. n=in.nextInt();
  46. }
  47. }
  48. private static void bfs(char[][] gra, boolean[][] vis, Point p, int m,
  49. int n, Queue<Point> q)
  50. {
  51. q.add(p);
  52. vis[p.x][p.y]=true;
  53. while(!q.isEmpty()){
  54. Point qq=q.remove();
  55. for(int i=0;i<8;i++){
  56. int xx=qq.x+dir[i][0];
  57. int yy=qq.y+dir[i][1];
  58. if((!(xx<0||yy<0||xx>=m||yy>=n))&&vis[xx][yy]==false&&gra[xx][yy]=='@'){
  59. vis[xx][yy]=true;
  60. q.add(new Point(xx,yy));
  61. }
  62. }
  63. }
  64. }
  65. }

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. 痞子衡嵌入式:超级下载算法(RT-UFL)开发笔记(3) - 统一FlexSPI驱动访问

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是超级下载算法开发笔记(3)之统一FlexSPI驱动访问. 文接上篇 <超级下载算法(RT-UFL)开发笔记(2) - 识别当前i. ...

  2. asp.net 从Excel表导入数据到数据库中

    http://www.cnblogs.com/hfzsjz/archive/2010/12/31/1922901.html http://hi.baidu.com/ctguyg/item/ebc857 ...

  3. MYSQL查询和插入数据的流程是怎样的

    一个查询语句经过哪些步骤 这次我们从MySQL的整体架构来讲SQL的执行过程,如下图: 在整体分为两部分Server和引擎层,这里引擎层我使用InnoDB去代替,引擎层的设计是插件形式的,可以任意替代 ...

  4. 最新 Steam 免费游戏

    最新 Steam 免费游戏 免费 免费游戏 免费开玩 免费游戏玩的游戏是有内购的. 免费开玩游戏开玩是一部分免费,玩到某个地方要购买才能继续玩. 免费就是永久免费并且无内购. refs https:/ ...

  5. vue & arrow function error

    vue & arrow function error <template> <div class="home"> <img alt=" ...

  6. MySQL 8.x

    MySQL 8.x SQL & NoSQL $ mysql --version # mysql Ver 8.0.21 for osx10.15 on x86_64 (Homebrew) # M ...

  7. auto open Chrome DevTools in the command line

    auto open Chrome DevTools in the command line --auto-open-devtools-for-tabs # macOS $ /Applications/ ...

  8. APC推出鞋底缓震科技 两款中高端跑鞋将陆续上市

    近日,英国知名运动品牌APC(公司编号:08703733)推出了全新的鞋底缓震科技 NOVR,该项技术将首先应用于两款跑步鞋上,随后陆续应用到其他重点鞋类产品. 是对于各大运动品牌来说,鞋底研发一直是 ...

  9. NGK是公链吗?NGK为何会这么火?

    NGK号称区块链3.0的经典代表之作,公链底层技术开源发布,支撑百万级应用场景,集合了其他公链的优点,不仅拥有超高的TPS,军业级DPOSS共识机制,还有极高的网络确认速度和转账速度.更重要的是无需矿 ...

  10. 1060 Are They Equal——PAT甲级真题

    1060 Are They Equal If a machine can save only 3 significant digits, the float numbers 12300 and 123 ...