简单搜索 打印十字图 蓝桥杯历届试题

简单搜索题

我们为了方便只取左上部分的四分之一进行分析,其他部分对称赋值即可

此图简单分析之后发现是一圈符号套一圈符号,这就和求联通块基本相同原理

开始时初始化为

#####

#####

####$

####$

##$$$

第一圈搜索连通的‘$’符号之后如下

#####
###. . 
###. $
#. . . $
#. $$$
第二圈搜索同理可得如下
##$$$
##$. . 
$$$. $
$. . . $
$. $$$
第三圈如下
#. $$$
. . $. . 
$$$. $
$. . . $
$. $$$
左上角点符号特判即可


#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;

typedef long long ll;
int n,count1,count2;
char s[100][10000];
int vis[100][10000];
struct node{
	int x;
	int y;
};
node a,next;
int towards1[3][2]={-1,0,0,-1,-1,-1};
int towards2[4][2]={1,0,-1,0,0,1,0,-1};

void bfs(char change,int sx,int sy,char search)
{
	queue<node> Q;
	a.x=sx;
	a.y=sy;
	vis[a.x][a.y]=1;
	Q.push(a);
	while(!Q.empty())
	{
		a=Q.front();
		Q.pop();
		
		next=a;
		for(int i=0;i<3;i++)//将左,上,左上进行变换
		{
			int tx=a.x+towards1[i][0];
			int ty=a.y+towards1[i][1];
			if(tx>=1&&tx<=count1/2+1&&ty>=1&&ty<=count2/2+1&&s[tx][ty]!=change&&s[tx][ty]!=search)
			{
				s[tx][ty]=change;
			}
		}
		
		for(int i=0;i<4;i++)//四方向寻找连通的相同符号
		{
			int tx=a.x+towards2[i][0];
			int ty=a.y+towards2[i][1];
			if(tx>=1&&tx<=count1/2+1&&ty>=1&&ty<=count2/2+1&&s[tx][ty]==search&&vis[tx][ty]==0)
			{
				next.x=tx;
				next.y=ty;
				vis[next.x][next.y]=1;
				Q.push(next);
			}
		}
	}
}

int main()
{
	scanf("%d",&n);
	count1 = 9+(n-1)*4;
	count2 = 9+(n-1)*4;
	memset(s,'#',sizeof(s));
	memset(vis,0,sizeof(vis));
	s[1][1]='.';
	s[count1/2+1][count2/2+1]='$';
	s[count1/2+1][count2/2+1-1]='$';
	s[count1/2+1][count2/2+1-2]='$';
	s[count1/2+1-1][count2/2+1]='$';
	s[count1/2+1-2][count2/2+1]='$';
	
	int a1,a2;
	a1=count1/2+1;
	a2=count2/2+1;
	for(int i=1;i<=n*2+1;i++)//循环判断,用奇偶判定改变的符号和寻找的符号
	{
		if(i%2==1)
		{
			bfs('.',a1,a2,'$');
		}
		else 
		{
			bfs('$',a1,a2,'.');
		}
		a1--;
		a2--;
	}
	
	for(int i=1;i<=count1/2+1;i++)//对称赋值
	{
		for(int j=1;j<=count2/2+1;j++)
		{
			s[count1-i+1][j]=s[i][j];
			s[i][count2-j+1]=s[i][j];
			s[count1-i+1][count2-j+1]=s[i][j];
		}
	}
	
	for(int i=1;i<=count1;i++)
	{
		for(int j=1;j<=count2;j++)
		{
			printf("%c",s[i][j]);
		}
		printf("\n");
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值