日韩性视频-久久久蜜桃-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 )的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 石原莉奈在线播放 | 成人网免费| 日韩亚洲精品在线 | 全部免费毛片在线播放 | 欧美日韩一区二区在线观看视频 | 人妻洗澡被强公日日澡电影 | 国产精品探花一区二区三区 | 国产精伦 | 婷婷综合激情网 | 精品91自产拍在线观看二区 | 日本在线观看www | 中文字幕第七页 | 91精品国产综合久久福利 | 91网站观看 | 日本做受 | 国产成人短视频在线观看 | 在线不欧美| a毛毛片| 999久久久国产精品 韩国精品一区二区 | 88av在线视频 | 日本在线二区 | 最新中文字幕在线 | 日韩美女激情视频 | 韩国禁欲系高级感电影 | 亚洲777 | 国产无遮挡呻吟娇喘视频 | 亚洲天堂男 | 麻豆国产91在线播放 | 中文字幕一区二区三区免费 | 日本成人精品视频 | 青青伊人av | 青青草这里只有精品 | 亚洲欧洲国产日韩 | 亚洲欧美一区二区三区四区 | 18视频在线观看娇喘 | 波多野结衣福利视频 | 波多野结衣潜藏淫欲 | 永久视频在线 | 成人在线观看网 | 国产精品一区二区久久国产 | 亚洲国产精品系列 | 台湾佬美性中文娱乐网 | a毛片在线观看 | 毛片在线视频 | 丁香花完整视频在线观看 | 国产综合精品一区二区三区 | 国产日产欧美一区二区 | 精品国产乱码久久久久久1区2区 | 国产91黄色 | 久操影视| 免费精品在线视频 | 亚洲精品乱码久久久久久国产主播 | 日韩av在线播放网址 | 丝袜在线一区 | 暗呦丨小u女国产精品 | 日韩欧美成人一区二区 | 亚洲精品男女 | 奇米影视第4色 | 亚洲三级在线 | 99reav| 日韩毛片在线 | 国内老熟妇对白hdxxxx | 国产91免费观看 | 国产aaaaaaa | 无码人妻精品一区二区三区夜夜嗨 | 福利在线一区二区 | 久久观看| 婷婷色六月 | 日日躁夜夜躁aaaabbbb | 欧美变态另类刺激 | 又欲又污又肉又黄短文 | 中文字幕乱码人妻无码久久95 | 欧美日韩不卡在线 | 波多野结衣av中文字幕 | 国产av无码专区亚洲精品 | 能在线看的av | 四虎成人在线观看 | 91华人在线 | 国产91丝袜在线播放0 | 久久久久久久久免费视频 | 亚洲欧美日韩国产精品 | 中文字幕亚洲一区二区三区五十路 | 午夜影院私人 | 超碰香蕉 | 亚洲一页| 日本欧美一区二区三区不卡视频 | 日本极品丰满ⅹxxxhd | 日本视频免费在线播放 | 91黄版 | 欧美色妞网 | 欧美视频亚洲 | 一区二区三区天堂 | 中文字幕无人区二 | 国产在线高清视频 | 韩国成人理伦片免费播放 | 夜夜操夜夜爱 | 国产天堂网 | 进去里视频在线观看 | 久草电影在线 |