思路:可以用DFS求解。遍历这个二维数组,没发现一次未被发现的‘@’,便将其作为起点进行搜索。最后的答案,是这个遍历过程中发现了几次为被发现的‘@’

import java.util.*;

public class Main{

    public static void main(String[] args) {
Scanner in = new Scanner( System.in );
while( true ) {
int rows, cols;
rows = in.nextInt(); // read the rows
cols = in.nextInt(); // read the cols
if( rows == 0 && cols == 0 ) { // the input end
break;
}
OilDeposits od = new OilDeposits( rows, cols );
od.readGrids( in ); // read the strings
System.out.println( od.getPocketsAmount() );
}
}
} class OilDeposits{
private int rows, cols;
private char grids[][];
private boolean vis[][];
private final int dir[][] = { // the eight directions
{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1}
}; private final char EMPTY = '*';
private final char EXIST = '@'; public OilDeposits( int rows, int cols ) {
this.rows = rows;
this.cols = cols;
this.grids = new char[ rows + 5 ][ cols + 5 ];
this.vis = new boolean[ rows ][ cols ];
for( int i=0; i<rows; i++ ) {
for( int j=0; j<cols; j++ ) {
this.vis[i][j] = false;
}
}
} private boolean isValid( int row, int col ) { // whether it can visit this position
if( row < 0 || row >= this.rows || col < 0 || col >= this.cols || vis[row][col] || this.grids[row][col] == EMPTY ) {
return false;
}
return true;
} public void readGrids( Scanner in ) {
String str;
for( int i=0; i<rows; i++ ) {
this.grids[i] = in.next().toCharArray();
}
} public void Search( int row, int col ) { // search this grid and its adjacent grids
if( !this.isValid( row, col ) ) {
return ;
}
this.vis[row][col] = true;
for( int i=0; i<8; i ++ ) {
Search( row+dir[i][0], col+dir[i][1] );
}
} public int getPocketsAmount() {
int counter = 0;
for( int i=0; i<this.rows; i++ ) {
for( int j=0; j<this.cols; j++ ) {
if( this.grids[i][j] == '@' && !this.vis[i][j] ) {
counter ++;
this.Search( i, j );
}
}
}
return counter;
}
}

其中之所以将Scanner的对象进行传递,是因为如果重新声明一个Scanner的对象,会使输入出错,错误的具体表现为一直等待输入,尽管已经在Console中输入了数据。猜测可能是未关闭上一个Scanner的对象导致的,所以这里将Scanner的对象进行传递。

Uva 572 Oil Deposits的更多相关文章

  1. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  2. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

  3. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  4. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  5. UVA - 572 Oil Deposits(dfs)

    题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...

  6. UVa 572 - Oil Deposits (简单dfs)

    Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...

  7. UVa 572 Oil Deposits (Floodfill && DFS)

    题意 :输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循 ...

  8. ACM:油田(Oil Deposits,UVa 572)

    /* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  9. Oil Deposits UVA - 572

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

随机推荐

  1. 配置vim之插件

    涉及linux平台下ctags, taglist插件 ctags是一个用于产生代码索引文件的插件,它产生的索引可以帮助我们更快的定位到特定位置. ctags支持很多语言,比如java, c, c++, ...

  2. JavaWEB HTTP请求中POST与GET的区别

    From 的get 和post方法.在数据传输过程中分别对应了HTTP协议中的GET和POST方法. 二者主要区别: GET从服务其获取数据;POST上传数据. GET将表单中的数据按照variabl ...

  3. mapreduce 关于小文件导致任务缓慢的问题

    小文件导致任务执行缓慢的原因: 1.很容易想到的是map task 任务启动太多,而每个文件的实际输入量很小,所以导致了任务缓慢 这个可以通过 CombineTextInputFormat,解决,主要 ...

  4. SAE上使用本地sql文件建表时出错解决方法

    在SAE上部署网站时需要上传本地的数据库结构,我也导出了本地数据库为sql文件,但是上传到SAE上时遇到了如下错误: MySQL 返回: #1044 - Access denied for user ...

  5. ffmpeg 命令详解

    常用视频命令 //转码(并添加黑边) ffmpeg -ss start_time -i input.mp4 -t total_duration -vcodec copy/h264/... -acode ...

  6. iter, yield与enumerate的实现

    模拟实现一个enumerate函数 def myEnumerate(seq, start=0): results = [] n = start for i in seq: results.append ...

  7. c# zip file and folder programmatically

    In .net 4.5 Framework, we can zip a file by this way: private static string CompressFile(string sour ...

  8. 【转】使用Boost Graph library(一)

    转自:http://shanzhizi.blog.51cto.com/5066308/942970 本文是一篇译文,来自:http://blog.csdn.net/jjqtony/article/de ...

  9. 阿里云ECS每天一件事D2:配置防火墙

    在linux中配置防火墙是一件比较有风险的事情,尤其是在ECS中,一个不当心的操作,可能就会需要重置服务器. 包括阿里云的官方建议,不要配置防火墙,没有必要什么的吧啦吧啦…… (http://bbs. ...

  10. QT不让windows休眠的方法

    对于一些Windows应用程序,必须要保证os不能休眠才能有效工作,如迅雷下载软件,如果os进入休眠,则会导致网络不正常,从而导致不能下载东西.那木有没有1种机制,当打开软件的时候,就自动将os设为不 ...