思路:可以用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. HDU1171:Big Event in HDU(多重背包分析)

    通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...

  2. Linux新手笔记 sudo

    centos 6.4 32bit 你是也像我一样,厌烦了在root用户和个人用户之间来回切换.或者干脆直接用root用户.可以这样设置,然后在命令前加sudo 即可使用自己到密码,临时用root身份执 ...

  3. spring 加载配置文件的相关配置总结

    PropertyPlaceholderConfigurer      注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:p ...

  4. Assertion failure in -[UIView layoutSublayersOfLayer:]

    Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.137/UIView.m:8 ...

  5. Android应用开发基础篇(6)-----Service

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/20/2360336.html 一.概述 我们知道,Service是Android的四大组件之一. ...

  6. (转)WIN2003服务器禁PING的方法

    方法一:用windows系统自带的防火墙规则设置禁止别人Ping我的机器 win2003系统默认情况下,所有Internet控制消息协议(ICMP)选项均被禁用,也就是对客户机有反应,因而易于受到攻击 ...

  7. PHP的环境搭建

    下载开发环境 wampserver 下载sublime text 2 sublime使用技巧 1:安装漂亮的编程字体http://pan.baidu.com/s/1xMex9 下载"程序编写 ...

  8. javamail发送二进制流附件的问题

    最近做个邮件发送功能,要内嵌图片并有附件. 需求很奇怪,图片和附件文件是放在ftp服务器上的,查了下javamail的文档. 添加附件方法如下 MimeBodyPart messageBodyPart ...

  9. C winpcap 网络抓包 并获取IP TCP 协议的相关信息

    以太网协议分析函数: void ethernet_protocol_packet_handle (u_char *argument, const struct pcap_pkthdr *packet_ ...

  10. Delphi调试DLL 不能调试 不能进入调试 注意!!!

    如何调试DLL,在这里就不再赘述了,但是,今天就碰到了一个特别奇怪的问题,参数设置正确,就是不能调试?? 通过上网查资料,发现了问题,注意: 1, 将Project主菜单的Project Option ...