2017青岛区域赛I题The Squared Mosquito Coil

Lusrica designs a mosquito coil in a board with n × n grids. The mosquito coil is a series of consecutive grids, each two neighboring grids of which share a common border. If two grids in the mosquito coil are not consecutive, they do not share any border, but they can share a common endpoint.

The mosquito coil Lusrica designed starts from the upper left corner of the board. It goes right to the last available grid. Alter the direction and go downward to the last available grid, and alter the direction again going left to the last available grid. To carry on after altering the direction and go upward to the last available grid. Then it goes right again and repeats the above turns.

It ends up in a grid such that the above process cannot be continued any more. Your mission now is to print the whole blueprint of Lusrica's mosquito coil.

Input

This problem has several test cases and the first line contains an integer t (1 ≤ t ≤ 36) which is the number of test cases. For each case a line contains an integer n (1 ≤ n ≤ 36) indicating the size of the board.

Output

For each case with input n, output n lines to describe the whole board. Each line contains n characters. If a grid is a part of Lusrica's mosquito coil, the corresponding character is '#', or ' ' (a single blank) if not.

样例输入
5
1
2
3
4
5
样例输出
#
##
 #
###
  #
###
####
   #
#  #
####
#####
    #
### #
#   #
#####
题意:给你一个n,再n*n的矩形中围成一个类似与蚊香的东西,具体看输出就明白了,像蚊香一样,不能相连,输出这样的矩阵。
题解:模拟,dfs,暴力。。。。都可以,但是觉得写暴力有点太弱的感觉,所以就写模拟了,感觉dfs比模拟的代码量多(其实是自己比较菜。。。)特判了n<=4的情况,然后就是模拟依次按左,下,右,上的方向进行模拟,并用bool数组表示是否已经走过。判断:当前的坐标加上此方向的2倍是填过的就进行下一个方向,如果当前坐标加上下一个方向的2倍已经填过,那么就表示已经填完,所以退出模拟。
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>


using namespace std;


bool vis[50][50]; //标记数组
int dir[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向


int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        if(n==1)
        {
            puts("#");
            continue;
        }
        if(n==2)
        {
            puts("##");
            puts(" #");
            continue;
        }
        if(n==3)
        {
            puts("###");
            puts("  #");
            puts("###");
            continue;
        }
        if(n==4)
        {
            puts("####");
            puts("   #");
            puts("#  #");
            puts("####");
            continue;
        }
        memset(vis,false,sizeof(vis));
        int sx=0,sy=0;
        vis[0][0]=true;
        int flag;
        while(1)
        {
            for(int i=0; i<4; i++)
            {
                flag=0;
                while(1)
                {
                    int nx=sx+dir[i][0],ny=sy+dir[i][1];
                    if(nx>=0&&nx<n&&ny>=0&&ny<n&&!vis[nx][ny]&&!vis[nx+dir[(i+1)%4][0]][ny+dir[(i+1)%4][0]])
                    {
                        sx=nx;
                        sy=ny;
                        vis[nx][ny]=true;
                    }
                    else break;
                    if(vis[nx+2*dir[i][0]][ny+2*dir[i][1]]) break;  //判断是否换方向模拟
                }
                if(vis[sx+2*dir[i][0]][sy+2*dir[i][1]]&&vis[sx+2*dir[(i+1)%4][0]][sy+2*dir[(i+1)%4][1]]) //判断是否已经模拟完
                {
                    flag=1;
                    break;
                }
            }
            if(flag) break;
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(vis[i][j]) cout << "#";
                else cout << " ";;
            }
            cout << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值