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;

}

}

HDU1241(bfs)JAVA的更多相关文章

  1. 面试10大算法汇总+常见题目解答(Java)

    原文地址:http://www.lilongdream.com/2014/04/10/94.html(为转载+整理) 以下从Java的角度总结了面试常见的算法和数据结构:字符串,链表,树,图,排序,递 ...

  2. "《算法导论》之‘图’":深度优先搜索、宽度优先搜索(无向图、有向图)

    本文兼参考自<算法导论>及<算法>. 以前一直不能够理解深度优先搜索和广度优先搜索,总是很怕去碰它们,但经过阅读上边提到的两本书,豁然开朗,马上就能理解得更进一步. 下文将会用 ...

  3. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  6. [LeetCode] 529. Minesweeper 扫雷

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  7. [LeetCode] 752. Open the Lock 开锁

    You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...

  8. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  9. DFS和BFS(无向图)Java实现

    package practice; import java.util.Iterator; import java.util.Stack; import edu.princeton.cs.algs4.* ...

随机推荐

  1. Pizza pieces

    Pizza pieces Description In her trip to Italy, Elizabeth Gilbert made it her duty to eat perfect piz ...

  2. BZOJ_2049_[Sdoi_2008]_Cave_洞穴勘测_(LCT/并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2049 给出一个森林,起始互不相连,现在有link和cut两种操作,问x,y是否在一棵树里. 分 ...

  3. Native Fullscreen JavaScript API (plus jQuery plugin)

    http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/ HTML5 <video> is gre ...

  4. iOS8新增加的frameworks, 在目前基于7以上开发的情况下, 使用下列sdk要注意设置成optional

    Added frameworks: AVKitCloudKitCoreAudioKitCoreAuthenticationHealthKitHomeKitLocalAuthenticationMeta ...

  5. ArrayList源码解析

    ArrayList简介 ArrayList定义 1 public class ArrayList<E> extends AbstractList<E> implements L ...

  6. VS.NET2010水晶报表安装部署

    水晶报表VS2010版IDE安装标准版SAP Crystal Reports, version for Visual Studio 2010 - Standard: 下载地址: http://down ...

  7. oracle自动执行一个sql文件的脚本

    如图 文件夹 打开文件夹,如图 autoExeSyn-local.bat文件里的内容如下 @echo off -----------用户/密码-实例-------------执行的sql文件---&g ...

  8. @DataProvider Method 参数传递

    package roger.testng; import java.lang.reflect.Method; import org.testng.annotations.DataProvider; i ...

  9. java md5方法 for Android

    很简单的方法 public static String md5(String string) { byte[] hash; try { hash = MessageDigest.getInstance ...

  10. oracle 分区和分区索引

    一.个人理解:建表时一般都会指定在一个表空间上,但是可能随着表空间扩大,查询越来越慢,分区表就是将一个表实际存在不同的表空间,oracle存储分为块,断,表空间.新建一个表,会给表分配指定大小的段,段 ...