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. Linux内核中流量控制

    linux内核中提供了流量控制的相关处理功能,相关代码在net/sched目录下:而应用层上的控制是通过iproute2软件包中的tc来实现, tc和sched的关系就好象iptables和netfi ...

  2. EventLog实现事件日志操作

    选中”我的电脑”,在其右键菜单中选择“管理”,在打开的对话框中包括了如下图所示的“日志”信息: 选中其中的某一条日志,可以看到如下的详细信息: 我们应该如何通过写代码的方式向其中添加“日志”呢? 在操 ...

  3. 操作系统杂谈 mac 和linux windows若干概念

    Mac: vmware 安装:1.方式一通过FreeBSD方式用 darwin.iso引导加载dmg安装 2.通过制作cdr /iso  vmware安装mac插件 mac有 macpe 使用open ...

  4. 【转】OS X Mavericks: 防止 Mac 进入睡眠 -- 不错

    原文网址:https://support.apple.com/kb/PH13808?locale=zh_CN&viewlocale=zh_CN 某些 Mac 电脑将在不活跃一段时间后自动进入睡 ...

  5. HTML5 SSE自动推送

    前端页面: <!doctype html> <html> <head> <meta charset="UTF-8"> <tit ...

  6. codeforce 660D Number of Parallelograms

    题意:询问多少个矩形. 统计横纵坐标差,放进vector中 #include<cstdio> #include<cstring> #include<iostream> ...

  7. 如何将SQL Server运行到Windows Azure上

    从2012年6月6日开始,Windows Azure上一些强大的新功能现在可用于预览,包括新的Windows Azure虚拟机(VM).其中有关Windows Azure虚拟机最强大的一件事是他们利用 ...

  8. if

    语句快中的变量与函数的局部变量关系;

  9. POJ 3295 Tautology (构造题)

    字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...

  10. Java读取文件夹大小的6种方法及代码

    (一)单线程递归方式 package com.taobao.test; import java.io.File; public class TotalFileSizeSequential { publ ...