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 ...
随机推荐
- Microsoft SQL Server 安全与权限
Microsoft SQL Server 安全与权限 登陆角色 计算机操作系统用户 --创建Windows身份验证用户 USE [master] GO CREATE LOGIN [计算机名称\计算机用 ...
- 抓取猫眼电影top100的正则、bs4、pyquery、xpath实现方法
import requests import re import json import time from bs4 import BeautifulSoup from pyquery import ...
- 山建校赛B题公式证明
原题 证明
- Linux 下 Bash 脚本对拍
背会... #!/bin/bash i= while true ;do ./maker > data.in ./a <data.in> data.out ./b <data.i ...
- 通过docker-composer启动容器nginx,并完成spring.boot的web站点端口转发
前面已经讲过2篇基于docker的mysql.redis容器编排并启动.这次将练习下nginx的docker方式的部署,以及通过nginx去代理宿主主机上的Web服务应该怎么配 PS:(这里由于ngi ...
- 初遇Java
什么是JVM?JVM是java虚拟机(JVM Java Virtual Machine),java程序需要运行在虚拟机上,不同平台有自己的虚拟机,因此java语言可以跨平台. 什么是JRE?包括Jav ...
- Vim常用快捷键--正常的学习曲线
vim可能对于初学者不太友好,学习曲线有点陡,特此整理了较为平滑的学习曲线的学习快捷键的方式,包含最常用的快捷键,让初学者领悟vim的优点,想要进阶学习请查找其它更好的教程 正常模式:可以使用快捷键命 ...
- 解决CUDA程序的黑屏恢复问题
本文引用自 http://blog.163.com/yuhua_kui/blog/static/9679964420146183211348/ 问题描述: 在运行CUDA程序时,出现黑屏,过一会儿 ...
- Webstorm如何配置自动补全前缀--autoprefixer
我们在写样式代码时,对不同平台会有不同的兼容性写法,会在代码前加前缀,但是手动加前缀很费时间而且很容易弄错.Webstorm编辑器是有自带补全前缀功能的,那为什么还要写这篇配置博客,因为Webstor ...
- Spring 使用注解注入 学习(四)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...