Uva 572 Oil Deposits
思路:可以用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的更多相关文章
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- UVA - 572 Oil Deposits(dfs)
题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...
- UVa 572 - Oil Deposits (简单dfs)
Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...
- UVa 572 Oil Deposits (Floodfill && DFS)
题意 :输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循 ...
- ACM:油田(Oil Deposits,UVa 572)
/* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- Oil Deposits UVA - 572
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
随机推荐
- POST 方式上传图片
Post 方式 模仿 form表单 上传 图片 设置enctype = multipart/form-data <form enctype="multipart/form-data&q ...
- zoj 3490
蛋都疼了,高了半天,Output Limit Exceeded 原来是输入的问题,我靠!!以后还是用输入输出c++好,这尼玛!!郁闷!!!!! #include<stdio.h> #inc ...
- iOS解析数据判断nil NULL Null的方法
+ (BOOL)isNil:(NSObject*)obj { if (obj == nil || obj == NULL) { return YES; } if ([obj isKindOfClass ...
- jsp 有哪些动作?作用分别是什么?
答:JSP 共有以下 6 种基本动作jsp:include: 在页面被请求的时候引入一个文件.jsp:useBean: 寻找或者实例化一个 JavaBean.jsp:setProperty: 设置 J ...
- error 和 exception 有什么区别?
1.error指恢复不是不可能但非常困难的情况下的一种严重错误.比如内存溢出,不指望程序可以处理这样的情况. 2.exception表示一种设计或者实现问题,如果程序正常运行,则从不会发生.
- jbpmAPI-5
Part II. jBPM Core 5.1.概述本章介绍了API需要加载过程和执行它们.更多的细节如何定义过程本身,看看在BPMN 2.0章.与流程引擎交互(例如,开始一个过程),你需要建立一个会话 ...
- javascriptDOM编程艺术_学习笔记_知识点 DOM
第三章 getElementById 返回一个与那个有着给定id属性值的元素节点对应的对象. document.getElementById(id) 下面一个例子: document.get ...
- Android 开发笔记“程序安装包APK的制作”
资源来源:http://blog.csdn.net/qualcent/article/details/6959547 完成Android项目后,需要将程序打包成APK文件(Android Packag ...
- Understanding and Selecting a SIEM/LM: Correlation and Alerting
Continuing our discussion of core SIEM and Log Management technology, we now move into event correla ...
- QT太多的内容和模块,怎么办?
我有个问题,QT可以做许多不同的开源项目,而且每个QT新版本都那么内容,感觉学不过来.用不过来那么我们还应该学习和使用其它语言吗? 如果回答,在需要的时候学习,那么这句话意味着,这几年你基本上就局限于 ...