Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38101 Accepted Submission(s): 22096
Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
import java.util.Scanner;
public class P1241 {
final static int dir[][]={ {-1,0}, //上
{1,0},//下
{0,-1},//左
{0,1},//右
{-1,-1},//左上
{1,-1},//左下
{-1,1},//右上
{1,1},//右下
};
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int m=sc.nextInt();
int n=sc.nextInt();
if(m==0&&n==0){
break;
}
//初始化图(标记):颜色、父节点、距离 //输入并初始化图
Plot plot[][]=new Plot[m][n];
for(int i=0;i<m;i++){//m行
String str = sc.next();
for(int j=0;j<n;j++){//n列
plot[i][j]=new Plot();//没有该行,会出现空指针异常
plot[i][j].x=i;
plot[i][j].y=j;
plot[i][j].c=str.charAt(j);
}
}
//print(plot);
//查找,看有几片连通区域---看需要进行几次广搜
int count=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(plot[i][j].c=='@' && !plot[i][j].isVisted){
//从plots[i][j]节点发起一次广搜
PlotQueue Que=new PlotQueue();
Que.add(plot[i][j]);
bfs(plot,Que);//实现广搜中的while功能
count++;
}
}
}
System.out.println(count);
}
}
private static void bfs(Plot[][] plot, PlotQueue Que) {
int px,py;//当前所搜索(邻居)的位置:x行,y列
int m=plot.length;//行数
int n=plot[0].length;//列数
while(!Que.isEmpty()){
Plot plot1=Que.pop();
//把plot节点的所有邻居节点加到Que中
for(int i=0;i<8;i++){
px = plot1.x+dir[i][0];
py = plot1.y+dir[i][1];
if( px>=0 && px<m && py>=0&& py<n && //注意,这一行不能写在下一行的后面,否则无法利用短路原理防护
plot[px][py].c=='@' && !plot[px][py].isVisted ){
plot[px][py].isVisted=true;
Que.add(plot[px][py]);
}
}
}
}
private static void print(Plot plot[][]) {
for(int i=0;i<plot.length;i++){
for(int j=0;j<plot[i].length;j++){
System.out.print(plot[i][j].c);
}
System.out.println();
}
}
}
class Plot {
int x,y;//位置:x行,y列
char c;//*或@
boolean isVisted=false;
}
class PlotQueue {
Plot plot[];
int end;
public PlotQueue() {
plot = new Plot[100];
}
//进队列
public void add(Plot p){
plot[end]=p;
end++;
}
//出队列
public Plot pop(){
//防护
if(end<=0){
return null;
}
//至少有一个元素,返回队首(第0个元素)
Plot p = plot[0];
if(end>1){ //多于1个元素时,后续的元素需要往前挪
for(int i=0;i<end;i++){
plot[i]=plot[i+1];
}
}
end--;
return p;
}
public boolean isEmpty(){
return end==0;
}
}