Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1 '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
Example 2:
[[0,0,0,0,0,0,0,0]]Given the above grid, return
Note: The length of each dimension in the given grid does not exceed 50.
<Solution>這題是典型的DFS,用DFS的觀念解即可
code 如下
C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxAreaOfIsland(vector<vector<int>>& grid) { | |
const int row = grid.size(); | |
const int col = grid[0].size(); | |
int ans = INT_MIN; | |
int cnt; | |
queue<pair<int,int>> dfs; | |
pair<int,int> land; | |
for(int r = 0; r < row; ++r) { | |
for(int c = 0; c < col; ++c) { | |
if(grid[r][c] == 1) { | |
cnt = 0; | |
grid[r][c] = -1; | |
dfs.push(make_pair(r,c)); | |
while(!dfs.empty()) { | |
land = dfs.front(); | |
dfs.pop(); | |
++cnt; | |
//>> up | |
if(land.first > 0 && grid[land.first-1][land.second] == 1) { | |
grid[land.first-1][land.second] = -1; | |
dfs.push(make_pair(land.first-1,land.second)); | |
} | |
//>> down | |
if(land.first+1 < row && grid[land.first+1][land.second] == 1) { | |
grid[land.first+1][land.second] = - 1; | |
dfs.push(make_pair(land.first+1,land.second)); | |
} | |
//>> left | |
if(land.second > 0 && grid[land.first][land.second-1] == 1) { | |
grid[land.first][land.second-1] = -1; | |
dfs.push(make_pair(land.first,land.second-1)); | |
} | |
//>> right | |
if(land.second+1 < col && grid[land.first][land.second+1] == 1) { | |
grid[land.first][land.second+1] = -1; | |
dfs.push(make_pair(land.first,land.second+1)); | |
} | |
} | |
ans = max(ans, cnt); | |
} | |
} | |
} | |
return ans == INT_MIN ? 0 : ans; | |
} | |
}; |
Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int maxAreaOfIsland(int[][] grid) { | |
int ans = 0; | |
int cnt; | |
Stack<int[]> dfs = new Stack<>(); | |
int[] tmp; | |
for(int r = 0; r < grid.length; ++r) { | |
for(int c = 0; c < grid[0].length; ++c) { | |
if(grid[r][c] == 1) { | |
cnt = 0; | |
grid[r][c] = -1; | |
dfs.push(new int[]{r, c}); | |
while(!dfs.empty()) { | |
++cnt; | |
tmp = dfs.pop(); | |
//>> up | |
if(tmp[0] > 0 && grid[tmp[0]-1][tmp[1]] == 1) { | |
grid[tmp[0]-1][tmp[1]] = -1; | |
dfs.push(new int[]{tmp[0]-1, tmp[1]}); | |
} | |
//>> down | |
if(tmp[0] + 1 < grid.length && grid[tmp[0]+1][tmp[1]] == 1) { | |
grid[tmp[0]+1][tmp[1]] = -1; | |
dfs.push(new int[]{tmp[0]+1, tmp[1]}); | |
} | |
//>> left | |
if(tmp[1] > 0 && grid[tmp[0]][tmp[1]-1] == 1) { | |
grid[tmp[0]][tmp[1]-1] = -1; | |
bfs.push(new int[]{tmp[0], tmp[1]-1}); | |
} | |
//>> right | |
if(tmp[1]+1 < grid[0].length && grid[tmp[0]][tmp[1]+1] == 1) { | |
grid[tmp[0]][tmp[1]+1] = -1; | |
bfs.push(new int[]{tmp[0], tmp[1]+1}); | |
} | |
} | |
ans = Math.max(ans, cnt); | |
} | |
} | |
} | |
return ans; | |
} | |
} |
沒有留言:
張貼留言