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. Linux基本操作指令

    Linux操作指令 到达当前用户目录:cd ~ 获得管理员权限执行:sudo 解压缩:tar -zxf XXX.tgz 安装包:dpkg -i XXX.deb 通过链接下载文件:wget  http: ...

  2. SQL Server和MySQL数据库

    导读:接下来的网上商城的项目,需要用到MySQL数据库了.这个对于我来说,是一个新接触的东西,按照惯例,在刚开始学习一个东西的时候,先从宏观上去了解它.本篇博客,先介绍SQL Server的基本内容, ...

  3. css flex 兼容ios android--商品展示 添加购物车

    https://blog.csdn.net/u010035608/article/details/52711248 <!DOCTYPE html> <html> <hea ...

  4. 接收Android数据 递归显示表格数据

    <html> <head> <title>展示</title> <script type="text/javascript" ...

  5. 04_web基础(二)之web构建

    03.04.05.06web项目创建 07.第一个Servlet程序 1.拷贝tomcat 中的 servlet-api.jar 在lib包下面 2.新建一个HelloWordServlet类并实现 ...

  6. cdh 安装系列3--cdh manager 安装 cdh 6.01 版本

    安装前提是cdh manager 已经可以通过admin登录,管理台安装在192.168.20.163 一.安装自动TLS Setup Auto-TLS 1.ssh 192.168.20.163 2. ...

  7. 解题10(LongestSubStrBetweenAB)

    题目描述 查找两个字符串a,b中的最长公共子串.若有多个,输出在较短串中最先出现的那个. 输入描述: 输入两个字符串 输出描述: 返回重复出现的字符 示例1 输入 abcdefghijklmnop a ...

  8. spring boot 中使用 Redis 与 Log

    spring boot + mybatis + redis 配置 1.application.yml #配置访问的URLserver: servlet-path: /web port: spring: ...

  9. day24 面向对象三大特性之封装

    本周内容 组合 封装 多态 面向对象高级 异常处理 网络协议 通讯原理 互联网协议 TCP/UDP 基于TCP协议的套接字 上周回顾 1.xml,os,os.path 2.ATM+购物车 三层结构 3 ...

  10. 正则表达式pattern属性

    你发现自己多久匆匆编写一些正则表达式验证一个特定的文本. 多亏了新的pattern属性,我们可以在标签处直接插入一个正则表达式. <form action="" method ...