前言
day 58,周四,ding~
题目详情
[卡码101] 孤岛的总面积
题目描述
解题思路
前提:孤岛的总面积
思路:DFS or BFS
重点:DFS、BFS的实现
代码实现
C语言
DFS
// DFS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
void dfs(int **grid, int gridSize, int *gridColSize, int colIdx, int rawIdx, int *curAns, int *cal)
{
// 退出条件
if (grid[colIdx][rawIdx] == 0) {
return ;
}
// 递归
grid[colIdx][rawIdx] = 0;
(*curAns)++;
if ((colIdx == 0) || (colIdx == (gridSize - 1)) || (rawIdx == 0) || (rawIdx == (gridColSize[colIdx] - 1))) {
*cal = 1;
}
for (int k = 0; k < 4; k++) {
int nextCol = colIdx + dir[k][0];
int nextRaw = rawIdx + dir[k][1];
// 越界
if ((nextCol < 0) || (nextCol >= gridSize) || (nextRaw < 0) || (nextRaw >= gridColSize[nextCol])) {
continue;
}
dfs(grid, gridSize, gridColSize, nextCol, nextRaw, curAns, cal);
}
return ;
}
int maxAreaOfIsland(int **grid, int gridSize, int *gridColSize)
{
int ans = 0;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j <gridColSize[i]; j++) {
int cal = 0;
int curAns = 0;
dfs(grid, gridSize, gridColSize, i, j, &curAns, &cal);
if ((cal == 0) && (curAns > 0)) {
ans += curAns;
}
}
}
return ans;
}
int main()
{
// 输入
int n = 0;
int m = 0;
scanf("%d %d\n", &n, &m);
int gridSize = n;
int **grid = (int **)malloc(sizeof(int *) * gridSize);
memset(grid, 0, sizeof(int *) * gridSize);
int *gridColSize = (int *)malloc(sizeof(int) * gridSize);
memset(gridColSize, 0, sizeof(int) * gridSize);
for (int i = 0; i < gridSize; i++) {
grid[i] = (int *)malloc(sizeof(int) * m);
memset(grid[i], 0, sizeof(int) * m);
gridColSize[i] = m;
int count = 0;
char ch = 0;
while (((ch = getchar()) != '\n') && (count < m)) {
if (ch == ' ') {
continue;
}
grid[i][count++] = ch - '0';
}
}
// 处理
int ans = maxAreaOfIsland(grid, gridSize, gridColSize);
// 输出
printf("%d\n", ans);
return 0;
}
BFS
// BFS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUEUE_MAX_SIZE 2500
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
void bfs(int **grid, int gridSize, int *gridColSize, int colIdx, int rawIdx, int *curAns, int *cal)
{
// 退出条件
if (grid[colIdx][rawIdx] == 0) {
return ;
}
// 队列
int queue[QUEUE_MAX_SIZE][2];
memset(queue, 0, sizeof(queue));
int head = 0;
int tail = 0;
queue[tail][0] = colIdx;
queue[tail][1] = rawIdx;
tail++;
grid[colIdx][rawIdx] = 0;
(*curAns)++;
while(head != tail) {
int curCol = queue[head][0];
int curRaw = queue[head][1];
head++;
if ((curCol == 0) || (curCol == (gridSize - 1)) || (curRaw == 0) || (curRaw == (gridColSize[colIdx] - 1))) {
*cal = 1;
}
for (int k = 0; k < 4; k++) {
int nextCol = curCol + dir[k][0];
int nextRaw = curRaw + dir[k][1];
// 越界
if ((nextCol < 0) || (nextCol >= gridSize) || (nextRaw < 0) || (nextRaw >= gridColSize[nextCol]) || (grid[nextCol][nextRaw] == 0)) {
continue;
}
queue[tail][0] = nextCol;
queue[tail][1] = nextRaw;
tail++;
grid[nextCol][nextRaw] = 0;
(*curAns)++;
}
}
return ;
}
int maxAreaOfIsland(int **grid, int gridSize, int *gridColSize)
{
int ans = 0;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j <gridColSize[i]; j++) {
int cal = 0;
int curAns = 0;
bfs(grid, gridSize, gridColSize, i, j, &curAns, &cal);
if ((cal == 0) && (curAns > 0)) {
ans += curAns;
}
}
}
return ans;
}
int main()
{
// 输入
int n = 0;
int m = 0;
scanf("%d %d\n", &n, &m);
int gridSize = n;
int **grid = (int **)malloc(sizeof(int *) * gridSize);
memset(grid, 0, sizeof(int *) * gridSize);
int *gridColSize = (int *)malloc(sizeof(int) * gridSize);
memset(gridColSize, 0, sizeof(int) * gridSize);
for (int i = 0; i < gridSize; i++) {
grid[i] = (int *)malloc(sizeof(int) * m);
memset(grid[i], 0, sizeof(int) * m);
gridColSize[i] = m;
int count = 0;
char ch = 0;
while (((ch = getchar()) != '\n') && (count < m)) {
if (ch == ' ') {
continue;
}
grid[i][count++] = ch - '0';
}
}
// 处理
int ans = maxAreaOfIsland(grid, gridSize, gridColSize);
// 输出
printf("%d\n", ans);
return 0;
}
今日收获
- 图的遍历:DFS、BFS