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
样例输出复制
#
##
 #
###
  #
###
####
   #
#  #
####
#####
    #
### #
#   #
#####
题目来源

ACM-ICPC 2017 Asia Qingdao

 

题意:给一个n,让你输出一个外圈长度为n的蚊香,蚊香都知道是啥样把!而且蚊香每一圈互相不能粘在一起。

 

题解:模拟,模拟之前一定要找规律,找到规律速度会快很多

1、第一行,第一列和最后一行是一定都要输出的

2、按顺序右、下、左、上四个方向走,如果按当前方向在继续往下走两步已经是#,换下一个方向走,走完n圈就结束

 

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
using namespace std;
char s[50][50];
int main()
{
    int n,t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        if(n==1)
        {
            cout<<'#'<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"##"<<endl;
            cout<<" #"<<endl;
            continue;
        }
        if(n==4)
        {
            cout<<"####"<<endl;
            cout<<"   #"<<endl;
            cout<<"#  #"<<endl;
            cout<<"####"<<endl;
            continue;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                s[i][j]=' ';
        }
        for(int i=1;i<=n;i++)//第一行、第n行,第n列一定都是#
        {
            s[1][i]='#';
            s[i][n]='#';
            s[n][i]='#';
        }

        int x=n,y=1,temp=n;
        while(temp--)
        {
            for(int i=x;i>=1;i--)//向上
            {
                if(s[i-2][y]=='#')
                    break;
                s[i-1][y]='#';
                x--;
            }
            for(int i=y;i<=n;i++)//向右
            {
                if(s[x][i+2]=='#')
                    break;
                s[x][i+1]='#';
                y++;
            }
            for(int i=x;i<=n;i++)//向下
            {
                if(s[i+2][y]=='#')
                    break;
                s[i+1][y]='#';
                x++;
            }
            for(int i=y;i>=1;i--)//向左
            {
                if(s[x][i-2]=='#')
                    break;
                s[x][i-1]='#';
                y--;
            }
        }
        //这里要特别判断一下,因为当n为偶数的时候最后会多输出一个#
        if(n%4==0)
            s[x-1][y+1]=' ';
        else if(n%2==0)
            s[x][y]=' ';
        ///
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                cout<<s[i][j];
            cout<<endl;
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/-citywall123/p/11368490.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值