Given a 2d grid map of
'1'
s (land) and '0'
s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110 11010 11000 00000
Answer: 1
Example 2:
11000 11000 00100 00011
Answer: 3
This file contains 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 numIslands(vector<vector<char>>& grid) { | |
int res=0; | |
if(grid.empty()) | |
return res; | |
vector<vector<int> > accessed(grid.size(), vector<int>(grid[0].size(), 0)); | |
for(int i=0; i<grid.size(); i++){ | |
for(int j=0; j<grid[i].size(); j++){ | |
if(grid[i][j]=='0' || accessed[i][j]==1) | |
continue; | |
queue<pair<int, int> > Q; | |
Q.push(pair<int, int> (i, j)); | |
accessed[i][j]=1; | |
while(!Q.empty()){ | |
pair<int, int> p=Q.front(); | |
Q.pop(); | |
if(p.first>0&&grid[p.first-1][p.second]=='1'&&accessed[p.first-1][p.second]==0){ | |
Q.push(pair<int, int> (p.first-1, p.second)); | |
accessed[p.first-1][p.second]=1; | |
} | |
if(p.first<grid.size()-1&&grid[p.first+1][p.second]=='1'&&accessed[p.first+1][p.second]==0){ | |
Q.push(pair<int, int> (p.first+1, p.second)); | |
accessed[p.first+1][p.second]=1; | |
} | |
if(p.second>0&&grid[p.first][p.second-1]=='1'&&accessed[p.first][p.second-1]==0){ | |
Q.push(pair<int, int> (p.first, p.second-1)); | |
accessed[p.first][p.second-1]=1; | |
} | |
if(p.second<grid[0].size()-1&&grid[p.first][p.second+1]=='1'&&accessed[p.first][p.second+1]==0){ | |
Q.push(pair<int, int> (p.first, p.second+1)); | |
accessed[p.first][p.second+1]=1; | |
} | |
} | |
res++; | |
} | |
} | |
return res; | |
} | |
}; |
Round 2 solution:
DFS
This file contains 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: | |
void helper(vector<vector<char> >& grid, vector<vector<bool> >& mark, int r, int c){ | |
if(r<0||r>=grid.size()||c<0||c>=grid[0].size()||grid[r][c]=='0'||mark[r][c]==true) | |
return; | |
mark[r][c]=true; | |
int steps[] = {0, 0, 0, -1, 0, 1, -1, 0, 1, 0}; | |
for(int i=0; i<5; i++) | |
helper(grid, mark, r+steps[i*2], c+steps[i*2+1]); | |
} | |
int numIslands(vector<vector<char>>& grid) { | |
int res = 0; | |
if(grid.empty()) | |
return res; | |
vector<vector<bool> > mark(grid.size(), vector<bool>(grid[0].size(), false)); | |
for(int i=0; i<grid.size(); i++){ | |
for(int j=0; j<grid[0].size(); j++){ | |
if(grid[i][j]=='1'&&mark[i][j]==false){ | |
res++; | |
helper(grid, mark, i, j); | |
} | |
} | |
} | |
return res; | |
} | |
}; |
Rnd3 sol:
This file contains 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: | |
void helper(vector<vector<char>>& grid, int row, int col){ | |
if(row<0||col<0||row>=grid.size()||col>=grid[0].size()||grid[row][col]=='0') | |
return; | |
grid[row][col]='0'; | |
helper(grid, row, col-1); | |
helper(grid, row, col+1); | |
helper(grid, row-1, col); | |
helper(grid, row+1, col); | |
} | |
int numIslands(vector<vector<char>>& grid) { | |
int res = 0; | |
if(grid.empty()) | |
return res; | |
for(int i=0; i<grid.size(); i++){ | |
for(int j=0; j<grid[0].size(); j++){ | |
if(grid[i][j]=='0') | |
continue; | |
helper(grid, i, j); | |
res++; | |
} | |
} | |
return res; | |
} | |
}; |
No comments:
Post a Comment