1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
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
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u011479875/article/details/47282747