# 《啊哈！算法》－第 4 章：万能的搜索 - 宝岛冒险

_我已经从中二毕业了

## 宝岛冒险

### 题目

``````1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0``````

### 计算小岛面积

``````#include <stdio.h>
#define NUM 10

int a[NUM][NUM] = {
{1, 2, 1, 0, 0, 0, 0, 0, 2, 3},
{3, 0, 2, 0, 1, 2, 1, 0, 1, 2},
{4, 0, 1, 0, 1, 2, 3, 2, 0, 1},
{3, 2, 0, 0, 0, 1, 2, 4, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 5, 3, 0},
{0, 1, 2, 1, 0, 1, 5, 4, 3, 0},
{0, 1, 2, 3, 1, 3, 6, 2, 1, 0},
{0, 0, 3, 4, 8, 9, 7, 5, 0, 0},
{0, 0, 0, 3, 7, 8, 6, 0, 1, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
};
int book[NUM][NUM];
int direction[4][2] = {
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};

int main(void) {
int i, j, tx, ty, area = 0;

for (i = 0; i < NUM; i++)
for (j = 0; j < NUM; j++)
book[i][j] = 0;

// 初始化队列
struct node {
int x;
int y;
} que[1000];
int tail = 0;
// 初始化位置
que[tail].x = 6;
que[tail].y = 8;
tail++;
book[6][8] = 1;
area++;

for (i = 0; i < 4; i++) {

if (tx >= 0 && tx < NUM && ty >= 0 && ty < NUM) {
if (a[tx][ty] > 0 && book[tx][ty] == 0) {
book[tx][ty] = 1;
a[tx][ty] = -1;
que[tail].x = tx;
que[tail].y = ty;
tail++;
area++;
}
}
}

}

printf("%d\n", area);

for (i = 0; i < NUM; i++) {
for (j = 0; j < NUM; j++) {
printf("%2d ", a[i][j]);
}
printf("\n");
}

return 0;
}``````

### 用符号表示路过的小岛

``````#include <stdio.h>
#define NUM 10

int a[NUM][NUM] = {
{1, 2, 1, 0, 0, 0, 0, 0, 2, 3},
{3, 0, 2, 0, 1, 2, 1, 0, 1, 2},
{4, 0, 1, 0, 1, 2, 3, 2, 0, 1},
{3, 2, 0, 0, 0, 1, 2, 4, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 5, 3, 0},
{0, 1, 2, 1, 0, 1, 5, 4, 3, 0},
{0, 1, 2, 3, 1, 3, 6, 2, 1, 0},
{0, 0, 3, 4, 8, 9, 7, 5, 0, 0},
{0, 0, 0, 3, 7, 8, 6, 0, 1, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
};
int book[NUM][NUM];
int sum = 0;
int direction[4][2] = {
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};

void dfs(int x, int y, int color) {
int i, tx, ty;
// 边界

a[x][y] = color;

// 尝试每一步
for (i = 0; i < 4; i++) {
tx = x + direction[i][0];
ty = y + direction[i][1];

if (tx >= 0 && tx < NUM && ty >= 0 && ty < NUM) {
if (a[tx][ty] > 0 && book[tx][ty] == 0) {
book[tx][ty] = 1;
sum++;
// 下一步
dfs(tx, ty, color);
}
}
}
}

int main(void) {
int i, j;

dfs(6, 8, -1);

printf("%d\n", sum);

for (i = 0; i < NUM; i++) {
for (j = 0; j < NUM; j++) {
printf("%2d ", a[i][j]);
}
printf("\n");
}

return 0;
}``````

### 记录地图有多少个小岛

``````#include <stdio.h>
#define NUM 10

int a[NUM][NUM] = {
{1, 2, 1, 0, 0, 0, 0, 0, 2, 3},
{3, 0, 2, 0, 1, 2, 1, 0, 1, 2},
{4, 0, 1, 0, 1, 2, 3, 2, 0, 1},
{3, 2, 0, 0, 0, 1, 2, 4, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 5, 3, 0},
{0, 1, 2, 1, 0, 1, 5, 4, 3, 0},
{0, 1, 2, 3, 1, 3, 6, 2, 1, 0},
{0, 0, 3, 4, 8, 9, 7, 5, 0, 0},
{0, 0, 0, 3, 7, 8, 6, 0, 1, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
};
int book[NUM][NUM];
int direction[4][2] = {
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};

void dfs(int x, int y, int color) {
int i, tx, ty;
// 边界

a[x][y] = color;

// 尝试每一步
for (i = 0; i < 4; i++) {
tx = x + direction[i][0];
ty = y + direction[i][1];

if (tx >= 0 && tx < NUM && ty >= 0 && ty < NUM) {
printf("%d %d\n", tx, ty);
if (a[tx][ty] > 0 && book[tx][ty] == 0) {
book[tx][ty] = 1;
// 下一步
dfs(tx, ty, color);
}
}
}
}

int main(void) {
int i, j, num = 0;

for (i = 0; i < NUM; i++) {
for (j = 0; j < NUM; j++) {
if (a[i][j] > 0 && book[i][j] == 0) {
num--;
book[i][j] = 1;
dfs(i, j, num);
}
}
}

// dfs(0, 0, -1);

for (i = 0; i < NUM; i++) {
for (j = 0; j < NUM; j++) {
printf("%2d ", a[i][j]);
}
printf("\n");
}

printf("%d\n", -num);

return 0;
}``````

7.9k 声望
235 粉丝
0 条评论