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. LinearLayout按下(pressed)或获取焦点(focused)时背景设置不同颜色或图片

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=&qu ...

  2. Visual Studio 那些隐藏的调试功能(转)

    原文出处:  微软互联网开发支持   Visual Studio 是一个强大的调试工具,里面很多隐藏功能少有人问津,但是在特定场景可以节省你很多时间,本文主要介绍一些Visual Studio调试相关 ...

  3. FRM-40401 No changes to save error

    FAQ: I have updated a table from the form and written a update statement in the when-button-pressed ...

  4. 【转】Mac QQ截图保存在哪里?

    原文网址:http://www.pc6.com/edu/67677.html QQ Mac版的截屏图片保存在哪儿呢?可不可以像Windows版本一样设定保存路径呢?当然是可定的.Mac QQ截图保存你 ...

  5. SharePoint 2010 master page 控件介绍(2):ribbon (一同事读听着像泪奔)

    转:http://blog.csdn.net/lgm97/article/details/6409208 <!-- =====  开始Ribbon ======================= ...

  6. 1.进入debug模式(基础知识列表)

    1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...

  7. MVC系统过滤器、自定义过滤器

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  8. 浅谈MS-SQL锁机制

    锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...

  9. JSON字符串转换成JSON对象

    字符串转对象(strJSON代表json字符串) var obj = eval(strJSON); var obj = strJSON.parseJSON(); var obj = JSON.pars ...

  10. [Stephen]Android的adb无法启动

    1.程序中运行收入cmd,打开dos命令窗口,在窗口中依次运行abd kill-server和  adb start-server 尝试重启adb服务 2.如果依然启动失败  dos命令窗口中键入  ...