#如何自己写出dfs和bfs算法的代码(可运用到该问题中去)代码不会写(涉及到队列等的数据结构均用自己的代码实现)
仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xs[10000];
int ys[10000];
int i=0,xx,yy;
int fc,bc;
void push(int x,int y) {
xs[i]=x;
ys[i]=y;
if (i<10000-1) {
i=i+1;
} else {
printf("stack overflow!\n");
exit(1);
}
}
void pop(void) {
i=i-1;
xx=xs[i];
yy=ys[i];
}
int check(int x,int y) {
int c;
c=getpixel(x,y); /* 获取当前点的颜色 */
return ((c!=bc)&&(c!=fc));/* 如果颜色为边界色或填充色则不填充 */
}
void seedfilling(int x,int y,int fill_color,int boundary_color) {
fc=fill_color;
bc=boundary_color;
push(x,y);
while (1) {
if (i<=0) return;
pop();
if (check(xx,yy)) {
putpixel(xx, yy, 14);getch(); /* 加上这行显示当前填充状态 */
putpixel(xx, yy, fill_color); /* 画点 */
if (check(xx-1,yy )) push(xx-1,yy );
if (check(xx ,yy+1)) push(xx ,yy+1);
if (check(xx ,yy-1)) push(xx ,yy-1);
if (check(xx+1,yy )) push(xx+1,yy );
/* 去掉下面四句就是四连通 */
if (check(xx-1,yy-1)) push(xx-1,yy-1);
if (check(xx-1,yy+1)) push(xx-1,yy+1);
if (check(xx+1,yy-1)) push(xx+1,yy-1);
if (check(xx+1,yy+1)) push(xx+1,yy+1);
}
}
}
void main() {
int a,b,color;
int gdriver = DETECT, gmode, errorcode;
int poly[10];
initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
a=150;
b=140;
color=4;
poly[0]=110;/* 第一个点的x坐标以及y坐标 */
poly[1]=110;
poly[2]=200;/* 第二点 */
poly[3]=105;
poly[4]=170;/* 第三点 */
poly[5]=120;
poly[6]=150;/* 第四点 */
poly[7]=170;
poly[8]=110;/* 多边形的起点与终点一样 */
poly[9]=110;
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
/* 保证边界对八连通是封闭的 */
setviewport(0,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,0,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
setviewport(1,1,600,300,0);
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
/* 恢复默认viewport */
setviewport(0,0,600,300,0);
seedfilling(a,b,color,15); /* 种子填充多边形 */
getch();
closegraph();
}
将地图看作一个二维数组,其中每个元素表示一个点,可以是陆地或水域。我们需要遍历这个数组,找到所有相连的陆地,然后将它们标记为一个岛屿。为了实现这个过程,用深度优先搜索(DFS)或广度优先搜索(BFS)算法。
首先定义一个visited数组来记录每个顶点是否已经被访问过。遍历整个地图,当找到一个陆地顶点时,从这个顶点开始进行DFS搜索。在DFS搜索过程中,访问所有与当前顶点相连的陆地顶点,并将它们标记为已访问。当完成DFS搜索时,将发现一个新的岛屿,并将其计数。
下面是一个使用DFS算法来计算岛屿数量的C语言程序示例:
#include <stdio.h>
#define ROWS 5
#define COLS 5
int map[ROWS][COLS] = {
{1, 1, 0, 0, 0},
{1, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 1},
{0, 0, 0, 1, 1}
};
int visited[ROWS][COLS] = {0};
void dfs(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
// out of bounds
return;
}
if (visited[row][col] || !map[row][col]) {
// already visited or not land
return;
}
visited[row][col] = 1;
dfs(row - 1, col); // up
dfs(row + 1, col); // down
dfs(row, col - 1); // left
dfs(row, col + 1); // right
}
int count_islands() {
int count = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (!visited[row][col] && map[row][col]) {
count++;
dfs(row, col);
}
}
}
return count;
}
int main() {
int islands = count_islands();
printf("There are %d islands.\n", islands);
return 0;
}
参考https://blog.csdn.net/Mars_PK_KING/article/details/100015538
// An highlighted block
#include <stdio.h>
char map[16][16];
int n, m;
int total;
void GetMap() {
scanf("%d%d\n", &n, &m);
int i,j;
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
scanf("%c", &map[i][j]);
}
getchar();
}
}
void Search(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '0') {
return;
}
map[x][y] = '0';
Search(x - 1, y);
Search(x, y - 1);
Search(x, y + 1);
Search(x + 1, y);
}
void GetResult() {
int i,j;
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
if (map[i][j] == '1') {
++total;
Search(i, j);
}
}
}
}
int main()
{
GetMap();
GetResult();
printf("%d\n", total);
return 0;
}