hdu1241(Oil Deposits)

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

import java.util.Scanner;

public class SearchMethodBFS {
	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();//如果此处用nextLine(),则前面输入n时的换行符应该先吸掉
				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){
						//广搜
						PlotQue queue = new PlotQue();
						queue.add(plots[i][j]);
						bfs(plots,queue);
						count++;
					}
				}
			}
			System.out.println(count);
		}
	}

	private static void bfs(Plot[][] plots, PlotQue queue) {
		int px,py;//当前所搜的位置:第px行py列
		int m = plots.length;//地图的行数
		int n = plots[0].length;//地图的列数
		
		while(!queue.isEmpty()){
			Plot plot = queue.pop();
			for(int i=0;i<8;i++){
				px=plot.x+dir[i][0];
				py=plot.y+dir[i][1];
				if(px>=0&&px<m && py>=0&&py<n  &&
				   plots[px][py].c=='@' && !plots[px][py].isVisited){
					plots[px][py].isVisited = true;
					queue.add(plots[px][py]);
				}
			}
		}
	}
	
	final static int dir[][]={
		{0,-1}, //上
		{0,1}, //下
		{-1,0}, //左
		{1,0}, //右
		{-1,-1}, //左上
		{1,-1}, //右上
		{-1,1}, //左下
		{1,1}, //右下
	};
}

class Plot{
	int x,y;//位置:第x行y列
	char c;
	boolean isVisited=false;
}

class PlotQue{
	Plot plots[];
	final int FRONT =0;
	int end;
	public PlotQue(){
		plots = new Plot[100];
		end = 0;
	}
	public void add(Plot p){
		plots[end] = p;
		end++;
	}
	public Plot pop(){
		if(end<=0){
			return null;
		}
		Plot p = plots[FRONT];
		//出队列:把队首的元素从队列中移出
		if(end>1){
			for(int i=0;i<end;i++){
				plots[i] = plots[i+1];
			}
		}
		end--;		
		return p;
	}
	public boolean isEmpty(){
		if(end<=0){
			return true;
		}
		return false;
	}
}


优化:循环队列

import java.util.Scanner;

public class SearchMethodBFS {
	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;
			}
			
			//输入图(初始化)
			PPlot plots[][] = new PPlot[m][n];
			for(int i=0;i<m;i++){
				String str = sc.next();//如果此处用nextLine(),则前面输入n时的换行符应该先吸掉
				for(int j=0;j<n;j++){
					plots[i][j] = new PPlot();
					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){
						//广搜
						
						CirclingQueue queue = new CirclingQueue();
						queue.add(plots[i][j]);
						bfs(plots,queue);
						count++;
					}
				}
			}
			System.out.println(count);
		}
	}

	private static void bfs(PPlot[][] plots, CirclingQueue queue) {
		int px,py;//当前所搜的位置:第px行py列
		int m = plots.length;//地图的行数
		int n = plots[0].length;//地图的列数
		
		while(!queue.isEmpty()){
			PPlot plot = queue.pop();
			for(int i=0;i<8;i++){
				px=plot.x+dir[i][0];
				py=plot.y+dir[i][1];
				//※px代表的是行号,行号的边界要用m来界定。py同理
				if(px>=0&&px<m && py>=0&&py<n  &&
				   plots[px][py].c=='@' && !plots[px][py].isVisited){
					plots[px][py].isVisited = true;
					queue.add(plots[px][py]);
				}
			}
		}
	}
	
	final static int dir[][]={
		{0,-1}, //上
		{0,1}, //下
		{-1,0}, //左
		{1,0}, //右
		{-1,-1}, //左上
		{1,-1}, //右上
		{-1,1}, //左下
		{1,1}, //右下
	};
}

class PPlot{
	int x,y;//位置:第x行y列
	char c;
	boolean isVisited=false;
	public PPlot(int x, int y) {
		this.x = x;
		this.y = y;
	}
	public PPlot() {
	}
	@Override
	public String toString() {
		return "PPlot [x=" + x + ", y=" + y + ", c=" + c + ", isVisited="
				+ isVisited + "]";
	}
	
}

class CirclingQueue{
	PPlot plots[];
	int front;
	int end;
	private final int MAX_SIZE=5;
		
	public CirclingQueue(){
		plots = new PPlot[MAX_SIZE];
		front =0;
		end = 0;
	}
	
	public void add(PPlot p){
		if(isFull()){
			System.out.println("Adding failure,the queue is Full!!!");
		}else{
			plots[end] = p;
			end = (end+1)%MAX_SIZE;
		}
	}
	private boolean isFull() {
		if((end+1)%MAX_SIZE==front){
			return true;
		}else{
		  return false;
		}
	}
	public boolean isEmpty(){
		return front==end ? true : false;
	}
	
	public PPlot pop(){
		if(isEmpty()){
			System.out.println("Poping failure,the queue is Empty!!!");
			return null;
		}
		
		PPlot p = plots[front];
		front = (front+1)%MAX_SIZE;
		return p;
	}
}


深搜也可以:http://blog.csdn.net/u011479875/article/details/47210941





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值