HDOJ 1241 油田 BFS(使用普通队列或循环队列) 2种方法

初级BFS算法

题目链接:点击打开链接

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31633    Accepted Submission(s): 18362


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

代码实现:

package cn.hncu.serch.bfs;

import java.util.Scanner;

public class P1241 {
	final static int dir[][]={		//表示8个方向
		{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}
	};
	static boolean isGoIn;		//判断是否进入
	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){
				break;
			}
			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]=new Plot();
					plots[i][j].x=i;
					plots[i][j].y=j;
					plots[i][j].c=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].isVisited){
						//从plots[i][j]发起一次广搜,搜出一片油田并且把搜过节点涂色(作标记)
						PlotQueue queue=new PlotQueue();	//普通队列
//						CirclingQueue queue=new CirclingQueue();	//循环队列
						plots[i][j].isVisited=true;		//入队列前标记已经搜
						queue.add(plots[i][j]);
						isGoIn=false;
						bfs(plots,queue,m,n);	//开始搜索,搜出一片连通
						//统计搜索次数
						if(isGoIn){
							count++;
						}
					}
				}
			}
			System.out.println(count);
		}
	}

	private static void bfs(Plot[][] plots, PlotQueue queue,int m,int n) {
		while(!queue.isEmpty()){
			isGoIn=true;
			Plot p=queue.pop();
			for(int i=0;i<dir.length;i++){
				//遍历弹出来的结点的邻居
				int nx=p.x+dir[i][0];
				int ny=p.y+dir[i][1];
				if(nx>=0 && nx<m && ny>=0 && ny <n					//防止越界
						&& plots[nx][ny].c=='@' && !plots[nx][ny].isVisited){
					plots[nx][ny].isVisited=true;		//入队列前标记已经搜
					queue.add(plots[nx][ny]);
				}
			}
		}
	}
}
package cn.hncu.serch.bfs;

public class Plot {
	int x,y;	//点的坐标
	char c;
	boolean isVisited=false;
}
1)普通队列

package cn.hncu.serch.bfs;

public class PlotQueue {
	
	Plot plots[]=new Plot[100];
	final int FORNT=0;
	int end;
	
	public boolean isEmpty(){
		return end==0;
	}
	
	public void add(Plot plot){
		plots[end++]=plot;
	}
	
	public Plot pop(){
		
		if(isEmpty()){
			return null;
		}
		
		Plot p=plots[FORNT];
		//如果超过一个元素就把每个元素,往前挪一位
		if(end>1){
			for(int i=0;i<end;i++){
				plots[i]=plots[i+1];
			}
		}
		end--;
		return p;
	}
}


2)循环队列

package cn.hncu.serch.bfs;

public class CirclingQueue {
	//最多只能存储MAX_SIZE-1个元素,留一个位置用来区分,队列满和空
	private Plot plots[];
	private final int MAX_SIZE=80;
	private int front;
	private int end;
	
	public CirclingQueue() {
		plots=new Plot[MAX_SIZE];
		front=0;
		end=0;
	}
	
	public boolean isEmpty(){
		//end与front位置重合时,表示队列空
		return front==end;
	}
	
	public boolean isFull(){
		//预留一个空间,当end距离front差一个位置时,表示队列满
		return (end+1)%MAX_SIZE==front;
	}
	
	public void add(Plot plot){
		if(isFull()){
			return;
		}
		
		plots[end]=plot;
		end=(end+1)%MAX_SIZE;		//使指针循环
	}
	
	public Plot pop(){
		if(isEmpty()){
			return null;
		}
		
		Plot p=plots[front];
		front=(front+1)%MAX_SIZE;		//使指针循环
		return p;
	}
}

附:DFS代码  点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值