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 非常流行,代码生成也很成熟,性能也很好.但 ...
随机推荐
- asp.net4.0
asp.net4.0安装路径:C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
- Spark之机器学习(Python版)(一)——聚类
https://www.cnblogs.com/charlotte77/p/5437611.html
- c3 新特性
渐变 线性渐变 .line { height: 100px; /*线性渐变语法*/ background-image: linear-gradient( to right,/* ...
- HTML各种标签复习
<html> --开始标签 <head> 网页上的控制信息 <title>页面标题</title> </head> <bod ...
- 2018面向对象程序设计(Java)第14周学习指导及要求
2018面向对象程序设计(Java)第14周学习指导及要求(2018.11.29-2018.12.2) 学习目标 (1) 掌握GUI布局管理器用法: (2) 掌握各类Java Swing组件用途及 ...
- ubuntu下zaibbix3.2报警搭建
1.安装sudo apt install sendmail 2.测试发送邮件: echo "正文!" | mail -s 标题 XXX@qq.com 3.成功后继续安装邮件服务器. ...
- 弹性盒子 flexbox 元素居中
1 .navtext{ width:800px; height:600px; border: 1px solid black; justify-content:center; align-items: ...
- Spring Boot application.yml bootstrap.yml
yml与properties 其实yml和properties文件是一样的原理,且一个项目上要么yml或者properties,二选一的存在. 推荐使用yml,更简洁. bootstrap与appli ...
- python文件操作之二进制
列表项 三元运算符号: a=3 b=7 val=a if a>b else val=b print(val) 文件处理 首先给你一个文件,或者自己建立一个文件,那如何查看文件的内容呢? 1.安装 ...
- leetcode 中等题(2)
50. Pow(x, n) (中等) double myPow(double x, int n) { ; unsigned long long p; ) { p = -n; x = / x; } el ...