HDU1241(bfs)JAVA

import java.util.Scanner;
public class Main1241 {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
while(cin.hasNext()){
int n=cin.nextInt();
int m=cin.nextInt();
if(n==0){
break;
}
plot [][]plots=new plot[n][m];
for(int i=0;i<n;i++){
String str=cin.next();
for(int j=0;j<m;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;
//print(plots);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(plots[i][j].c=='@'&&!plots[i][j].isvisable){
Plotqueue queue=new Plotqueue();
queue.in(plots[i][j]);
bfs(plots,queue);
count++;
}

}
}
System.out.println(count);

}
}
final static int [][]dir={{-1,0},{0,-1},{-1,-1},{0,1},{1,0},{1,1},{1,-1},{-1,1}};
private static void bfs(plot[][] plots, Plotqueue queue) {
int px;
int py;
int n=plots.length;
int m=plots[0].length;
while(!queue.isempty()){
plot Plots=queue.out();
for(int i=0;i<8;i++){
px=Plots.x+dir[i][0];
py=Plots.y+dir[i][1];
if(px>=0&&px<n&&py>=0&&py<m&&plots[px][py].c=='@'&&!plots[px][py].isvisable){
plots[px][py].isvisable=true;
queue.in(plots[px][py]);

}
}

}
}

private static void print(plot[][] plots) {
for(int i=0;i<plots.length;i++){
for(int j=0;j<plots[i].length;j++){
System.out.print(plots[i][j].c);
}
System.out.println();
}
}
}

class plot{
int x;
int y;
char c;
boolean isvisable=false;

}
class Plotqueue{
plot []Plot;
int end;
final int FRIST=0;
public Plotqueue() {
Plot=new plot[100];

}
public void in(plot p){
Plot[end]=p;
end++;
}
public plot out(){
plot p;
if(end<=0){
return null;
}
else{
p=Plot[FRIST];
for(int i=0;i<end;i++){
Plot[i]=Plot[i+1];
}
end--;
}
return p;
}
public boolean isempty(){
if(end<=0){
return true;
}
return false;

}

}

上一篇:linux:ifconfig命令


下一篇:SpringBoot报错:nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.tuyrk.test.User matching [java.lang.Long, java.lang.String, java.lang.String]