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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. MFC Grid control 2.27

    原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...

  5. 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 ...

  6. grid布局——从入门到放弃

    基本知识 CSS grid 布局有两个核心组成部分:wrapper(网格容器,父元素)和items(网格项,子元素). 基本属性 属性 含义 display: grid 网格布局(父元素设置) gri ...

  7. [深度优先搜索] POJ 3620 Avoid The Lakes

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 4270 D ...

  8. arcmap Command

    The information in this document is useful if you are trying to programmatically find a built-in com ...

  9. Codeforces Round #356 (Div. 2)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. setTimeout 0

    setTimeout 0 就是把事件放到下一次事件循环时调用,至少要一个时钟之后: ); console.log('this should before setTimeout 0'); var i=0 ...

  2. oracle 入门笔记---分区表的分区交换

    本文参考来自作者:蓝紫 详细内容请阅读原文 : http://www.cnblogs.com/lanzi/archive/2013/01/24/2875838.html 在oracle 11.2环境下 ...

  3. 三维卷积:全景图像Spherical CNNs(Code)

    卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...

  4. Lazarus 字符集转换 Utf8ToAnsi,UTF8ToWinCP,UTF8ToSys,UTF8ToConsole

    由于Lazarus从1.2版开始默认字符集就是UTF8,如果要转到系统正常显示或文本保存,就必须对字符集进行转换.Lazarus提供了很多函数.如题. 那么这里面有什么关系呢? UTF8ToSys 需 ...

  5. pyhon模块

    模块基础 什么是模块 模块式一系列功能的集合体,而函数是某一个功能的集合体,因此模块可以看成是一堆函数的集合体.一个py文件内部可以放一堆函数,因此一个py文件就可以看成是一个模块.如果这个py文件的 ...

  6. 表格 —— 一个单元格插入多个tags

    <st #st [columns]="columns" [data]="data" [bordered]='true'> <ng-templa ...

  7. 【转载】Java下利用Jackson进行JSON解析和序列化

    参考资料: https://blog.csdn.net/sdut406/article/details/85647982 Java下常见的Json类库有Gson.JSON-lib和Jackson等,J ...

  8. 【6.24校内test】T1 江城唱晚

    [题目背景] 墙角那株海棠,是你种下的思念. 生死不能忘,高烛照容颜. 一曲江城唱晚,重忆当年坐灯前, 青衫中绣着你留下的线. ——银临<江城唱晚> [问题描述] 扶苏是个喜欢一边听古风歌 ...

  9. springcloud中feign接值问题

    很多时候使用feign都接收不到传过来的数据,一般情况如下! 如果是基本数据类型的话,使用@RequestParam @RequestMapping(value = "/selectDeta ...

  10. Linux学习笔记记录(九)