蓝桥杯 打印十字图(BFS染色法)

本文介绍了一种使用C++实现的算法,用于生成可自定义层数的十字形徽标。通过广度优先搜索(BFS)算法,从中心点开始向外扩展,实现了在控制台输出不同层数的十字形图案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
在这里插入图片描述
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。

输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
在这里插入图片描述
样例输入2
3
样例输出2
在这里插入图片描述
提示
请仔细观察样例,尤其要注意句点的数量和输出位置。

思路:

首先存入中间的那个十字的坐标,然后以此为中心向外拓展
第偶次拓展记为1,标记“$”

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

using namespace std;

bool book[200][200];
int map_[200][200];

int N;

struct node
{
    int x;
    int y;
    int s;
} date;
queue<node>q;

int next_[9][5] =
{
    { 0, 0},
    { 0,-1},
    {-1, 0},
    { 0, 1},
    { 1, 0},
    {-1,-1},
    {-1, 1},
    { 1, 1},
    { 1,-1}
};

void pre()
{
    int midx = 2 * N + 3;
    int midy = 2 * N + 3;
    date.s = 0;
    book[midx][midy] = true;
    map_[midx][midy] = 1;
    
    
    date.x = midx - 1;
    date.y = midy;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx - 2;
    date.y = midy;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx;
    date.y = midy + 1;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx;
    date.y = midy + 2;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx + 1;
    date.y = midy;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx + 2;
    date.y = midy;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx;
    date.y = midy - 1;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;

    date.x = midx;
    date.y = midy - 2;
    q.push(date);
    book[date.x][date.y] = true;
    map_[date.x][date.y] = 1;
}

void print()
{
    int M = 4*N + 5;
    for(int i = 1;i<=M;i++)
    {
        for(int j = 1;j<=M;j++)
        {
            if(map_[i][j])
                printf("$");
            else
                printf(".");
        }
        if(i!=M)
            printf("\n");
    }
}

void bfs()
{
    int K = 2 * N;
    int tx,ty,ts = 0;
    int ms = 0;

    while (ms<K)
    {
        for (int j = 1; j <= 8; j++)
        {
            tx = q.front().x + next_[j][0];
            ty = q.front().y + next_[j][1];
            ts = q.front().s;
            if (!book[tx][ty])
            {
                book[tx][ty] = true;
                date.x = tx;
                date.y = ty;
                date.s = ts + 1;
                q.push(date);
                ms = min(ts,date.s); //注意是取最小的step 
                if(date.s%2==0)
                    map_[tx][ty]  = 1;
            }
        }
        q.pop();
    }
}



int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);

    scanf("%d", &N);
    pre();
    bfs();
    print();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值