啊哈!算法—— 4.5 宝岛探险(搜索浅理解)

第四章万能的搜索——第五节 宝岛探险

题目大意
  钓鱼岛由一个主岛和一些附属岛屿组成,小明决定去钓鱼岛探险。下面这个10*10的二维矩阵就是钓鱼岛的航拍地图。图中数字表示海拔,0表示海洋,1~9都表示陆地。小明的飞机将会降落在(6,8)处,现在需要
计算出小明将落地所在岛的面积(即有多少个格子)。注意此处把与小明降落点上下左右相链接的陆地视为同一岛屿。
题意很简单,即给出一个点,求这个点所在的岛屿即可。数字只要不是0,都不用理会大小。

样例
输入

10 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

输出:38

1.bfs方法

#include<iostream>
using namespace std;

struct note
{
	int x;
	int y; 
};
struct note que[2505];
int head, tail;
int a[55][55];
int book[55][55];

int main()
{
	int n;
	int startx, starty;
	int sum;
	int next[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };

	cin >> n >> startx >> starty;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];

	//队列初始化
	head = 1;
	tail = 1;
	que[1].x = startx;
	que[1].y = starty;
	tail++;
	book[startx][starty] = 1;
	sum = 1;

	while (head < tail)
	{
		for (int i = 0; i <= 3; i++)
		{
			int tx = que[head].x + next[i][0];
			int ty = que[head].y + next[i][1];

			if (tx<1 || tx>n || ty<1 || ty>n)
				continue;

			if (a[tx][ty] > 0 && book[tx][ty] == 0)
			{
				sum++;
				book[tx][ty] = 1;
				que[tail].x = tx;
				que[tail].y = ty;
				tail++;
			}
		}
		head++;
	}

	cout << sum;
}

2.dfs方法

#include<iostream>
using namespace std;

int n;
int a[55][55];
int book[55][55];
int startx, starty;
int sum;
int  next1[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };//next会报错

void dfs(int x, int y)//now为当前点
{
	for (int i = 0; i <= 3; i++)
	{
		int tx = x + next1[i][0];
		int ty = y + next1[i][1];
		if (tx<1 || tx>n || ty<1 || ty>n)
			continue;
		if (book[tx][ty] == 0 && a[tx][ty] > 0)
		{
			sum++;
			book[tx][ty] = 1;
			dfs(tx, ty);
		}
	}
	return;
}

int main()
{
	cin >> n >> startx >> starty;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];

	book[startx][starty] = 1;//一定记得标记第一个点
	sum = 1;
	dfs(startx, starty);
	cout << sum;
	return 0;
}

由此引出

1.染色法:在dfs中加入参数color,即每一组dfs都改变color数值为某一定值

void dfs(int x, int y, int color)
{
........
a[x][y]=color;
}
........
dfs(startx,starty,color);

输出

  1  2  1  0  0  0  0  0  2  3
  3  0  2  0 -1 -1 -1  0  1  2
  4  0  1  0 -1 -1 -1 -1  0  1
  3  2  0  0  0 -1 -1 -1  0  0
  0  0  0  0  0  0 -1 -1 -1  0
  0 -1 -1 -1  0 -1 -1 -1 -1  0
  0 -1 -1 -1 -1 -1 -1 -1 -1  0
  0  0 -1 -1 -1 -1 -1 -1  0  0
  0  0  0 -1 -1 -1 -1  0  1  2
  0  0  0  0  0  0  0  0  1  0

2.独立子图个数:(即对不同子图染不同的色)
与所学离散产生联系了~

int num=0;
for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			if (a[i][j] > 0)
			{
				num--;//小岛染色编号
				book[i][j] = 1;
				dfs(i, j, num);
			}
		}

输出

 -1 -1 -1  0  0  0  0  0 -2 -2
 -1  0 -1  0 -3 -3 -3  0 -2 -2
 -1  0 -1  0 -3 -3 -3 -3  0 -2
 -1 -1  0  0  0 -3 -3 -3  0  0
  0  0  0  0  0  0 -3 -3 -3  0
  0 -3 -3 -3  0 -3 -3 -3 -3  0
  0 -3 -3 -3 -3 -3 -3 -3 -3  0
  0  0 -3 -3 -3 -3 -3 -3  0  0
  0  0  0 -3 -3 -3 -3  0 -4 -4
  0  0  0  0  0  0  0  0 -4  0
发布了7 篇原创文章 · 获赞 3 · 访问量 422

猜你喜欢

转载自blog.csdn.net/weixin_44223646/article/details/104242336
4.5