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;
}
}
HDU1241(bfs)JAVA的更多相关文章
- 面试10大算法汇总+常见题目解答(Java)
原文地址:http://www.lilongdream.com/2014/04/10/94.html(为转载+整理) 以下从Java的角度总结了面试常见的算法和数据结构:字符串,链表,树,图,排序,递 ...
- "《算法导论》之‘图’":深度优先搜索、宽度优先搜索(无向图、有向图)
本文兼参考自<算法导论>及<算法>. 以前一直不能够理解深度优先搜索和广度优先搜索,总是很怕去碰它们,但经过阅读上边提到的两本书,豁然开朗,马上就能理解得更进一步. 下文将会用 ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [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 ...
- [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 ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- [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', ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- DFS和BFS(无向图)Java实现
package practice; import java.util.Iterator; import java.util.Stack; import edu.princeton.cs.algs4.* ...
随机推荐
- 安装gem invalid date format in specification错误的解决方法
别的不说,报错信息直接贴图: 解决方法: 1.找到你环境目录下的spec,例如:D:\Ruby187\lib\ruby\gems\1.8\specifications. 2.找到引起错误文件的gems ...
- 【HDOJ】2045 不容易系列之(3)—— LELE的RPG难题
着色问题,递推,当超过3个块时,规律明显,此时可以是n-2的头尾重复+与头尾不同颜色,也可以是n-1+与头尾均不相同眼色情况.经典递推.注意long long. #include <stdio. ...
- 【PythonChallenge】Level 4
如题,是一个链表,N多数据,其中还有其它操作,比较麻烦,也是刚学python网络编程.对于Perl的RE很熟悉,还没有学python的,还是啃手册吧.其中在读出16044时,并没有找到匹配项,如下图所 ...
- 【HDOJ】1098 Ignatius's puzzle
数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...
- servlet读取cookie问题
String sessionid = request.getSession().getId(); // 取得当前的session id ckSessionid = new Cookie("s ...
- How to Set Word Document Properties with C#
Word properties shows a brief description about one document. Through properties, we can learn gener ...
- Android 中HttpClient和HttpURLConnection选取
原文地址:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 译文:http://yunfeng.sin ...
- iframe 处理
import java.io.File; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org ...
- domReady source code, domready源码
domready源码,domready实现代码,js实现domready的源代码. jquery的domready源码,require.js的domready源代码 . [domReady sourc ...
- Java语言使用HttpClient模拟浏览器登录
使用HttpClient来模拟浏览器登录网站,然后可以进行操作,比如发布信息等 第一步:获取实际的post网址,(不考虑复杂情况下) 1.需要使用到firefox的httpfox插件,httpfox中 ...