宝岛探险

题目描述

小哼通过秘密方法得到一张不完整的钓鱼岛航拍地图。钓鱼岛由一个主岛和一些附属岛屿组成,小哼决定去钓鱼岛探险。下面这个10*10的二维矩阵就是钓鱼岛的航拍地图。图中数字表示海拔,0表示海洋,1~9都表示陆地。小哼的飞机将会降落在(6,8)处,现在需要计算出小哼降落所在岛的面积(即有多少个格子)。注意此处我们把与小哼降落点上下左右相链接的陆地均视为同一岛屿。

输入

多组输入n,m,x,y

n<=100

m<=100

0<x<=n

0<y<=m

其后n*m个数字

输出

输出面积

样例输入

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.*;
public class Main {
    static int n,m;
    static int[][]a;
    static int[][]b;
    static int sum=1;
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
            n=cin.nextInt();
            m=cin.nextInt();
            int x=cin.nextInt();
            int y=cin.nextInt();
            a=new int[n+1][m+1];
            b=new int[n+1][m+1];
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    a[i][j]=cin.nextInt();
                }
            }
            b[x][y]=1;
            System.out.println(dfs(x,y));
        }
        cin.close();
    }
    private static int dfs(int x, int y) {
        int next[][]={{0,1},{1,0},{0,-1},{-1,0}};
        int k,tx,ty;
        for(k=0;k<=3;k++)
        {
            tx=x+next[k][0];
            ty=y+next[k][1];
            if(tx<1||tx>n||ty<1||ty>m)
                continue;
            if(a[tx][ty]>0&&b[tx][ty]==0)
            {
                sum++;
                b[tx][ty]=1;
                dfs(tx,ty);
            }
        }
        return sum;
    }
}

                                   *

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值