package com.algorithm.test;

 import java.util.ArrayDeque;
import java.util.Scanner; public class DfsAndBfs { private static final int[][] dir = {
{,},
{,-},
{,-},
{,},
{,},
{-,-},
{-,},
{-,},
}; private static int[][] vis;
private static char[][] map = {
{'*','*','*','*','@'},
{'*','@','@','*','@'},
{'*','@','*','*','@'},
{'@','@','@','*','@'},
{'@','@','*','*','@'},
}; private static Node q; private static Node pos; private static ArrayDeque<Node> que; private static Scanner cin; public static void main(String[] args) {
cin = new Scanner(System.in);
// cinDataForBfs();
cinDataForDfs();
} public static void cinDataForBfs() {
while(cin.hasNext()) {
int m = cin.nextInt();
int n = cin.nextInt();
if(m == && n == ) {
break;
}
int ans = ;
for(int i = ; i < m; i++) {
for(int j = ; j < n; j++) {
if(map[i][j] == '@') {
ans++;
bfs(i,j,m,n);
}
}
}
System.out.println(ans);
}
} public static void cinDataForDfs() {
while(cin.hasNext()) {
int m = cin.nextInt();
int n = cin.nextInt();
if(m == && n == ) {
break;
}
int ans = ;
for(int i = ; i < m; i++) {
for(int j = ; j < n; j++) {
if(map[i][j] == '@') {
ans++;
dfs(i,j,m,n);
}
}
}
System.out.println(ans);
}
} public static void dfs(int x, int y, int m, int n) {
for(int i = ; i < ; i ++) {
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx >= && xx < n && yy >= && yy < m && map[xx][yy] == '@'){
map[xx][yy] = '*';
dfs(xx,yy,m,n);
}
}
} public static void bfs(int x, int y,int m, int n) {
vis = new int[][];
for(int i = ; i < ; i ++) {
for(int j = ; j < ; j++) {
vis[i][j] = ;
}
}
que = new ArrayDeque<Node>();
que.clear();
pos = new Node();
q = new Node();
pos.x = x;
pos.y = y;
vis[x][y] = ;
que.add(pos);
while(!que.isEmpty()) {
pos = que.poll();
map[pos.x][pos.y] = '*';
for(int i = ; i < ; i++) {
int next_x = pos.x + dir[i][];
int next_y = pos.y + dir[i][];
if(next_x >= && next_x < m && next_y >= && next_y < n && vis[next_x][next_y] == ) {
q.x = next_x;
q.y = next_y;
vis[next_x][next_y] = ;
map[next_x][next_y] = '*';
que.add(q);
}
}
} } }
class Node{
public int x, y;
}

BFS和DFS (java版)的更多相关文章

  1. 【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)

    本文总结了刷LeetCode过程中,有关树的遍历的相关代码实现,包括了二叉树.N叉树先序.中序.后序.BFS.DFS遍历的递归和迭代实现.这也是解决树的遍历问题的固定套路. 一.二叉树的先序.中序.后 ...

  2. 玩转算法系列--图论精讲 面试升职必备(Java版)

    第1章 和bobo老师一起,玩转图论算法欢迎大家来到我的新课程:<玩转图论算法>.在这个课程中,我们将一起完整学习图论领域的经典算法,培养大家的图论建模能力.通过这个课程的学习,你将能够真 ...

  3. BFS和DFS详解

    BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...

  4. hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. 剑指offer题解(Java版)

    剑指offer题解(Java版) 从尾到头打印链表 题目描述 输入一个链表,按从尾到头的顺序返回一个ArrayList. 方法1:用一个栈保存从头到尾访问链表的每个结点的值,然后按出栈顺序将各个值存入 ...

  6. BFS与DFS常考算法整理

    BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...

  7. 聊聊算法——BFS和DFS

    如果面试字节跳动和腾讯,上来就是先撕算法,阿里就是会突然给你电话,而且不太在意是周末还是深夜, 别问我怎么知道的,想确认的可以亲自去试试.说到算法,直接力扣hard三百题也是可以的,但似乎会比较伤脑, ...

  8. ArcGIS Server 10 Java 版的Rest服务手动配置方法

    Java版的Manager中发布的服务默认只发布了该服务的SOAP接口,而REST接口需要用户在信息服务器,如Tomcat. Apache.WebLogic等中手工配置.由于在Java版的Server ...

  9. PetaPojo —— JAVA版的PetaPoco

    背景 由于工作的一些原因,需要从C#转成JAVA.之前PetaPoco用得真是非常舒服,在学习JAVA的过程中熟悉了一下JAVA的数据组件: MyBatis 非常流行,代码生成也很成熟,性能也很好.但 ...

随机推荐

  1. 使用 Asp.Net Response.Write() 制作实时进度条

    准备: 一个 StudyResponse.aspx 页面和 CodeBehind 文件. Web 页面中的内容如下: <%@ Page Language="C#" AutoE ...

  2. js判断json对象中是否含有某个属性

    obj.hasOwnProperty("key"); 原地址:https://blog.csdn.net/feicongcong/article/details/53463872

  3. setting 常用配置

    一,保存logging 信息 # 保存log信息的文件名 LOG_LEVEL = "INFO" LOG_STDOUT = True LOG_ENCODING = 'utf-8' # ...

  4. 给RabbitMQ发送消息时,设置请求头Header。

    消费者的请求头 生产者设置请求头 由于消费者那里,@Payload是接受的消息体,使用了@Header注解,需要请求头,生产者这边就要设置请求头,然后rabbitTemplate再调用convertA ...

  5. docker run option

    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Options: --add ...

  6. opencv 对RGB图像直接二值化

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  7. js-for (var in )遍历顺序乱了

    存放的key 为如下: “01”,“02”,“03”,········“10”,“11”,“12” 遍历之后“10”,“11”,“12”, “01”,“02”,“03”,········ 解决办法:把 ...

  8. python网络编程之开启进程的方式

    标签(空格分隔): 开启进程的方式 multiprocessing模块介绍: python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyth ...

  9. jms版本

    Java消息服务是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914). 2001年6月25日,Java消息服务发布JMS 1.0.2b,2002年3月18日Java消息服务发布 1 ...

  10. openstack(Pike 版)集群部署(六)--- Horizon 部署

    一.介绍 参照官网部署:https://docs.openstack.org/horizon/pike/install/    继续上一博客进行部署:http://www.cnblogs.com/we ...