广度优先搜索在搜索访问一层时,需要记住已被访问的顶点,以便在访问下层顶点时,从已被访问的顶点出发搜索访问其邻接点。所以在广度优先搜索中需要设置一个队列Queue,使已被访问的顶点顺序由队尾进入队列。在搜索访问下层顶点时,先从队首取出一个已被访问的上层顶点,再从该顶点出发搜索访问它的各个邻接点。
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
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();
}*/
}