宝岛探险-(BFS)

题目:
ljlsister意外找到一个宝藏地图,这时候她想实现自己一夜暴富的梦想,所以,她下定决心要去探险…

第一题:
给定一个地图,是一个n行m列的地图,她坐飞机进入到宝岛中的其中一个岛屿,其中每一个坐标的值为0代表是海洋,非零代表是岛屿,她想知道她降落到一个岛屿,如何知道这个岛屿的面积是多少?请你帮助她计算一下:
**输入格式:**第一行四个参数,n,m,a,b,其中n和m代表是地图的大小,a,b表示ljlsister降落时候的坐标。
**输出格式:**一个数字,代表降落的岛屿的面积。
分析:只需要对降落的哪一点进行bfs操作,每到达一个地方,就对这个地方进行标记为已访问。

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

public class treasure {
	//宽搜
	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(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。最后只需要知道进行了多少次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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WeChat098

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

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

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

打赏作者

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

抵扣说明:

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

余额充值