LeetCode463. Island Perimeter

2023-12-03 13:52

本文主要是介绍LeetCode463. Island Perimeter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.

Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).

The island doesn’t have “lakes”, meaning the water inside isn’t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.

Example 1:

Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:

Input: grid = [[1]]
Output: 4
Example 3:

Input: grid = [[1,0]]
Output: 4

Constraints:

row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j] is 0 or 1.
There is exactly one island in grid.

二、题解

遍历所有值为1的节点,若周围节点是水或边界,则周长+1。

class Solution {
public:int islandPerimeter(vector<vector<int>>& grid) {int dirs[4][2] = {1,0,0,1,-1,0,0,-1};int m = grid.size(),n = grid[0].size();int res = 0;for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(grid[i][j] == 1){for(int k = 0;k < 4;k++){int x = i + dirs[k][0];int y = j + dirs[k][1];if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0) res++;}   }}}return res;}
};

这篇关于LeetCode463. Island Perimeter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/449679

相关文章

[LeetCode] 695. Max Area of Island

题:https://leetcode.com/problems/max-area-of-island/description/ 题目 Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizont

[IOI2008]Island

题目地址 思路: 无向基环树森林求直径和。对于每颗基环树,它对答案的贡献只有可能经过环或不经过,分类讨论即可。首先处理出每个点往下的最大深度,顺便进行树形DP,再用单调队列获取经过环时的答案即可。最终树形DP与单调队列的结果取max即可。(注意内存,数组需要使用技巧二次利用)  (该代码内存超限) #pragma GCC diagnostic error "-std=c

[LightOJ 1265] Island of Survival (概率)

LightOJ - 1265 一个岛上有若干只虎,若干只鹿,一个人 每天只有两个动物会相见 如果人和虎相见,人死 如果鹿和虎相见,鹿死 如果虎和虎相见,虎死 其他情况均没有伤亡,各种情况均等概率 问人活到虎全死光的概率有多少 感觉二维dp直接搞正确性很显然 但是网上有另一种做法,就是直接忽略掉鹿的存在,当没有鹿 不是很懂这样做的正确性,网上的解释是鹿吃与被吃, 与人

HDU4280 Island Transport【最大流】【SAP】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4280 题目大意: 有N个岛屿,M条双向道路。每条路每小时最多能通过Ci个人。给你N个岛屿的坐标。问:一个小时内, 最多能将多少游客从最西边的岛送至最东边的岛屿上。 思路: 网络流求最大流的裸题。先通过坐标找到最西边的岛屿和最东边的岛屿,记录并标记为源点和汇点。然后 用链式

USACO2013 island travels

题意:一个R行C列的矩阵,'X'表示地,'S'表示浅水,'.'表示不能走的深水。连通的X视为一个岛(不超过15个)。现在要走完所有岛,求最少的踩在浅水格子的次数。 题解:岛屿不超过15个,明显的暗示可以用状态压缩DP跑旅行商问题。但是这题需要较多的预处理。首先给每个X连通块标上岛屿的序号,然后对每一个岛屿,将它直接相邻的浅水格子压入队列跑BFS即可求出所有岛屿到他的距离。然后记得一定要跑一次Fl

zoj 3810 A Volcanic Island(构造)

题目链接:zoj 3810 A Volcanic Island 题目大意:给定n,要求用n块面积为n的拼图铺满n∗n的矩阵,任意两块拼图形状不能相同(包括旋转和镜像),并且n块拼图只能有4中颜色,相邻两块拼图颜色不能相同。 解题思路:构造,n = 2,3,4时是不存在的。然后对于n >= 5的直接构造,具体看代码。注意这种构造方式构造6的时候会出现相同的拼图,所以特判。 #includ

hdu 4640 Island and study-sister(最短路+状压dp)

题目链接:hdu 4640 Island and study-sister 解题思路 用二进制数表示2~n的点是否移动过的状态, dp[s][i] dp[s][i]表示状态s上的点必须经过并且当前在i节点的最小代价, 这步用类似最短路的方式求出。 然后是 dp2[i][s] dp2[i][s]表示i个人移动过s状态的点的最小代价。 代码 #include <cstdio>#includ

190.Largest Perimeter Triangle

题目 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. If it is impossible to form any triangle of non-zero area, ret

HDU 4280 Island Transport 网络流

题意:给你一些点,自己找最左边的点为起点,最右边的点位终点,给你边的权值,求最大流,dinic就可以过。 思路:dinic,建无向图,正反向flow一样。 #pragma comment(linker,"/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#inclu

[leetcode] max-area-of-island

. - 力扣(LeetCode) 给你一个大小为 m x n 的二进制矩阵 grid 。 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。 岛屿的面积是岛上值为 1 的单元格的数目。 计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。