搜索算法(三)--DFS/BFS求解宝岛探险问题(JAVA )
宝岛探险问题
问题描述:某片海域有诸多岛屿,用0表示海洋,1-9表示陆地,现给定一个岛屿上的坐标点,求解所在岛屿的面积
思路:显然这是一个搜索算法,即只要从当前坐标点开始遍历,每遍历到一个点进行计数即可,但是要注意sum的初始值为1!!!
Input:
10 10
1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0
6 8
Output:
38
DFS
import java.util.Scanner;public class DFS {static int[][] a = new int[50][50];static int[][] book = new int[50][50];static int sum = 1;static int n, m;static Scanner input = new Scanner(System.in);public static void main(String[] args) {n = input.nextInt();m = input.nextInt();for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {a[i][j] = input.nextInt();}}int startX = input.nextInt();int startY = input.nextInt();book[startX][startY] = 1;dfs(startX, startY);System.out.println(sum);}public static void dfs(int x, int y) {int[][] next = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int tx, ty;for (int i = 0; i < 4; i++) {tx = x + next[i][0];ty = y + next[i][1];if(tx < 0 || tx > n - 1 || ty < 0 || ty > n - 1) {continue;}if (a[tx][ty] > 0 && book[tx][ty] == 0) {sum ++;book[tx][ty] = 1;dfs(tx, ty);}}return;}
}
BFS
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;class node {int x;int y;node(int x, int y) {this.x = x;this.y = y;}
}
public class BFS {static int[][] a = new int[50][50];static int[][] book = new int[50][50];static int n, m;static int sum = 1;static Queue<node> queue = new LinkedList<>();static Scanner input = new Scanner(System.in);public static void main(String[] args) {n = input.nextInt();m = input.nextInt();for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {a[i][j] = input.nextInt();}}int startX = input.nextInt();int startY = input.nextInt();queue.offer(new node(startX, startY));book[startX][startY] = 1;bfs();System.out.println(sum);}public static void bfs() {int[][] next = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int tx, ty;while (!queue.isEmpty()) {for (int i = 0; i < 4; i++) {tx = queue.peek().x + next[i][0];ty = queue.peek().y + next[i][1];if (tx < 0 || tx > n - 1 || ty < 0 || ty > n - 1) {continue;}if(a[tx][ty] > 0 && book[tx][ty] == 0) {queue.offer(new node(tx, ty));sum++;book[tx][ty] = 1;}}queue.remove();}return;}
}
拓展:漫水填充法(FloodFill)
问题: 要求解区域中总共有多少岛屿??
思路:对每个点进深搜,对于每个大于0的点进行填充负值,负值每次-1,最后输出正的这个值,即是岛屿个数。
Input:
10 10
1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0
Output:
import java.util.Scanner;public class DFS {static int[][] a = new int[50][50];static int[][] book = new int[50][50];static int sum = 1;static int num = 0;static int n, m;static Scanner input = new Scanner(System.in);public static void main(String[] args) {n = input.nextInt();m = input.nextInt();for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {a[i][j] = input.nextInt();}}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (a[i][j] > 0) {num--;book[i][j] = 1;dfs(i, j, num);}}}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {System.out.print(a[i][j] + "\t");}System.out.println();}System.out.println(-num);}public static void dfs(int x, int y, int color) {int[][] next = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int tx, ty;a[x][y] = color;for (int i = 0; i < 4; i++) {tx = x + next[i][0];ty = y + next[i][1];if(tx < 0 || tx > n - 1 || ty < 0 || ty > n - 1) {continue;}if (a[tx][ty] > 0 && book[tx][ty] == 0) {sum ++;book[tx][ty] = 1;dfs(tx, ty, color);}}return;}
}
搜索算法(三)--DFS/BFS求解宝岛探险问题(JAVA )相关推荐
- 搜索算法(二)--DFS/BFS求解炸弹人问题(JAVA )
炸弹人问题 问题描述:小人可以在迷宫中任意地方放置一个炸弹,炸弹可以在以该点为中心的十字方向杀死怪物,但是触碰到墙之后不再能传递攻击.求将一个炸弹放在哪个位置可以杀死更多的怪物?? Input: 13 ...
- 搜索算法(一)--DFS/BFS求解拯救同伴问题(JAVA)
拯救同伴问题 问题描述:假设有如下迷宫,求解从某一点出发到目标位置的最短距离 Input: 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 1 4 3 Ou ...
- 1.5万字详述 | 全开源:python写小游戏+AI强化学习与传统DFS/BFS控制分别实现
简介:本周的强化学习我们来到实践部分.我以我在 GitHub 上开源的项目 PiperLiu / Amazing-Brick-DFS-and-DRL 为对象,从零开始与各位朋友分享:如何用 pytho ...
- 宝岛探险(C语言 )(DFS+BFS)
宝岛探险 某岛是由一个主岛和一些附属岛屿组成,小哼决定去某岛探险.下面这个10*10的二维矩阵就是某岛的航拍地图.图中数字表示海拔,0表示海洋,1~9表示陆地.小哼的飞机将会降落在(6,8)处,现在需 ...
- DFS BFS简单理解
文章目录 BFS DFS介绍 实现思路 DFS BFS怎么应用 DFS BFS对比 又水了一篇博客呜呜,第一次尝试写DFS和BFS,做题也迷迷糊糊,看着大佬文章简单写了写总结,后续会补上DFS和BFS ...
- 数据结构——dfs/bfs
文章目录 dfs/bfs 迷宫 OpenJ_Bailian - 2790 走迷宫 OpenJ_Bailian - 3752 迷宫问题 POJ - 3984 迷宫(一) 计蒜客 - T1595 迷宫(二 ...
- 2018-2-22 《啊哈,算法》再练习广度优先搜索,题:炸怪兽, 2-23改用深度优先搜索。宝岛探险(广度,深度,及地图着色)2-24水管工游戏,2-25测试水管工代码...
2小时. 先是是纠错,通过对代码运行过程的测试.发现是变量打错.以及录入地图❌. 重构练习题,改使用while..end代替for in. ⚠️ : 在while(k <= n)中如果用到nex ...
- java bfs dfs_java优先搜索(DFS/BFS)实际应用
深度优先搜索DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.广度优先搜索BFS是Breadth First Sear ...
- 洛谷 P1710 地铁涨价 (dfs+bfs)
地铁涨价 题目描述 博艾市除了有海底高铁连接中国大陆.台湾与日本,市区里也有很成熟的轨道交通系统.我们可以认为博艾地铁系统是一个无向连通图.博艾有N个地铁站,同时有M小段地铁连接两个不同的站. 地铁计 ...
最新文章
- android 模板 ui布局,Android UI布局
- git中的gitgnore是什么? 码云gitee
- 【NLP】基于预训练的中文NLP工具介绍:ltp 和 fastHan
- 使用XmlReader读Xml
- ES6入门之Generator函数
- 慎用Hyper-Threading Technology
- 应届毕业生面试软件测试工程师时应注意什么?
- 联合促经济 伟库网在江门普及SaaS
- 500G JAVA视频网盘分享 JEECG开源社区
- 使用wamp3.0.6安装LimeSurvey时报“参数默认值只能为NULL”错误的解决办法
- c语言牛顿法求整数平方根,牛顿法求平方根-编程练习
- 自己做量化交易软件(40)小白量化实战13--Alpha101及自编因子公式
- 想找个这样的男朋友,要求高吗?
- idea中为啥要用maven
- docker入门课程
- java 混淆war,java yguard混淆web工程
- 使用motan+Zookeeper构建RPC服务
- 中职计算机优质课教学设计,中职教师优秀教学设计
- mysql4.0事务_聊一聊 MySQL 中的事务及其实现原理
- 2021年临颖一高高考成绩查询,2021年漯河高考状元是谁分数多少分,历年漯河高考状元名单...
热门文章
- HTML+CSS+JS实现燃烧的火焰火花动画特效
- 《SpringCloud超级入门》Spring Cloud Eureka是什么?《八》
- 2014 java面试题_2014 java面试题 (答案)
- mysql 组合索引 or_mysql索引优化实例(单列索引与组合索引)
- 合同相似可逆等价矩阵的关系及性质_行列式的性质问题
- 首款搭载鸿蒙os的设备,华为发布会配件汇总,首款搭载 鸿蒙OS 的设备来了
- mysql 5.6 command line client闪退_MySQL 5.6 Command Line Client 点开闪退解决方法
- python 八大排序_八大排序算法的 Python 实现
- qt弹框输入密码_Android仿支付宝密码输入框
- java 配置jmstemplate_SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解...