Hackerrank Connected Cell in a Grid
Problem Statement
You are given a matrix with m rows and n columns of cells, each of which contains either 1or 0. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally. The connected and filled (i.e. cells that contain a 1) cells form aregion. There may be several regions in the matrix. Find the number of cells in the largest region in the matrix.
Input Format
There will be three parts of t input:
The first line will contain m, the number of rows in the matrix.
The second line will contain n, the number of columns in the matrix.
This will be followed by the matrix grid: the list of numbers that make up the matrix.
Output Format
Print the length of the largest region in the given matrix.
Constraints
0<m<10
0<n<10
Sample Input:
4
4
1 1 0 0
0 1 1 0
0 0 1 0
1 0 0 0
Sample Output:
5
Task:
Write the complete program to find the number of cells in the largest region.
Explanation
X X 0 0
0 X X 0
0 0 X 0
1 0 0 0
The X characters indicate the largest connected component, as per the given definition. There are five cells in this component.
思路分析:这题是Hackerrank一次的比赛题目。也是G公司一次面试中出现的面试原题。
要在一个矩阵中找到最大的连通区域。
基本能够用DFS搜索解决,在每一个位置重新启动搜索找连通区域,一共同拥有8个方向/分支,贪心保留最大cell数目。用visited标记数组记录已经count过的位置进行剪枝加速。
是一道中规中矩的考察DFS/BFS搜索的题目。
AC Code
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*; public class Solution { static int[][] actionCosts = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; static int cellCounter = 0; public static void main(String[] args) throws NumberFormatException, IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
int m = Integer.valueOf(in.readLine());
int n = Integer.valueOf(in.readLine());
int [][] matrix = new int [m][n];
for(int i = 0; i < m; i++){
String line = in.readLine();
for(int j = 0; j < n; j++){
matrix[i][j] = Integer.valueOf(line.split(" ")[j]);
}
}
int maxNum = findMaxConnectedCellNum(matrix, m, n);
System.out.println(maxNum);
} private static int findMaxConnectedCellNum(int[][] matrix, int m, int n) {
// TODO Auto-generated method stub
int [][] visited = new int[m][n];
int maxNum = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
dfs(matrix, visited, i, j, m, n);
if(cellCounter > maxNum) maxNum = cellCounter;
cellCounter = 0;
}
}
return maxNum;
} private static void dfs(int[][] matrix, int[][] visited, int i, int j,
int m, int n) {
// TODO Auto-generated method stub
if(i < 0 || i >= m || j < 0 || j >= n){
return;
}
if(visited[i][j] == 1 || matrix[i][j] == 0) return;
cellCounter++;
visited[i][j] = 1;
for(int di = 0; di < 8; di++){
dfs(matrix, visited, i + actionCosts[di][0], j + actionCosts[di][1], m, n);
}
}
}
Hackerrank Connected Cell in a Grid的更多相关文章
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块
E. Bear and Square Grid 题目连接: http://www.codeforces.com/contest/680/problem/E Description You have a ...
- Problem A. Dynamic Grid
Problem We have a grid with R rows and C columns in which every entry is either 0 or 1. We are going ...
- MFC Grid control 2.27
原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...
- B.Grid with Arrows-The 2019 ICPC China Shaanxi Provincial Programming Contest
BaoBao has just found a grid with $n$ rows and $m$ columns in his left pocket, where the cell in the ...
- grid布局——从入门到放弃
基本知识 CSS grid 布局有两个核心组成部分:wrapper(网格容器,父元素)和items(网格项,子元素). 基本属性 属性 含义 display: grid 网格布局(父元素设置) gri ...
- [深度优先搜索] POJ 3620 Avoid The Lakes
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 4270 D ...
- arcmap Command
The information in this document is useful if you are trying to programmatically find a built-in com ...
- Codeforces Round #356 (Div. 2)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- Java 基础入门随笔(10) JavaSE版——单例设计模式
设计模式:对问题行之有效的解决方式.其实它是一种思想. 1.单例设计模式. 解决的问题:就是可以保证一个类在内存中的对象唯一性.(单个实例) 使用单例设计模式需求:必须对于多个程序使用同一个配置信息对 ...
- 3星|林毅夫《战胜命运》:事实证明华盛顿共识是错误的,GIFF是穷国发展正道。
本书是林毅夫与喀麦隆一位经济学家合著.基本的观点是:事实证明华盛顿共识是错误的,GIFF是穷国发展正道.GIFF的主要思想是政府找到对标国家,强力推行产业政策. 作为一个经济学外行,读后感觉关于华盛顿 ...
- 前端JavaScript入门——JavaScript变量和操作元素
变量JavaScript 是一种弱类型语言,javascript的变量类型由它的值来决定. 定义变量需要用关键字 ‘var’: var a = 123; var b = 'asd'; //同时定义多个 ...
- JAVA程序员面试笔试宝典1
1.为什么Java中有些接口没有任何方法? 这些没有任何方法声明的接口又被称为标识接口,标识接口对于实现它的类没有任何语义上的要求,它仅仅充当一个标识的作用,用来表明它的类属于一个特定的类型. 2.j ...
- vue学习总结(简单介绍)
声明式渲染 Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: <div id="app"> {{ message }} < ...
- maven入门链接
http://www.cnblogs.com/now-fighting/p/4857625.html
- 洛谷 1541 NOIp2010提高组 乌龟棋
[题解] 很容易想到这是一个DP,f[i][j][k][l]表示4种卡片分别用了多少张,那么转移方程就是f[i][j][k][l]=Max(f[i-1][j][k][l],f[i][j-1][k][l ...
- 第一个Maven工程的目录结构和文件内容及联网问题
[第一个Maven工程] ①目录结构 Hello |---src |---|---main |---|---|---java |---|---|---resources |---|---test |- ...
- JavaSE 学习笔记之正则表达式(二十五)
正则表达式:其实是用来操作字符串的一些规则. 好处:正则的出现,对字符串的复杂操作变得更为简单. 特点:将对字符串操作的代码用一些符号来表示.只要使用了指定符号,就可以调用底层的代码对字符串进行操作. ...
- 阿里巴巴json包 --------fastjson
fastjson对null的处理----String str2 = JSONObject.toJSONString(jsonMap, SerializerFeature.WriteMapNullVal ...