# [LeetCode] BFS解决的题目

```class Solution {
public:
void solve(vector<vector<char>>& board) {
queue<int> x;
queue<int> y;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if ((i == 0 || i == board.size() - 1 ||
j == 0 || j == board[i].size() - 1) &&
board[i][j] == 'O') {
x.push(i);
y.push(j);
board[i][j] = 'Y';
}
}
}
while (!x.empty() && !y.empty()) {
int i = x.front();
int j = y.front();
x.pop();
y.pop();
if (i - 1 >= 0 && board[i - 1][j] == 'O') {
x.push(i - 1);
y.push(j);
board[i - 1][j] = 'Y';
}
if (i + 1 < board.size() && board[i + 1][j] == 'O') {
x.push(i + 1);
y.push(j);
board[i + 1][j] = 'Y';
}
if (j - 1 >= 0 && board[i][j - 1] == 'O') {
x.push(i);
y.push(j - 1);
board[i][j - 1] = 'Y';
}
if (j + 1 < board[0].size() && board[i][j + 1] == 'O') {
x.push(i);
y.push(j + 1);
board[i][j + 1] = 'Y';
}
}
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (board[i][j] == 'Y') {
board[i][j] = 'O';
}
else {
board[i][j] = 'X';
}
}
}
}
};```

（1）我用了三层循环去写，因为我想着要判断什么时候所有位置都为0，但其实是多此一举，只需要遍历每一个位置就行了。

（2）我一开始是先把位置放进队列，在出队列的时候才把这个位置的值变为0，但这是不对的：这样会造成一个位置的多次进队列（细想一下吧，出队列的时候才变为0，那在进队列到出队列的过程中这个位置的值还是1，还是会被其他位置检测到，多次加进队列），肯定会TLE。因此一定要先把值变为0，再加进队列。

```class Solution {
public:
int numIslands(vector<vector<char> >& grid) {
int ans = 0;
queue<int> x, y;
for (int i = 0; i < grid.size(); i++ ) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] == '1') {
x.push(i);
y.push(j);
grid[i][j] = '0';
while (!x.empty() && !y.empty()) {
int i = x.front();
int j = y.front();
x.pop();
y.pop();
if (i - 1 >= 0 && grid[i - 1][j] == '1') {
x.push(i - 1);
y.push(j);
grid[i - 1][j] = '0';
}
if (i + 1 < grid.size() && grid[i + 1][j] == '1') {
x.push(i + 1);
y.push(j);
grid[i + 1][j] = '0';
}
if (j - 1 >= 0 && grid[i][j - 1] == '1') {
x.push(i);
y.push(j - 1);
grid[i][j - 1] = '0';
}
if (j + 1 < grid[0].size() && grid[i][j + 1] == '1') {
x.push(i);
y.push(j + 1);
grid[i][j + 1] = '0';
}
}
ans++;
}
}
}
return ans;
}
};```

```class Solution {
public:
int numIslands(vector<vector<char> >& grid) {
int ans = 0;
for (int i = 0; i < grid.size(); i++ ) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] == '1') {
bfs(grid, i, j);
ans++;
}
}
}
return ans;
}
void bfs(vector<vector<char> >& grid, int i, int j) {
grid[i][j] = '0';
if (i - 1 >= 0 && grid[i - 1][j] == '1') {
bfs(grid, i - 1, j);
}
if (i + 1 < grid.size() && grid[i + 1][j] == '1') {
bfs(grid, i + 1, j);
}
if (j - 1 >= 0 && grid[i][j - 1] == '1') {
bfs(grid, i, j - 1);
}
if (j + 1 < grid[0].size() && grid[i][j + 1] == '1') {
bfs(grid, i, j + 1);
}
}
};```

三.542 01 Matrix

题目：

```class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
vector<vector<int> > ans = matrix;
queue<int> x, y;
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans[0].size(); j++) {
if (matrix[i][j] == 0) {
ans[i][j] = 0;
x.push(i);
y.push(j);
}
else {
ans[i][j] = -1;
}
}
}
while (!x.empty() && !y.empty()) {
int i = x.front(), j = y.front();
x.pop();
y.pop();
if (i - 1 >= 0 && ans[i - 1][j] == -1) {
ans[i - 1][j] = ans[i][j] + 1;
x.push(i - 1);
y.push(j);
}
if (i + 1 < ans.size() && ans[i + 1][j] == -1) {
ans[i + 1][j] = ans[i][j] + 1;
x.push(i + 1);
y.push(j);
}
if (j - 1 >= 0 && ans[i][j - 1] == -1) {
ans[i][j - 1] = ans[i][j] + 1;
x.push(i);
y.push(j - 1);
}
if (j + 1 < ans[0].size() && ans[i][j + 1] == -1) {
ans[i][j + 1] = ans[i][j] + 1;
x.push(i);
y.push(j + 1);
}
}
return ans;
}
};```

2018.1.7补充：