leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0200.cpp (925B)


0 class Solution {
1 public:
2 int numIslands(vector<vector<char>> &grid) {
3 queue<pair<int, int>> q;
4 int cnt = 0;
5 int m = grid.size(), n = grid[0].size();
6 for (int i = 0; i < m; i++) {
7 for (int j = 0; j < n; j++) {
8 if (grid[i][j] == '0') continue;
9 q.push(make_pair(i, j));
10 cnt++;
11 while (!q.empty()) {
12 int i = q.front().first;
13 int j = q.front().second;
14 q.pop();
15 if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') continue;
16 grid[i][j] = '0';
17 q.push(make_pair(i + 1, j));
18 q.push(make_pair(i - 1, j));
19 q.push(make_pair(i, j + 1));
20 q.push(make_pair(i, j - 1));
21 }
22 }
23 }
24 return cnt;
25 }
26 };