Problem:


ぐるぐる模様

「ぐるぐる模様」を表示するプログラムを作成することにしました。「ぐるぐる模様」は以下のようなものとします。

1 辺の長さが n の場合、n 行 n 列の文字列として表示する。

左下隅を基点とし，時計回りに回転する渦状の模様とする。

線のある部分は #（半角シャープ）、空白部分は " "（半角空白）で表現する。

線と線の間は空白を置く。

整数 n を入力とし，1 辺の長さが n の「ぐるぐる模様」を出力するプログラムを作成してください。

Input

入力は以下の形式で与えられます。

d
n1
n2
:
nd

１行目にデータセットの数 d (d ≤ 20)、続く d 行に i 個目のぐるぐる模様の辺の長さ ni (1 ≤ ni ≤ 100) がそれぞれ１行に与えられます。

Output

データセットごとに、ぐるぐる模様を出力してください。データセットの間に１行の空行を入れてください。

Sample Input

2
5
6

Output for the Sample Input 

#####
#   #
# # #
# # #
# ###

######
#    #
# ## #
# #  #
# #  #
# ####



Sample input:
2
5
6


Sample output:
#####
#   #
# # #
# # #
# ###

######
#    #
# ## #
# #  #
# #  #
# ####

Submission status: WA: Presentation Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0141

"""
import sys
from sys import stdin
input = stdin.readline
from itertools import cycle


def guruguru(n):
    line = 0
    dirs = cycle([(0, -1), (1, 0), (0, 1), (-1, 0)])

    M = [[' '] * n for _ in range(n)]
    cx = 0
    cy = n - 1

    while line < n:
        line += 1
        dx, dy = dirs.__next__()
        while True:
            M[cy][cx] = '#'
            nx = cx + dx
            ny = cy + dy
            nnx = nx + dx
            nny = ny + dy

            if nx < 0 or nx >= n:
                break
            if ny < 0 or ny >= n:
                break
            if nny < 0 or nny >= n or nnx < 0 or nnx >= n:
                pass
            else:
                if M[nny][nnx] != ' ':
                    break
            cx = nx
            cy = ny

    for l in M:
        print(''.join(map(str, l)))


def main(args):
    n = int(input())
    for _ in range(n):
        size = int(input())
        guruguru(size)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: WA: Presentation Error

Solution code:
class Vector:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self, offset):
        self.x += offset[0]
        self.y += offset[1]

    def move_offset(self, offset, multiple=1):
        x = self.x + offset[0] * multiple
        y = self.y + offset[1] * multiple
        return Vector(x, y)


NOTHING = " "
EXIST = "#"
SENTINEL = "?"

MOVE = [
    [[-1, -1], [-1, +0], [-1, +1]],
    [[-1, +1], [-0, +1], [+1, +1]],
    [[+1, +1], [+1, +0], [+1, -1]],
    [[+1, -1], [+0, -1], [-1, -1]],
]


def create_area(size):
    area = [[SENTINEL] * 2 + [NOTHING] * size + [SENTINEL] * 2 for _ in range(size)]
    tmp = [[SENTINEL] * size + [SENTINEL] * 2 * 2]
    area = tmp * 2 + area + tmp * 2
    return area


def even_spiral_pattern(area, point):
    move_index = 0
    area[point.x][point.y] = EXIST

    while True:

        left, center, right = MOVE[move_index]
        end1, end2 = point.move_offset(left), point.move_offset(right)
        offset, offset2 = point.move_offset(center), point.move_offset(center, 2)

        if area[end1.x][end1.y] == EXIST or area[end2.x][end2.y] == EXIST:
            return area
        elif area[offset.x][offset.y] == NOTHING and area[offset2.x][offset2.y] != EXIST:
            point.move(center)
            area[point.x][point.y] = EXIST
        else:
            move_index += 1
            move_index %= 4


def odd_spiral_pattern(area, point):
    move_index = 0
    is_end = False
    area[point.x][point.y] = EXIST

    while True:

        left, center, right = MOVE[move_index]
        offset, offset2 = point.move_offset(center), point.move_offset(center, 2)

        if area[offset.x][offset.y] == NOTHING and area[offset2.x][offset2.y] != EXIST:
            point.move(center)
            area[point.x][point.y] = EXIST
            is_end = False
        else:
            if is_end:
                return area
            else:
                is_end = True

            move_index += 1
            move_index %= 4


def output(area):
    output = ["".join(item).replace(SENTINEL, "") for item in result[2:-2]]
    output = "\n".join(output)
    return output


for _ in range(int(input())):
    size = int(input())

    area = create_area(size)
    point = Vector(size - 1 + 2, 2)

    if size % 2 == 0:
        result = even_spiral_pattern(area, point)
    else:
        result = odd_spiral_pattern(area, point)

    print(output(result))

Submission status: WA: Presentation Error

Solution code:
for r in range(input()):
	if r > 0: print
	n = input()
	if n < 3:
		for i in range(n): print "#"*(n-i)
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if y+dy == -1: raise
			if S[y+dy][x+dx] == "#":
				y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])

Submission status: WA: Presentation Error

Solution code:
for r in range(input()):
	n = input()
	if n < 3:
		for i in range(n): print "#"*(n-i)
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if S[y+dy][x+dx] == "#":
				if y+dy != -1:
					y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])

Submission status: WA: Presentation Error

Solution code:
for r in range(input()):
	if r > 0: print
	n = input()
	if n == 2:
		print "##"
	if n < 3:
		print "#"
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if S[y+dy][x+dx] == "#":
				if y+dy != -1:
					y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])

Submission status: WA: Presentation Error

Solution code:
for r in range(input()):
	n = input()
	if n == 2:
		print "##"
	if n < 3:
		print "#"
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if S[y+dy][x+dx] == "#":
				if y+dy != -1:
					y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])

Submission status: WA: Presentation Error

Solution code:
for r in range(input()):
	n = input()
	if n < 3:
		for i in range(n+1):
			print "#"*(n-i)
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if S[y+dy][x+dx] == "#":
				if y+dy != -1:
					y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])
	print

Submission status: WA: Presentation Error

Solution code:
d = input()
for r in range(d):
	n = input()
	if n < 3:
		for i in range(n): print "#"*(n-i)
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if S[y+dy][x+dx] == "#":
				if y+dy != -1:
					y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])
	if r != d-1: print

Submission status: WA: Presentation Error

Solution code:
d = input()
for r in range(d):
	n = input()
	if n < 3:
		for i in range(n): print "#"*(n-i)
	else:
		S = [[" "]*n for i in range(n)]
		x,y,dx,dy = 0,n-1,0,-1
		while 1:
			S[y][x] = "#"
			y += dy; x += dx
			try:
				if S[y+dy][x+dx] == "#":
					if y+dy != -1:
						y -= dy; x -= dx
					raise
			except:
				S[y][x] = "#"
				dx,dy = -dy,dx
				y += dy; x += dx
			if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
		for i in range(n):
			print "".join(S[i])
	if r != d-1: print