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
the perimeter of the region. By keeping state only about the local topology, GPSR scales better in per-router state than shortest-path and ad-hoc routing protocols as the number of network ...
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
在打开此页面 用作扩展 该存储库可以作为扩展添加到MakeCode中。 打开 ...搜索并导入 编辑这个专案 要在MakeCode中编辑此存储库。... 单击导入,然后单击导入URL ...此图显示了master中最后一次提交的块代码。...
React周长 :construction: 在元素周围创建不可见的填充,并在其被破坏时做出响应。...< Perimeter xss=removed xss=removed> < button onClick = { this . fetch } >Load More</ button > </
面积-周长-矩形在此程序中,用户必须输入矩形的长度和宽度。 然后根据用户的选择计算并显示“面积/周长”。我在此程序中使用了“切换案例”语法。
Area-Perimeter
Networkers2009:BRKSEC-2006 - Inside the Perimeter: 6 Steps to Improve Your Security Monitoring
阿里巴巴问题LeetCode.463.Island-Perimeter 您将获得一张二维整数网格形式的地图,其中 1 表示陆地,0 表示水。 网格单元水平/垂直(不是对角线)连接。 网格完全被水包围,只有一个岛(即一个或多个相连的陆地单元...
- 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 表示水。 网格单元水平/垂直(不是对角线)连接。 网格完全被水包围,只有一个岛(即一个或多个相连的陆地单元)。...
Sets_of_Finite_Perimeter_and_Geometric_Variational_Problems
圆周长和面积计算器 圆周长和面积计算器 输入: 第一行,变量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 ...
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
资源分类:Python库 所属语言:Python 资源全名:django_perimeter-0.9.1-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Eric Seagren, CISSP, CISA, ISSAP, JPMorganChase has 10 years experience in IT Security and has spent the last 7 years at, one of the largest financial institutions in the world. Eric has contributed ...