题目:海岸线长度

该问题要求在N×M的网格地图中计算所有岛屿的海岸线总长度,地图由陆地和水域组成,相邻陆地算作连接。输入包含地图尺寸及表示陆水状态的矩阵,输出海岸线总长度。解决方案是先在外围添加一圈水域,确保最外层为海洋,然后从左上角开始深度优先搜索,标记可达水域,并在遍历地图时累加陆水边界以得出海岸线长度。
摘要由CSDN通过智能技术生成

You will be given a map of Soteholm as an N×M grid. Each square in the grid has a side length of 1 km and is either water or land. Your goal is to compute the total length of sea coast of all islands. Sea coast is all borders between land and sea, and sea is any water connected to an edge of the map only through water. Two squares are connected if they share an edge. You may assume that the map is surrounded by sea. Lakes and islands in lakes are not contributing to the sea coast.

Figure 1: Gray squares are land and white squares are water. The thick black line is the sea coast. This example corresponds to Sample Input.

Input:

The first line of the input contains two space separated integers N and M where 1 ≤ N, M ≤ 1000. The follo

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值