2018蓝桥杯C/C++A组 全球变暖(DFS与BFS)

标题:全球变暖
你有一张某海域NxN像素的照片,".“表示海洋、”#"表示陆地,如下所示:

.##…
.##…
…##.
…####.
…###.

####其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。

####由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。

####例如上图中的海域未来会变成如下样子:





…#…

####请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。

【输入格式】
第一行包含一个整数N。 (1 <= N <= 1000)
以下N行N列代表一张海域照片。

照片保证第1行、第1列、第N行、第N列的像素都是海洋。

【输出格式】
一个整数表示答案。
【输入样例】
7

.##…
.##…
…##.
…####.
…###.

【输出样例】
1

思路:就是利用深度优先的算法把把与海洋相邻的陆地变成海洋,再进行一次搜索,查找图中的连通块数量,就是岛屿的数量(DFS)见第一第二段代码

改进:这题的岛屿淹没后有可能产生多个岛屿,所以我们要判断哪些是完全淹没,哪些是不会完全淹没的,甚至会产生型岛屿的。首先就要bfs搜索一共有多少个岛屿,再进行一次bfs找到不会完全淹没的岛屿数量,最后两数字相减便是完全淹没的岛屿数量了;(BFS)见下面第三段代码

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<stdio.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int, int>::iterator it;

struct node
{
	int x, y;
	node(int x = 0, int y = 0) :x(x), y(y) {}
};

int n;
int cnt;
char mp[1005][1005];
int vis[1005][1005];
vector<node> d[maxn];
int ne[4][2] = { 1,0,-1,0,0,1,0,-1 };

void find(int x, int y)//寻找相连点
{
	d[cnt].push_back(node(x, y));
	vis[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int tx = x + ne[i][0];
		int ty = y + ne[i][1];

		if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty] || mp[tx][ty] == '.')
			continue;
		find(tx, ty);
	}

	return;
}

void Preprocess()//预处理岛屿
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (mp[i][j] == '#' && !vis[i][j])
			{
				cnt++;
				find(i, j);
			}
	return;
}

void dfs(int x, int y)//侵蚀
{
	vis[x][y] = 1;

	for (int i = 0; i < 4; i++)
	{
		int tx = x + ne[i][0];
		int ty = y + ne[i][1];

		if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty])
			continue;

		if (mp[tx][ty] == '#')
			vis[tx][ty] = 1;
		else
			dfs(tx, ty);
	}

	return;
}

void solve()//开始侵蚀
{
	mem(vis, 0);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (mp[i][j] == '.' && !vis[i][j])
				dfs(i, j);
	return;
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> mp[i];

	//Preprocess();
	solve();
	Preprocess();
	cout << cnt << endl;
	system("pause");
	return 0;
}

这是精简过后的代码

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
struct node
{
	int x, y;
	node(int x1 = 0, int y1= 0)
	{
		x = x1; y = y1;
	}
};
int n;
int cnt;
char mp[1005][1005];//存符号
int vis[1005][1005];
vector<node> d[1000];
int mov[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };//坐标移动
void find(int x, int y)//寻找相连点
{
	d[cnt].push_back(node(x, y));
	vis[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int tx = x + mov[i][0];
		int ty = y + mov[i][1];
		if (tx < 0 || tx >= n || ty >= n || ty < 0 || vis[tx][ty] || mp[tx][ty] == '.')
			continue;
		find(tx, ty);
	}
	return;
}
void preprocess()//处理岛屿
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (mp[i][j] == '#'&&!vis[i][j])
			{
				cnt++;
				find(i, j);
			}
		}
	}

}
void dfs(int x, int y)//侵蚀
{
	vis[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int tx = x + mov[i][0];
		int ty = y + mov[i][1];
		if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty])
			continue;
		if (mp[tx][ty] == '#')//深度优先的终止条件
		{
			vis[tx][ty] = 1;
		}
		else
			dfs(tx, ty);
	}
	return;
}
void solve()//开始腐蚀
{
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (mp[i][j] == '.' && !vis[i][j])
				dfs(i, j);
		}
	}
	return;
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> mp[i];
	}
	solve();
	preprocess();//查找连通块
	cout << "剩下的岛屿" << cnt << endl;


	system("pause");
	return 0;
}

BFS的代码(真正的答案)

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;



char a[1000][1000];//记录图的信息
int n, vis[1000][1000];
int dir[4][2] = { -1,0,0,-1,0,1,1,0 };
int num = 0;//桥梁陆地
struct point
{
	int x, y;
	point(int a, int b)
	{
		x = a;
		y = b;
	}
};
void find(int x, int y)//寻找相连点
{

	vis[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];

		if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty] || a[tx][ty] == '.')
			continue;
		find(tx, ty);
	}

	return;
}

int bfs(int x, int y)
{
	queue<point> q;//队列
	q.push(point(x, y));
	int left=0;
	while (!q.empty())
	{
		point p = q.front();
		q.pop();
		
		int cnt = 0;//一块陆地周围的陆地数量
		for (int i = 0; i < 4; i++)
		{
			int dx = p.x + dir[i][0];
			int  dy = p.y + dir[i][1];
			if (dx < 0 || dx >= n || dy < 0 || dy >= n)
				continue;
			if (a[dx][dy] == '#')
			{
				cnt++;
				if (!vis[dx][dy])
				{
					vis[dx][dy] = 1;
					q.push(point(dx, dy));
					/*if ((a[dx - 1][dy] == '#'&&a[dx + 1][dy] == '#'&&a[dx][dy + 1] == '.'&&a[dx][dy - 1] == '.') || (a[dx - 1][dy] == '.'&&a[dx + 1][dy] == '.'&&a[dx][dy + 1] == '#'&&a[dx][dy - 1] == '#'))
					{
						num++;
					}*/
				}

			}
			
		}
		if (cnt == 4)
			left++;
		
	}
	return left;
}

int main()
{
	int i, j, ans = 0;//不会被完全淹没的连通块的数量
	int cnt = 0;//淹没前的连通块数量
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (a[i][j] == '#' && !vis[i][j])
			{
				cnt++;
				find(i, j);
			}



	memset(vis, 0, sizeof(vis));
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (a[i][j] == '#' && !vis[i][j])
			{
				vis[i][j] = 1;
				if (!bfs(i, j))
					ans++;
			}
		}
	}
	cout << "被完全淹没的岛屿数量" << endl;
	cout << cnt << "  " << ans << endl;
	cout << cnt - ans  << endl;
	
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值