日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

搜索算法(三)--DFS/BFS求解宝岛探险问题(JAVA )

發布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 搜索算法(三)--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 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。