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;
}

 

posted @ 2019-08-17 14:21  知道了呀~  阅读(504)  评论(0编辑  收藏  举报