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. 新建gradle文件

    按照新建自动步骤,建好文件后,在build-gradle 里面 写上: allprojects { group 'aaaa' version '1.0-SNAPSHOT' apply plugin: ...

  2. IDEA导入jar包

    http://blog.csdn.net/a153375250/article/details/50851049

  3. 跨年操作--new Date()

    //时间在2017/12/31 17:00 --- 2018/1/1 06:00区间,提示用户无法操作公告. //time.js (function(){ var date = new Date(); ...

  4. 面试真题-----hashMap原理

    HashMap详解   JDK1.8对HashMap底层的实现进行了优化,例如引入红黑树的数据结构和扩容的优化等 简介 Java为数据结构中的映射定义了一个接口java.util.Map HashMa ...

  5. 环境变量之path的一点理解

    最初安装java环境时一直不明白为什么要配置环境变量,百度了一下还是理解不透彻. 后来安装python时也要配置环境变量.. 在经过未配置和配置的操作后,才有些理解path的含义. 1.未配置环境变量 ...

  6. js获取url参数,直接获取url中文

    function GetQueryString(name)//name参数名称 { var reg = new RegExp("(^|&)"+ name +"=( ...

  7. Snipaste截图

    Snipaste 是一个简单但强大的贴图工具,同时也可以执行截屏.标注等功能. 引自: https://blog.csdn.net/qq_36279445/article/details/702109 ...

  8. Javascript Property Names

    [Javascript Property Names] Property names must be strings. This means that non-string objects canno ...

  9. oracle学习--循环语句

    oracle学习--循环语句  loop循环: create or replace procedure pro_test_loop is i number; begin i:=0; loop i:=i ...

  10. oracle 简单输出语句与赋值

    输出: declare stuName varchar2(30); stuAge number; begin stuName:='jack'; stuAge:=30; dbms_output.put_ ...