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 ...
随机推荐
- 自学php【二】 PHP计算时间加一天
最近几天在做一个项目,主要是将SQLserver数据到MySQL数据库,一个url跑一次 同步一次昨天的数据,由于很多数据需要同步,所以做了一个操作界面的,一个单纯跑url的 在其中涉及到了对于时间的 ...
- 牛客多校Round 10
咕咕咕.... 去烽火台和兵马俑了
- 第四章 模块化React和Redux应用
第四章 模块化React和Redux应用 4.1 模块化应用要点 构建一个应用的基础: 代码文件的组织结构: 确定模块的边界: Store的状态树设计. 4.2 代码文件的组织方式 4.2.1 按角色 ...
- attack on titans(动态规划递推,限制条件,至少转至多方法,进击的巨人)
题目意思: 给n个士兵排队,每个士兵三种G.R.P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数. 原题 Attack on Titans Time Limit: 2 Seconds ...
- 洛谷——P1273 有线电视网
P1273 有线电视网 题目大意: 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树 ...
- pandas文本处理
import pandas as pd import numpy as np s = pd.Series([', np.nan, 'hj']) df = pd.DataFrame({'key1': l ...
- orcad中注意的事情
1.地的标识不能放到已经分配了网络的线上. 在用orcad画原理图的时候,把电源放到网络的时候需要特别的注意,如果,将电源地直接放到线上,而这根线又已经被分配了网络标号,那这个地会随已经分配了的网络号 ...
- Leetcode 87.扰乱字符串
扰乱字符串 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = "great" 的一种可能的表示形式. 在扰乱这个字符串 ...
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
(提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...
- python——正则表达式的理解
概念:又称规则表达式,常用来检索.替换符合某个规则的文本. 理解:特殊字符--------->规则---------->过滤字符串 目的:1.匹配给定的字符串,2.从字符串中过滤出我们需要 ...