Description:
Given a non-empty 2D array
grid
of 0's and 1's, an island is a group of1
's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
Example 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,1,1,0,1,0,0,0,0,0,0,0,0], [0,1,0,0,1,1,0,0,1,0,1,0,0], [0,1,0,0,1,1,0,0,1,1,1,0,0], [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return
6
. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:
[[0,0,0,0,0,0,0,0]]
Given the above grid, return
0
.
Note:
The length of each dimension in the given
grid
does not exceed 50.
分析:
和逃生类似,借助前后左右四个方向({{1,0}, {-1,0},{0,1},{0,-1}})进行DFS。
代码:
public class Solution {
private int area;
private int maxArea = 0;
private int[][] direction = {{1,0}, {-1,0},{0,1},{0,-1}};
public int maxAreaOfIsland(int[][] grid) {
if (null == grid || grid.length < 1){
return 0;
}
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[0].length; j++){
if (grid[i][j] == 1) {
area = 0;
dfs(grid, i, j);
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
}
private void dfs(int[][] grid, int r, int c){
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] == 0){
return ;
}
area++;
grid[r][c] = 0;
for (int i = 0; i < direction.length; i++){
dfs(grid, r + direction[i][0], c + direction[i][1]);
}
}
}
题目来源:https://leetcode.com/problems/max-area-of-island/
类似题目(解题思路类似):
1) https://leetcode.com/problems/island-perimeter/
class Solution {
private int[][] direction = {{1,0}, {-1,0},{0,1},{0,-1}};
public int islandPerimeter(int[][] grid) {
if (grid.length < 1 || grid[0].length < 1){
return 0;
}
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[0].length; j++){
if (grid[i][j] == 1){
return dfs(grid, i, j);
}
}
}
return 0;
}
private int dfs(int[][] grid, int i, int j){
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0){
return 1;
}
if (grid[i][j] == -1){
return 0;
}
int cnt = 0;
grid[i][j] = -1;
for (int[] dir : direction){
cnt += dfs(grid, i + dir[0], j + dir[1]);
}
return cnt;
}
}
2) https://leetcode.com/problems/word-search/
找到每个单词的第一个匹配字母和递归以检查其余单词。为了遵守不使用位置多于一次的规则,我们设定为“0”被访问的位置。
class Solution {
private int[][] direction = {{1,0},{-1,0},{0,1},{0,-1}};
public boolean exist(char[][] board, String word) {
if (board.length < 1 || board[0].length < 1 || word.trim() == ""){
return false;
}
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[0].length; j++){
if (board[i][j] == word.charAt(0) && dfs(board, word, 0, i, j)){
return true;
}
}
}
return false;
}
private boolean dfs(char[][] board,String word, int start, int r, int c){
//当大于等于字符串长度的时候说明已经找到该字符串了
if (word.length() <= start){
return true;
}
if (r < 0 || r >= board.length || c < 0 || c >= board[0].length || board[r][c] == '0'
|| board[r][c] != word.charAt(start)){
return false;
}
char tmp = board[r][c];
board[r][c] = '0';
for (int[] dir : direction){
if (dfs(board, word, start+1, r+dir[0], c+dir[1])){
return true;
}
}
//还原
board[r][c] = tmp;
return false;
}
}