【搜索】训练题J-山峰和山谷 Ridges and Valleys

题意

棋盘每个数字表示高度,四周高中间低为山谷,反之为山峰,要求统计山峰和山谷的数量

思路

枚举每个点,对每个点进行对8个方向跑bfs,判断该方块周围的高度情况。

代码

#include<iostream>
#include<cmath>
#include<stack>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>

#define Endl "\n"
typedef long long ll;
const int maxn=1e3+5;
const int mod=1e9+7;
using namespace std;
int a[maxn][maxn];
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int n,ans1=0,ans2=0,high=0,low=0;
bool p[1005][1005];
struct location{
	int x;
	int y;
	int height;
};

void bfs(int x,int y,int height)
{
	queue<location> q;
	location begin;
	begin.x=x;
	begin.y=y;
	begin.height=height;
	q.push(begin);	
	while(!q.empty())
	{
	
		location temp;
		temp = q.front();
		q.pop();
		int xx=temp.x;
		int yy=temp.y;
		int key=temp.height;
//		cout<<"------------"<<Endl;
		for(int i=0;i<8;i++)
		{
//			cout<<"i="<<i<<Endl;
			int nx = xx+dir[i][0];
			int ny = yy+dir[i][1];
//			cout<<"dir"<<dir[0][i]<<" "<<dir[1][i]<<Endl;
//			cout<<"nx="<<nx<<" "<<"ny="<<ny<<Endl;
			if(nx>=0&&nx<n&&ny>=0&&ny<n)
			{
				if(!p[nx][ny]&&a[nx][ny]==key)
				{
					location m;
					m.x=nx;
					m.y=ny;
					m.height=key;
					q.push(m);
					p[nx][ny]=1;
				}
				
				if(a[nx][ny]<key) low++;
				if(a[nx][ny]>key) high++;
	//			cout<<low<<" "<<high<<Endl;
			}	
		}
	//	cout<<"------------"<<Endl;
	}
}


int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
			p[i][j]=0;
		}
			
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
		{
			if(!p[i][j])
			{
				high = 0;
				low = 0;	
				p[i][j]=1;
				bfs(i,j,a[i][j]);
				if(high&&low)	continue;
				else
					 if(high&&!low) ans1++;
				else
					 if(!high&&low) ans2++;
			}
		}
	if(!ans1&&!ans2)
		cout<<"1 1"<<Endl;
	else cout<<ans2<<" "<<ans1<<Endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
theme_ridges()是一个基于ggplot2的R包ggjoy的主,用于绘制joyplot(也称为ridge plot)。该主可以帮助用户更好地可视化密度数据,并提供了许多自定义选项,以满足用户的需求。 以下是theme_ridges()的一些常见参数: 1. ridge_linetype - 设置joyplot线条的类型 2. ridge_color - 设置joyplot线条的颜色 3. ridge_alpha - 设置joyplot线条的透明度 4. ridge_size - 设置joyplot线条的大小 5. axis_text_size - 设置x轴和y轴标签的字体大小 6. axis_text_color - 设置x轴和y轴标签的颜色 7. axis_ticks_length - 设置x轴和y轴刻度线的长度 8. axis_ticks_color - 设置x轴和y轴刻度线的颜色 9. panel_border_color - 设置joyplot面板的边框颜色 10. panel_border_size - 设置joyplot面板的边框大小 11. panel_background_color - 设置joyplot面板的背景颜色 12. legend_position - 设置图例的位置 13. legend_title - 设置图例的标 14. legend_text_size - 设置图例文本的字体大小 15. legend_text_color - 设置图例文本的颜色 16. title_text_size - 设置图表标的字体大小 17. title_text_color - 设置图表标的颜色 18. subtitle_text_size - 设置图表副标的字体大小 19. subtitle_text_color - 设置图表副标的颜色 20. caption_text_size - 设置图表注释的字体大小 21. caption_text_color - 设置图表注释的颜色 总之,theme_ridges()是一个功能强大的ggplot2主,可以帮助用户更好地呈现密度数据,并提供了许多自定义选项以满足用户的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值