java数据结构 广搜,队列,循环队列

本文介绍了一种利用广度优先搜索算法来探测和计算油田中不同油藏数量的方法。通过创建网格来模拟土地区域,并使用字符标记油藏位置,算法能够有效地识别相连的油藏并将其归为同一油藏。此方法适用于处理大型数据集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

广度优先搜索在搜索访问一层时,需要记住已被访问的顶点,以便在访问下层顶点时,从已被访问的顶点出发搜索访问其邻接点。所以在广度优先搜索中需要设置一个队列Queue,使已被访问的顶点顺序由队尾进入队列。在搜索访问下层顶点时,先从队首取出一个已被访问的上层顶点,再从该顶点出发搜索访问它的各个邻接点。




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
0 1 2 2


package cn.hncu.serch;



import java.util.Scanner;


public class serchmethodBFS {
static int dir[][]={
{0,-1}, //上
{0,1},  //下
{-1,0}, //左
{1,0},  //右
{-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){
return;
}
serchmethodBFS bfs=new serchmethodBFS();//调用内部类
Plot plots[][]=new Plot[m][n];
for(int i=0;i<m;i++){
String str=sc.next();
for(int j=0;j<n;j++){
plots[i][j]=bfs.new Plot(i, j, str.charAt(j));
}
}
int count=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(plots[i][j].c=='@'&&!plots[i][j].isVisit){
PlotQueue queue=bfs.new PlotQueue();
queue.add(plots[i][j]);
bfs(plots,queue);
count++;
}
}
}
System.out.println(count);
}
}


private static void bfs(Plot[][] plots, PlotQueue queue) {
int px;
int py;
int m=plots.length;
int n=plots[0].length;
while(!queue.isEmpty()){
Plot p=queue.pop();
for(int i=0;i<dir.length;i++){
px=p.x+dir[i][0];
py=p.y +dir[i][1];
if(px>=0&&px<m&&py>=0&&py<n&&plots[px][py].c=='@'&&!plots[px][py].isVisit){
plots[px][py].isVisit=true;
queue.add(plots[px][py]);
}
}
}

}


class Plot{
private int x;//如果是外部类private 不能访问
private int y;
private boolean isVisit=false;
private char c;
public Plot(int x, int y, char c) {
this.x = x;
this.y = y;
this.c = c;
}
public Plot(){

}
}
class PlotQueue{
private int end;
private final int FRONT =0;
private Plot plots[];
public PlotQueue() {
end=0;
plots=new Plot[100]; 
}
public void add(Plot p) {
plots[end]=p;
end++;
}
public Plot pop(){
if(end<=0){
return null;
}
Plot p=plots[FRONT];
for(int i=0;i<end;i++){
plots[i]=plots[i+1];
}
end--;
return p;
}
public boolean isEmpty(){
if(end<=0)
 return true;
else
 return false;
}
}

}


下面是演示队列

class Circling {
private int frant;
private int end;
final int MAX_SIZE=9;
private Plot plot[];
public Circling() {
this.frant = 0;
this.end = 0;
this.plot = new Plot[MAX_SIZE];
}
public void add(Plot p){
if(isFull()){
return;
}
plot[end]=p;
end=(end+1)%MAX_SIZE;
}
public Plot pop(){
if(isEmpty()){
return null;
}
Plot p=plot[frant];
frant=(frant+1)%MAX_SIZE;
return p;
}
public boolean isEmpty(){
if(end==frant){
//System.out.println("isEmpty");
return true;
}
return false;
}
private boolean isFull(){
if((end+1)%MAX_SIZE==frant){
//System.out.println("isFull");
return true;
}
return false;
}
public void print(){
int i=frant;
while(i!=end){
// Plot p=pop();
System.out.println( plot[i] +" " );
i=(i+1)%MAX_SIZE;
}
System.out.println("__________");
}
/*public static void main(String[] args) {
Circling queue = new Circling();
//1 测试队列为空时的执行情况
// queue.add(new Plot(1,1));queue.print();
// queue.pop();queue.print();
// //2 测试队列为满时的执行情况
// queue.add(new Plot(1,1));queue.print();
// queue.add(new Plot(2,2));queue.print();
// queue.add(new Plot(3,3));queue.print();
// queue.add(new Plot(4,4));queue.print();
// queue.add(new Plot(5,5));queue.print();
// queue.add(new Plot(6,6));queue.print();
// 3 测试队列循环入与出
// queue.add( new Plot(1,1) ); queue.print();
//   queue.add( new Plot(2,2) ); queue.print();
//   queue.add( new Plot(3,3) ); queue.print();
//   queue.add( new Plot(4,4) ); queue.print();
//   queue.pop();queue.print();
//   queue.add( new Plot(5,5) ); queue.print();
//   queue.pop();queue.print();queue.pop();queue.print();
//   queue.add( new Plot(6,6) ); queue.print();
//   queue.add( new Plot(7,7) ); queue.print();
//   queue.add( new Plot(7,7) ); queue.print();
}*/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值