宝岛探险

题目描述

一个小岛由一个主岛和一些复附属岛屿组成,该岛使用一个二维矩阵表示,其中数字表示海拔,0表示海洋,1~9表示陆地。探险家乘坐飞机降落在(6,8)处,现在需要统计探险家降落的小岛的面积大小,我们将探险家降落点上下左右相连接的陆地视作同一个岛屿

测试样例

10 10 6 8
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

测试结果

38

示范代码

import java.util.Scanner;

/*
测试数据
10 10 6 8
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
* /

/**
 * Created by handsome programmer with IntelliJ IDEA.
 * User: chen
 * Date: 18-12-6
 * Time: 上午3:03
 * Description: 宝岛探险
 *
 * @author chen
 */
public class Search {
    public static void main(String[] args) {
        // 定义四个方向
        int[][] next = {
                // 向右走
                {0, 1},
                // 向左走
                {0, -1},
                // 向上走
                {1, 0},
                // 向下走
                {-1, 0}
        };

        Scanner scanner = new Scanner(System.in);
        // 地图大小,n行m列
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        // 起始位置
        int startX = scanner.nextInt();
        int startY = scanner.nextInt();

        // 标记数组
        int[][] mark = new int[n + 1][m + 1];
        // 遍历队列
        Node[] queue = new Node[n * m + 1];

        // 加载地图
        int[][] map = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                map[i][j] = scanner.nextInt();
            }
        }
        // 初始化队列两个游标
        int head = 1;
        int tail = 1;
        // 将降落的起始坐标插入队列
        Node node = new Node(startX, startY);
        queue[tail++] = node;
        // 将降落点标记为已遍历
        mark[startX][startY] = 1;
        int sum = 1;
        while (head < tail) {
            // 枚举当前点的四个方向
            for (int i = 0; i < 4; i++) {
                // 下一个的坐标
                int x = queue[head].getX() + next[i][0];
                int y = queue[head].getY() + next[i][1];
                // 判断是否越界
                if (x < 1 || x > n || y < 1 || y > m) {
                    continue;
                }
                // 判断是否遍历过该点并且改点是否是陆地
                if (map[x][y] != 0 && mark[x][y] == 0) {
                    sum++;
                    // 入队该节点并标记为已遍历
                    queue[tail++] = new Node(x, y);
                    mark[x][y] = 1;
                }
            }
            head++;
        }
        // 输出岛的大小
        System.out.println("岛的大小为:" + sum);
    }
}


class Node {
    /**
     * 横坐标
     */
    private int x;
    /**
     * 纵坐标
     */
    private int y;

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Node() {
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值