宝岛探险-BFS

题目:
ljlsister最近捡到了一张宝藏地图,她想去这个地方探险,希望通过来这里探险可以得到宝藏,一夜暴富…
题目一:
她雇来一个飞机,她乘坐降落伞到达地图中的某一个坐标,这个地图是有一些岛屿组成,她现在想知道她现在降落的岛屿的面积:
输入:
第一行四个参数n,m,a,b:n,m表示地图的大小,a,b降落的位置坐标
接下来n行m列表示地图,0代表是海域,大于零的代表是岛屿。
她想知道她降落的岛屿中岛屿的面积是多少。
分析:
这道题我们只需要将ljlsister进入的那个岛屿通过bfs进行搜索,只要扩展出一个点,我们就对area加一,到最后可以观察到右多少个:

import java.util.LinkedList;
import java.util.Scanner;

public class One报道探险 {
	//宽搜
	public static int shu[][];
	public static boolean flag[][];
	public static int a,b;//ab代表地图的大小
	public static int startx,starty,area = 1,max = 0;
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		a = input.nextInt();
		b = input.nextInt();
		startx = input.nextInt();
		starty = input.nextInt();
		shu = new int[a][b];
		flag = new boolean[a][b];
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				shu[i][j] = input.nextInt();
			}
		}
		bfs(startx,starty);
		System.out.println(area);
	}
	public static void bfs(int startx,int starty) {
		LinkedList<Point> list = new LinkedList<>();
		list.addLast(new Point(startx,starty,0));
		flag[startx][starty] = true;
		int next[][] ={{0,1},{1,0},{0,-1},{-1,0}};//表示方向变量
		int tx,ty;
		while(!list.isEmpty()) {
			Point temporacy = list.removeFirst();
			for (int i = 0; i < next.length; i++) {
				tx = temporacy.x+next[i][0];
				ty = temporacy.y+next[i][1];
				if (tx < 0 || tx >= a || ty < 0 || ty >= b) {
					continue;
				}
				if (flag[tx][ty] == false && shu[tx][ty] > 0) {
					flag[tx][ty] = true;
					list.addLast(new Point(tx, ty,temporacy.area+1));
					area++;
				}
			}
		}
	}
}
class Point{
	int x;
	int y;
	int area;
	public Point(int x,int y,int area) {
		super();
		this.x =x;
		this.y =y;
		this.area = area;
	}
}

题目二:
要求她从地图最左上角出发,可以向东南西北四个方向移动,她想知道这个地图中有多少个岛屿。
分析:
这道题我们可以再上一题的基础上对地图进行遍历,如果当前一个点暂时还没有被访问,并且当前这个点的值是大于零,代表当前这个点是岛屿,这个时候我们就对这个点在进行访问,重新进行bfs。

import java.util.LinkedList;
import java.util.Scanner;

public class One报道探险 {
	//宽搜
	public static int shu[][];
	public static boolean flag[][];
	public static int a,b;//ab代表地图的大小
	public static int startx,starty,area = 1,max = 0;
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		a = input.nextInt();
		b = input.nextInt();
//		startx = input.nextInt();
//		starty = input.nextInt();
		shu = new int[a][b];
		flag = new boolean[a][b];
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				shu[i][j] = input.nextInt();
			}
		}
		bfs();
		System.out.println(max);
	}
	public static void bfs() {
		for (int i = 0; i < shu.length; i++) {
			for (int j = 0; j < shu.length; j++) {
				if (flag[i][j] == false && shu[i][j] > 0) {
					bfs(i,j);
					max++;
				}
			}
		}
	}
	public static void bfs(int startx,int starty) {
		LinkedList<Point> list = new LinkedList<>();
		list.addLast(new Point(startx,starty,0));
		flag[startx][starty] = true;
		int next[][] ={{0,1},{1,0},{0,-1},{-1,0}};//表示方向变量
		int tx,ty;
		while(!list.isEmpty()) {
			Point temporacy = list.removeFirst();
			for (int i = 0; i < next.length; i++) {
				tx = temporacy.x+next[i][0];
				ty = temporacy.y+next[i][1];
				if (tx < 0 || tx >= a || ty < 0 || ty >= b) {
					continue;
				}
				if (flag[tx][ty] == false && shu[tx][ty] > 0) {
					flag[tx][ty] = true;
					list.addLast(new Point(tx, ty,temporacy.area+1));
					area++;
				}
			}
		}
	}
}
class Point{
	int x;
	int y;
	int area;
	public Point(int x,int y,int area) {
		super();
		this.x =x;
		this.y =y;
		this.area = area;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WeChat098

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值