思路:可以用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. 图画hadoop -- 生态圈

  2. zoj 2110

    这道题困扰我的不是算法问题.而是细节问题.不优化一直搜到底 时间是690ms左右 没有优化的dfs #include<stdio.h> #include<string.h> # ...

  3. 当前项目与当前环境的JDK版本不匹配”Bad version number in .class file“

    java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLoader.d ...

  4. 怎样用Excel自动排成绩

    经常需要做表格,排成绩名次总是笨笨的一个一个填,费时又费力,终于找到了解决的办法%>_<%(不要嘲笑我了(✿◡‿◡)),原来就是一个名叫RANK的函数,还在苦逼地自己输的小伙伴们,快来看吧 ...

  5. ORA-02396: exceeded maximum idle time, please connect again的原因

    一般为了防止过多活动的session占用资源,可以对允许连接到数据库的session个数,已连接到数据库的session空闲时间等进行限制(当然也可以对尝试连接次数等其它很多内容进行限制).方式就是可 ...

  6. python3.4 使用pymssql 乱码

    问题:sqlserver数据库编码为gbk,使用python3.4+pymssql 查询,中文乱码 解决办法: conn = pymssql.connect(host="192.168.12 ...

  7. 设置windows密码只存在NTLM-Hash下

    修改注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 下添加名为"NoLMHash"的DWORD值,并设置为1 ...

  8. PowerShell Remove all user defined variable in PowerShell

    When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...

  9. inline-block 垂直居中

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 怎么给没链接的flash加超链接

    最近开始准备设计一个广告条,本想用阿里妈妈的的banner marker来设计,却遗憾的发现,banner marker已经实行收费模式了. 我不得不启用另一款在线banner生成工具,百度旗下的&q ...