Hackerrank Connected Cell in a Grid
来源:互联网 发布:2017黑色星期五 软件 编辑:程序博客网 时间:2024/06/06 03:07
Problem Statement
You are given a matrix with
Input Format
There will be three parts of t input:
The first line will contain
The second line will contain
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
Sample Input:
441 1 0 00 1 1 00 0 1 01 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 00 X X 00 0 X 01 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 stubint [][] 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 stubif(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
- Connected Cells in a Grid(dfs)
- Best Route in a Grid
- 8.2 Robot in a Grid
- #409 – 加入Grid中的子元素默认占满所在单元格(Child Elements in a Grid Size to Fit the Containing Cell)
- HackerRank Find first repeating letter in a string
- ios UISwitch in a UITableView cell
- Problem 11:Largest product in a grid
- (advanced) UVA 10949Kids in a Grid
- PE 11 Largest product in a grid
- hihocoder1241 : Best Route in a Grid
- hihocoder 1241:Best Route in a Grid
- HIHOCODE #1241:Best Route in a Grid
- hihoCoder 1241 Best Route in a Grid
- Problem 11 Largest product in a grid
- Ext的Grid列中插入进度条 (Progress Bar Inside A Grid Cell)
- 81.You are currently located in Singapore and have connected to a remote database in Chicago.
- Data Grid Merge Cell
- Consuming ASP.NET WEB API using ASP.NET MVC4 and RestSharp
- 线列车行驶在晓东村到珥季路下行车行驶在晓东村到珥季路下行轨行区
- C#多线程和事件
- df就会发飞华健康沙迪克就
- 关于今晚解题后的感想
- Hackerrank Connected Cell in a Grid
- nginx/php-fpm 访问php文件直接下载而不运行
- LeetCode 2.1.2 Remove Duplicates from Sorted Array II
- LeetCode 2.1.1 Remove Duplicates from Sorted Array
- C++游戏之游戏动画的实现(三)
- 深刻的印象
- 还未来得及
- 绿条长带雪
- 还能翻出什么花来……”