200. 岛屿数量
class Solution {
void DFS(vector<vector<char>> &G, int X, int Y) {
int Height = G.size(), Width = G.front().size();
if (G[Y][X] == '0')
return;
// Boom!
G[Y][X] = '0';
if (X - 1 >= 0)
DFS(G, X - 1, Y);
if (X + 1 < Width)
DFS(G, X + 1, Y);
if (Y - 1 >= 0)
DFS(G, X, Y - 1);
if (Y + 1 < Height)
DFS(G, X, Y + 1);
}
public:
int numIslands(vector<vector<char>>& grid) {
int Result = 0;
int Height = grid.size(), Width = grid.front().size();
for (int y = 0; y < Height; ++y) {
for (int x = 0; x < Width; ++x) {
if (grid[y][x] == '1') {
DFS(grid, x, y);
++Result;
}
}
}
return Result;
}
};
这个就叫过河拆桥,发现一个岛就把这个岛给炸沉。