# 方法一

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
*
*
* @param grid int整型vector<vector<>>
* @return int整型
*/
int x[4] = {-1,1,0,0};
int y[4] = {0,0,-1,1};
int s = 0;
vector<vector<bool>> pass;

void dfs(vector<vector<int>>& grid, int i, int j) {
for (int k=0;k<4;k++)
{
//注意边界
if (i+x[k]>-1 && i+x[k]<grid[0].size() &&
j+y[k]>-1 && j+y[k]<grid.size() &&
pass[i+x[k]][j+y[k]]==false &&
grid[i+x[k]][j+y[k]] == 1)
{
pass[i+x[k]][j+y[k]] = true;
s++;
dfs(grid, i+x[k], j+y[k]);
}
}
return;
}

int maxAreaIsland(vector<vector<int> >& grid) {
// write code here
int ans = 0;
vector<bool> temp;
//初始化pass
for (int i=0;i<grid[0].size();i++)
temp.push_back(false);
for (int i=0;i<grid.size();i++)
pass.push_back(temp);
//遍历grid
for (int i=0;i<grid.size();i++)
{
for (int j=0;j<grid[0].size();j++)
{
if (grid[i][j])
{
//当前位置pass设置为true
pass[i][j] = true;
s = 1;
dfs(grid, i, j);
ans = max(ans, s);
}
}
}
return ans;
}
};



# 方法二

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
*
*
* @param grid int整型vector<vector<>>
* @return int整型
*/
int x[4] = {-1,1,0,0};
int y[4] = {0,0,-1,1};
int s = 0;
queue<pair<int,int>> q;
vector<vector<bool>> pass;

void bfs(vector<vector<int>>& grid, int i, int j) {
//坐标(i,j)周围的格子没有扫描过，那么它就要先进入队列
q.push({i,j});
while (!q.empty())
{
//从队列头部弹出
auto coordinate = q.front();
q.pop();
for (int k=0;k<4;k++)
{
int xx = coordinate.first + x[k], yy = coordinate.second +y[k];
if (xx>-1 && xx<grid.size() && yy>-1 && yy<grid[0].size()
&& pass[xx][yy]==false && grid[xx][yy])
{
pass[xx][yy] = true;
s++;
bfs(grid, xx, yy);
}
}
}
return;
}

int maxAreaIsland(vector<vector<int> >& grid) {
// write code here
int ans = 0;
vector<bool> temp;
//初始化pass
for (int i=0;i<grid[0].size();i++)
temp.push_back(false);
for (int i=0;i<grid.size();i++)
pass.push_back(temp);
//遍历grid
for (int i=0;i<grid.size();i++)
{
for (int j=0;j<grid[0].size();j++)
{
if (grid[i][j])
{
//当前位置pass设置为true
pass[i][j] = true;
s = 1;
bfs(grid, i, j);
ans = max(ans, s);
}
}
}
return ans;
}
};