C算法-DFS(深度优先搜索法)

leetcode200题,岛屿数量。
二维矩阵,1代表陆地,连在一起算一个岛,返回岛屿个数。
leetcode130题,被围绕的区域。
二位矩阵,如果0被1框住,那么把0填成1
leetcode417题,太平洋大西洋水流问题
从上往下流的水,从左往右流的水mark出来,另外两个方向的存数组2里面。存的方法是dfs

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

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;

}

你可能感兴趣的:(C)