资源限制
时间限制: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;
}