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 ...
随机推荐
- zoj 2966 Build The Electric System
就是套了个prim算法就ac了 #include <stdio.h> #include <string.h> #define MaxInt 0x3f3f3f3f #define ...
- Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览
1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示 ...
- Matlab中的取整-floor,ceil,fix,round
FLOOR Round towards minus infinity. FLOOR(X) rounds the elements of X to the nearest integers toward ...
- iOS工程上传AppStore时遇到的问题“ERROR ITMS-90046”解析
在我们将代码写完整,测试没有bug之后,我们就可以将它上传到AppStore了,上传的过程只要操作正确并不会有太大的问题,但是打包的过程中会出现一些小问题,导致打的包不能上传或者上传的时候会出现错误. ...
- 注册表缺失导致Windows Server 2008 R2时钟服务W32time不能自启
参照@飘云 http://blog.csdn.net/piaoyunqing/article/details/6323647 的文章. 测试环境中有一台Windows Server 2008 R2的虚 ...
- 数组名和指针的深入理解(C++)
指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很多程序设计者就被搞糊涂了.魔幻数组名请看程序(本文程序在WIN32平台下编译): #includ ...
- jquery左右自适应伸缩插件
简单css布局 body { margin: 0 auto; padding: 0 auto; } .showpannal { position:absolute; left: 200px; top: ...
- 调不尽的内存泄漏,用不完的Valgrind
调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-X.X.X.tar.bz2 2. 解压安装包:tar –jxvf ...
- AttributeError: 'module' object has no attribute 'Thread'
$ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last): File "th ...
- HDU 1847 Good Luck in CET-4 Everybody!
题解:巴什博弈,2^k+1=3N或2^k2=3N,所以3N为P-position,3N+r为N-position. #include <cstdio> int main(){ int n; ...