Defend the farm (bfs) problem solution

A question that is unfriendly to beginners BFS: defend the farm (Luogu p2919)

Luogu p2919


The title describes that
Farmer John’s farmer has many small hills, and he wants to deploy some bodyguards there to defend his valuable cows.

He wanted to know how many bodyguards he would need to recruit if he deployed a bodyguard on a small hill. He now has a map that uses a matrix of numbers to represent the terrain. This matrix has N rows (1<N≤100) and M columns (1<M≤70). Each element in the matrix has a value H_ij (0≤H_ij≤10000) to indicate the altitude of the area. Please help him count how many small hills there are on the map.

The definition of a small hill is: if all elements adjacent to an element in the map are not higher than the height of this element (or it is adjacent to the border of the map) (that is, less than or equal to), then the element and all its surroundings are in this order The set of arranged elements is called a small hill. The meaning of adjacency here is: if the difference between an element and another abscissa ordinate and its abscissa and ordinate does not exceed 1, then the two elements are said to be adjacent. (That is, there are 8 points adjacent to a non-boundary point, top, bottom, left, right, top left, top right, bottom left, bottom right)

Input format: the
first line, two integers N and M separated by spaces;

The second line to the N+1 line, the I+1 line describes the I line on the map, there are M integers H_ij separated by spaces.

Output format:
one line, the number of small hills.

Input example:
8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0
Output example:
3

Example description:
There are three small hills on the map: the peak position of each hill is in the upper left corner (height is 4), the upper right corner (height is 1), and the bottom (height is 2).


At first glance, I think I need to use dfs, but the teacher stipulates that I need to use bfs (or delete the code)
so, I don’t deserve to learn bfs...


Code 0 (+ did not understand the question not get to know the usage bfs) (in fact, the title describes not clear) crazy rejection pot

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>

using namespace std;

int n,m,ans,kx,ky,map[105][105],a[105][105];
int dx[10]={
    
    0,0,1,-1};
int dy[10]={
    
    1,-1,0,0};

void check(int x,int y)//找到一个判断是不是山丘,后面发现题目不是这样的
{
    
    
	int sum=0;
	if(a[x][y+1]<=a[x][y]&&map[x][y+1]==0)
	sum++;
	if(a[x][y-1]<=a[x][y]&&map[x][y-1]==0)
	sum++;
	if(a[x+1][y]<=a[x][y]&&map[x+1][y]==0)
	sum++;
	if(a[x-1][y]<=a[x][y]&&map[x-1][y]==0)
	sum++;
	if(a[x+1][y+1]<=a[x][y]&&map[x+1][y+1]==0)
	sum++;
	if(a[x+1][y-1]<=a[x][y]&&map[x+1][y-1]==0)
	sum++;
	if(a[x-1][y+1]<=a[x][y]&&map[x-1][y+1]==0)
	sum++;
	if(a[x-1][y-1]<=a[x][y]&&map[x-1][y-1]==0)
	sum++;
	if(sum==8)
	ans++;
	return;
}

void bfs()
{
    
    
	queue<int> qx;
	queue<int> qy;
	qx.push(1);
	qy.push(1);
	kx=qx.front();
	ky=qy.front();
	map[kx][ky]=1;
	while(qx.empty()==0&&qy.empty()==0)
	{
    
    
		kx=qx.front();
		ky=qy.front();
		check(kx,ky);
		for(int i=0;i<4;i++)
		{
    
    
			if(map[kx+dx[i]][ky+dy[i]]==0)
			{
    
    
				map[kx+dx[i]][ky+dy[i]]=1;
				qx.push(kx+dx[i]);
				qy.push(ky+dy[i]);
			}
		}
		qx.pop();
		qy.pop();
	}
	return;
}

int main()
{
    
    
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j<=m;j++)
		{
    
    
			cin>>a[i][j]; 
		} 
	}
	for(int i=1;i<=m;i++)
	{
    
    
		map[0][i]=1;
		map[n+1][i]=1;
	}
	for(int i=1;i<=n;i++)
	{
    
    
		map[i][0]=1;
		map[i][m+1]=1;
	}
    bfs();
    cout<<ans;
	return 0;
}

Then we can know by reading the questions

Then the set of the element and all the surrounding elements arranged in this order is called a small hill

Question:
In a matrix, if the height of an element is greater than or equal to the other eight adjacent elements (which may have
boundaries), then it can be used as the top of a hill, and those eight elements can diffuse outward to form a strict
Blocks that do not rise. (From Luogu user quality player Sun 1 Chao )

"In other words, you have to find all the small hills"

Tips:
Since one hill may contain another hill, starting from the highest can effectively avoid repeated accumulation

AC code

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>

using namespace std;

int n,m,ans,kx,ky,map[105][105],maps[105][105],a[105][105];
int dx[10]={
    
    0,0,0 ,1,1,1 ,-1,-1,-1};
int dy[10]={
    
    0,1,-1,0,1,-1,0 ,1 ,-1};
struct quick_sort//用于快排
{
    
    
	int x,y,h;
}p[100005];

void bfs(int x,int y)//bfs套路模板   $没有奇奇怪怪的骚操作$
{
    
    
	queue<int> qx;
	queue<int> qy;
	qx.push(x);
	qy.push(y);
	while(qx.empty()==0&&qy.empty()==0)
	{
    
    
		kx=qx.front();
		ky=qy.front();
		for(int i=1;i<=8;i++)
		{
    
    
			if(map[kx+dx[i]][ky+dy[i]]==0&&a[kx+dx[i]][ky+dy[i]]<=a[kx][ky]&&(kx+dx[i]>=1&&kx+dx[i]<=n)&&(ky+dy[i]>=1&&ky+dy[i]<=m))//边界
			{
    
    
				map[kx+dx[i]][ky+dy[i]]=1;//标记
				qx.push(kx+dx[i]);
				qy.push(ky+dy[i]);
			}
		}
		qx.pop();
		qy.pop();
	}
	return;
}
bool cmp(quick_sort a,quick_sort b)
{
    
    
	return a.h>b.h;
}

int main()
{
    
    
	cin>>n>>m;
	int w=0;
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j<=m;j++)
		{
    
    
			cin>>a[i][j]; 
			w++;
			p[w].h=a[i][j];
			p[w].x=i;
			p[w].y=j;
		} 
	}
	sort(p+1,p+1+w,cmp); //由于一个山丘里面可能包含另一个山丘,从最高的开始找可以有效避免重复累加
	for(int i=1;i<=w;i++)
	{
    
    
		if(map[p[i].x][p[i].y]==0)//这个点不属于任何一座山
		{
    
    
		map[p[i].x][p[i].y]=1;
		bfs(p[i].x,p[i].y);
		ans++;	
		}
		
	}
    cout<<ans;
	return 0;
}

Bfs novice friendly code (really without those very showy operations)

void bfs(int x,int y)
{
    
    
	queue<int> qx;
	queue<int> qy;
	qx.push(x);
	qy.push(y);
	map[kx][ky]=1;
	while(qx.empty()==0&&qy.empty()==0)
	{
    
    
		kx=qx.front();
		ky=qy.front();
		for(int i=1;i<=8;i++)
		{
    
    
			if(map[kx+dx[i]][ky+dy[i]]==0&&......&&(...>=1&&...<=n)&&(...>=1&&...<=m))
			{
    
    
				map[kx+dx[i]][ky+dy[i]]=1;
				qx.push( );
				qy.push( );
			}
		}
		qx.pop();
		qy.pop();
	}
	return;
}

ε=(´ο`*))) alas, bfs is too difficult

Guess you like

Origin blog.csdn.net/yyh0910/article/details/115190761