《啊哈!算法》-第 4 章:万能的搜索 - 宝岛冒险

_我已经从中二毕业了

宝岛冒险

题目

小哼通过秘密方法得到了一张不完整的钓鱼岛航拍地图。钓鱼岛由一个主岛和一些附属岛屿组成,小哼决定去钓鱼岛冒险。下面这个 10 * 10 的二维码矩阵就是钓鱼岛的航拍地图。图中数字代表海拔,0 表示海洋,1~9 表示陆地。小哼的飞机将会在(6,8)处,现在需要计算出小哼落地所在岛屿的面积(即有多少个格子)。

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 head = 0;
  int tail = 0;
  // 初始化位置
  que[tail].x = 6;
  que[tail].y = 8;
  tail++;
  book[6][8] = 1;
  area++;

  while (head < tail) {
    for (i = 0; i < 4; i++) {
      tx = que[head].x + direction[i][0];
      ty = que[head].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;
          a[tx][ty] = -1;
          que[tail].x = tx;
          que[tail].y = ty;
          tail++;
          area++;
        }
      }
    }
    
    head++;
  }

  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;
}

这一个算法就是鼎鼎大名的 FloodFill 漫水填充法(也称种子填充法)。常用于图像分割、物体识别等等。

阅读 2.1k

各种各样的学习笔记
不搞前端会死星人!

不搞前端会死星人。

7.9k 声望
235 粉丝
0 条评论

不搞前端会死星人。

7.9k 声望
235 粉丝
文章目录
宣传栏