## C算法-DFS（深度优先搜索法）

leetcode200题，岛屿数量。

leetcode130题，被围绕的区域。

leetcode417题，太平洋大西洋水流问题

leetcode1254题，统计封闭岛屿的数目

dfs套路跟之前介绍的回溯风格有点不一样C算法-贪心/回溯/剪枝
1、for循环节点，和判断什么时候递归，放在主函数，与回溯风格明显不同。
2、dfs结构：a、if 出界 xxx；b、if被访问过 xxx；c、标记访问； d、dfs(next)
3、注意满足条件这个地方一定要好好填

1254题~如果越界了，那么周围不是封闭的；

``````bool Dfs(int i, int j, int **grid, int row, int col)
{

if(i < 0 || j < 0 || i >= row || j >= col) {

return false;
}
if(grid[i][j] == 1) {

return true;
}
if(grid[i][j] == 2) {

return true;
}
grid[i][j] = 2;
bool a, b, c, d;
a = Dfs(i+1,j,grid,row,col);
b = Dfs(i,j+1,grid,row,col);
c = Dfs(i-1,j,grid,row,col);
d = Dfs(i,j-1,grid,row,col);
return a&b&c&d;
}

int closedIsland(int** grid, int gridSize, int* gridColSize){

if(gridSize == 0) {

return 0;
}
int i, j, row, col, cnt;
row = gridSize;
col = *gridColSize;
cnt = 0;
for(i = 1; i < row - 1; i++) {

for (j = 1; j < col - 1; j++) {

if(grid[i][j]==0 && Dfs(i,j, grid, row, col) == true) {

cnt++;
}
}
}
return cnt;
}
``````

200岛屿问题

``````void dfs(char **grid,int row, int col, int i ,int j) {

if (i < 0 || j < 0 || i >=row || j >=col) {

return;
}
if (grid[i][j] == '0') {

return;
}
grid[i][j] = '0';
dfs(grid, row, col, i+1, j);
dfs(grid, row, col, i-1, j);
dfs(grid, row, col, i, j+1);
dfs(grid, row, col, i, j-1);
}

int numIslands(char** grid, int gridSize, int* gridColSize){

int i, j, col, row, count;
if (gridSize == 0) {

return 0;
}
col = *gridColSize;
row = gridSize;
count = 0;
for (i = 0; i < row; i++) {

for (j = 0; j < col; j++) {

if (grid[i][j] == '1') {

count++;
dfs(grid, row, col, i, j);
}
}
}
return count;
}
``````

130围绕的区域

``````void dfs(int i, int j, char **board, int row, int col)
{

if (i < 0 || j < 0 || i >= row || j >= col) {

return;
}
if (board[i][j] != 'O') {

return;
}
board[i][j] = 'B';
dfs(i + 1, j, board, row, col);
dfs(i - 1, j, board, row, col);
dfs(i, j + 1, board, row, col);
dfs(i, j - 1, board, row, col);
return;
}

void solve(char** board, int boardSize, int* boardColSize) {

int i, j;
int col, row;
if (boardSize == 0) {

return;
}
row = boardSize;
col = *boardColSize;
for (i = 0; i < row; i++) {

for (j = 0; j < col; j++) {

if (i != 0 && i != row - 1 && j!= 0 && j!= col -1 ) {

continue;
}
if (board[i][j] == 'X') {

continue;
}
dfs(i, j, board, row, col);
}
}
for (i = 0; i < row; i++) {

for (j = 0; j < col; j++) {

if (board[i][j] == 'O') {

board[i][j] = 'X';
}
}
}
for (i = 0; i < row; i++) {

for (j = 0; j < col; j++) {

if (board[i][j] == 'B') {

board[i][j] = 'O';
}
}
}
return;
}
``````

417太平洋大西洋的水流问题

``````#define MAXLEN 160
int ocean1[MAXLEN][MAXLEN];
int ocean2[MAXLEN][MAXLEN];
void dfs(int ** matrix, int row, int col, int i, int j, int count, int flag)
{

if (i < 0 || j < 0 || i >= row || j >= col) {

return;
}
if (matrix[i][j] < count) {

return;
}
if (flag == 0 && ocean1[i][j] == 1) {

return;
}
if (flag == 1 && ocean2[i][j] == 1) {

return;
}
if (flag == 0) {

ocean1[i][j] = 1;
}
if (flag == 1) {

ocean2[i][j] = 1;
}
dfs(matrix, row, col, i + 1, j, matrix[i][j], flag);
dfs(matrix, row, col, i - 1, j, matrix[i][j], flag);
dfs(matrix, row, col, i, j + 1, matrix[i][j], flag);
dfs(matrix, row, col, i, j - 1, matrix[i][j], flag);
}

int** pacificAtlantic(int** matrix, int matrixSize, int* matrixColSize, int* returnSize, int** returnColumnSizes) {

if (matrixSize == 0) {

*returnSize = 0;
return NULL;
}

int row, col, i, j, count;
int tmp[MAXLEN*MAXLEN][2];
row = matrixSize;
col = *matrixColSize;
count = 0;
memset(ocean1, 0, sizeof(int)*MAXLEN*MAXLEN);
memset(ocean2, 0, sizeof(int)*MAXLEN*MAXLEN);
i = 0;
for (j = 0; j < col; j++) {

dfs(matrix, row, col, i, j, 0, 0);
}
j = 0;
for (i = 0; i < row; i++) {

dfs(matrix, row, col, i, j, 0, 0);
}
i = row - 1;
for (j = 0; j < col; j++) {

dfs(matrix, row, col, i, j, 0, 1);
}
j = col - 1;
for (i = 0; i < row; i++) {

dfs(matrix, row, col, i, j, 0, 1);
}
count = 0;
for (i = 0; i < row; i++) {

for (j = 0; j < col; j++) {

if (ocean1[i][j] && ocean2[i][j]) {

tmp[count][0] = i;
tmp[count][1] = j;
count++;
}
}
}
*returnSize = count;
int **ret = (int**)malloc(sizeof(int*)*count);
*returnColumnSizes = (int*)malloc(sizeof(int)*count);
int *total = (int*)malloc(sizeof(int)*count * 2);
for (i = 0; i < count; i++) {

ret[i] = total + 2 * i;
ret[i][0] = tmp[i][0];
ret[i][1] = tmp[i][1];
(*returnColumnSizes)[i] = 2;
}

return ret;

}
``````