given grid of colors, coordinate of a point and its color, find the perimeter of the region that has the same color of that point.
BFS或DFS,构成perimeter的条件是只要上下左右有一个不是同颜色或者是out of bound 用一个set记录visit的信息
public static class Point{ int r, c; public Point(int x, int y) {r = x; c = y;} public int hashCode() {return r*31+c; } } private static final int[][] dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}}; public static int findPerimeter(int[][] mat, int r, int c) { int m = mat.length, n = mat[0].length; boolean[][] visited = new boolean[m][n]; int cnt = 0; Queue<Point> q = new LinkedList<>(); q.offer(new Point(r,c)); while(!q.isEmpty()) { Point p = q.poll(); if(visited[p.r][p.c]) continue; visited[p.r][p.c] = true; boolean isEdge = false; for(int[] dir: dirs) { int i = p.r+dir[0], j = p.c+dir[1]; if(i<0||j<0||i>=m||j>=n||mat[p.r][p.c]!=mat[i][j]) { isEdge = true; } else { q.offer(new Point(i,j)); } } if(isEdge) cnt++; } return cnt; } public static void main (String[] args) { int[][] mat = { {1,1,2,2,3,4,5}, {1,1,1,1,1,4,5}, {1,1,1,1,1,4,5}, {1,1,2,2,3,4,5}}; System.out.println(findPerimeter(mat, 2, 1)); }
相关推荐
Software-Defined-Perimeter.zip
Apress - Securing the Perimeter.2019 Apress - Securing the Perimeter.2019
PaloAlto
on a curve by the distance from the original point on the perimeter. - ADD: Added the method TCustomProp.GetPublishedComplexProp - returns included complex flex-property by name registered through ...
jitc-perimeter-bridge-testing
本项目"Area-Perimeter-Rectangle"就是这样一个例子,它允许用户输入矩形的长度和宽度,并根据用户的选择来计算并显示矩形的面积或周长。这个程序运用了条件控制结构——“switch case”,这是一种多路选择语句,...
在打开此页面 用作扩展 该存储库可以作为扩展添加到MakeCode中。 打开 ...搜索并导入 编辑这个专案 要在MakeCode中编辑此存储库。... 单击导入,然后单击导入URL ...此图显示了master中最后一次提交的块代码。...
React周长 :construction: 在元素周围创建不可见的填充,并在其被破坏时做出响应。...< Perimeter xss=removed xss=removed> < button onClick = { this . fetch } >Load More</ button > </
The document provides a detailed explanation of a simple C program to calculate the area and perimeter of a rectangle: - **The Program Example**: This is a step-by-step breakdown of the code, ...
阿里巴巴问题LeetCode.463.Island-Perimeter 您将获得一张二维整数网格形式的地图,其中 1 表示陆地,0 表示水。 网格单元水平/垂直(不是对角线)连接。 网格完全被水包围,只有一个岛(即一个或多个相连的陆地单元...
标题“Area-Perimeter”可能指的是一个编程项目,专注于计算几何中的面积和周长。在Python编程语言中,处理这些概念通常涉及到基本的数学运算和数据结构。让我们深入探讨这个主题,看看可以从这个项目中学到哪些重要...
The friction coefficient is an indicator that reflects the change in friction performance of the pavement surface, indirectly reflecting the influence of the micro-environment on the pavement surface...
- This book will be unique in that it will be the only book that teaches readers how to build a DMZ using all of these products: ISA Server, Check Point NG, Cisco Routers, Sun Servers, and Nokia ...
leetcode 阿里巴巴问题岛周长 您将获得一张二维整数网格形式的地图,其中 1 表示陆地,0 表示水。 网格单元水平/垂直(不是对角线)连接。 网格完全被水包围,只有一个岛(即一个或多个相连的陆地单元)。...
- **演讲题目**:《Inside the Perimeter: 6 Steps to Improve Your Security Monitoring》(内部防御:提升网络安全监控的六个步骤) - **演讲者**:Martin G. Nystrom, CISSP-ISSAP - **演讲者职位**:Cisco CSIRT...
圆周长和面积计算器 圆周长和面积计算器 输入: 第一行,变量n(n> 0),代表几组数据 C ++: 接下来的n行(或多行),输入变量a和变量m。 a表示不同的物种: 当您输入a时,变量m将为圆的半径。...
This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products: Learning Penetration Testing with Python ...
### 有限周长集合与几何变分问题 #### 前言 《有限周长集合与几何变分问题》一书是由弗朗切斯科·马吉(Francesco Maggi)编著,作为剑桥高级数学研究系列的一部分,旨在通过分析与几何之间的紧密联系为读者提供一个...
Because of the power and flexibility offered by it, Python has become one of the most popular languages used for penetration testing. This book highlights how you can evaluate an organization ...
perimeter determination