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=0 && nx=0 && 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


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代码  点击打开链接

你可能感兴趣的:(搜索,HDOJ水过的那些题)