[蓝桥杯]真题讲解:岛屿个数(BFS遍历图)

[蓝桥杯]真题讲解:岛屿个数(BFS遍历图)

一、视频讲解

视频讲解
在这里插入图片描述

二、暴力代码(也是正解代码)

//岛屿个数:搜索(BFS/DFS)
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 100;

int g[N][N];//存地图
int n, m;//地图的长和宽


//避免走重复路造成死循环
bool st_sea[N][N];//判断哪个海已经访问过了。
bool st_road[N][N];//判断哪个陆地已经访问过了。

//方向向量
//海水的方向向量
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

//陆地的方向向量
int ddx[4] = {-1, 0, 1, 0};
int ddy[4] = {0, 1, 0, -1};
int ans = 0;

//判断你是否超越了地图的边界。
bool check(int x, int y)
{
	return ( x >= 0 and x < n and y >= 0 and y < m);		
}


//找到了x,y所在的岛屿,并且把该岛屿中的其他的1都标记成了true。
void bfs_road(int x, int y)
{

	queue<pii>q;
	st_road[x][y] = true;
	q.push({x, y});
	while(q.size())
	{
		auto t = q.front();
		q.pop();
		for(int i = 0; i < 4; i ++)
		{
			int nx = t.first + ddx[i];
			int ny = t.second + ddy[i];
			if(check(nx, ny) and g[nx][ny] and !st_road[nx][ny])
			{
				st_road[nx][ny] = true;
				q.push({nx, ny});
			}
		}
	}
}

void bfs_sea(int x, int y)
{
	queue<pii>q;
	st_sea[x][y] = true;
	q.push({x, y});
	while(q.size())
	{
		auto t = q.front();
		q.pop();
		for(int i = 0; i < 8; i ++)
		{	
			int nx = t.first + dx[i];
			int ny = t.second + dy[i];
			if(check(nx, ny) and !g[nx][ny] and !st_sea[nx][ny])
			{
				st_sea[nx][ny] = true;
				q.push({nx, ny});
			}

			if(check(nx, ny) and g[nx][ny] and !st_road[nx][ny])
			{
				ans ++;
				bfs_road(nx, ny);
			}
		}

	}
}


void solve()
{
	cin >> n >> m;

	ans = 0;
	for(int i = 0; i <= n; i ++)
		for(int j = 0; j <= m; j ++)
			st_sea[i][j] = st_road[i][j] = false;

	for(int i = 0; i < n; i ++)
	{
		string s; cin >> s;
		for(int j = 0; j < m; j ++)
			g[i][j] = s[j] - '0';
	}

	bool flag = false;
	for(int i = 0; i < n; i ++)
	{
		for(int j = 0; j < m; j ++)
		{
			if(!i || i == n - 1 || !j || j == m - 1)
			{
				if(!g[i][j] && !st_sea[i][j])
				{
					flag = true;
					bfs_sea(i, j);
				}
			}
		}
	}
	if(!flag)
		ans = 1;
	
	cout << ans << endl;
}	

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	cin >> t;
	while(t--)
	solve();
}
  • 40
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值