BFS和DFS (java版)
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版)的更多相关文章
- 【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)
本文总结了刷LeetCode过程中,有关树的遍历的相关代码实现,包括了二叉树.N叉树先序.中序.后序.BFS.DFS遍历的递归和迭代实现.这也是解决树的遍历问题的固定套路. 一.二叉树的先序.中序.后 ...
- 玩转算法系列--图论精讲 面试升职必备(Java版)
第1章 和bobo老师一起,玩转图论算法欢迎大家来到我的新课程:<玩转图论算法>.在这个课程中,我们将一起完整学习图论领域的经典算法,培养大家的图论建模能力.通过这个课程的学习,你将能够真 ...
- BFS和DFS详解
BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...
- 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 ...
- 剑指offer题解(Java版)
剑指offer题解(Java版) 从尾到头打印链表 题目描述 输入一个链表,按从尾到头的顺序返回一个ArrayList. 方法1:用一个栈保存从头到尾访问链表的每个结点的值,然后按出栈顺序将各个值存入 ...
- BFS与DFS常考算法整理
BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...
- 聊聊算法——BFS和DFS
如果面试字节跳动和腾讯,上来就是先撕算法,阿里就是会突然给你电话,而且不太在意是周末还是深夜, 别问我怎么知道的,想确认的可以亲自去试试.说到算法,直接力扣hard三百题也是可以的,但似乎会比较伤脑, ...
- ArcGIS Server 10 Java 版的Rest服务手动配置方法
Java版的Manager中发布的服务默认只发布了该服务的SOAP接口,而REST接口需要用户在信息服务器,如Tomcat. Apache.WebLogic等中手工配置.由于在Java版的Server ...
- PetaPojo —— JAVA版的PetaPoco
背景 由于工作的一些原因,需要从C#转成JAVA.之前PetaPoco用得真是非常舒服,在学习JAVA的过程中熟悉了一下JAVA的数据组件: MyBatis 非常流行,代码生成也很成熟,性能也很好.但 ...
随机推荐
- 学习QT——GUI的基础用法(2)
1.listWidget列表 在构造函数里面添加: ; i<; i++) { ui->listWidget->addItem(QString::number(i)+"ite ...
- openfire连接数据库mysql
openFire下载地址http://www.igniterealtime.org/downloads/index.jsp#openfire 如果有UAC保护的话,需要右键以管理员方式启动openfi ...
- 03_java基础(八)之static关键字与代码块
20\21.static关键字 /** * static关键字 * 1.用static修饰后的方法,称为静态方法. * 2.静态的方法特点,可以使用 类名.方法名称 调用方法 * 3.静态方法只能调用 ...
- sql语句where条件判断是否是相同的string时 原来不判断大小写
SELECT * from api_check where api ="Worker" SELECT * from api_check where api ="worke ...
- C#之代码优化
1.if和swith: 区别:1.if语句会执行多次判断,增加CPU的消耗,效率较低:switch只判断一次,效率较高 2.if表示的是一个范围,switch表示一个点 2.for和foreach f ...
- cacti报ERROR: unknown option '--border' 解决方法
cacti制图报下面提示 if (isset($rrdborder) && $rrdversion >= 1.4) { $graph_opts .= "--border ...
- burpsuite的使用(二)
爬网 为了爬网更加顺畅,先关掉截断功能 先进行手动爬网 然后进到目标页面需要点的地方和输入的地方去操作一下,在spider下就能看到爬网的记录 自动爬网 当你爬网时需要登录身份验证时,提示输入,也可以 ...
- 解决ie浏览器下载apk或ipa变为zip
Tomcat/conf/web.xml <mime-mapping> <extension>apk</extension> <mime-type>app ...
- 完整性约束&外键变种三种关系&数据的增删改
完整性约束 本节重点: not null 与 default unique primary auto_increment foreign key 一.介绍 约束条件与数据类型的宽度一样,都是可选参数 ...
- cetnos 7 增加新的硬盘
fdisk -l 查看新的硬盘是否挂载 如没有挂载 ls /sys/class/scsi_host/ 查看设备列表 echo "- - - " > /sys/class/sc ...