Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	out := make([][]string, 100)
	for i := range out {
		out[i] = make([]string, 100)
	}
	dx := []int{0, 1, 0, -1}
	dy := []int{-1, 0, 1, 0}
	scanner.Scan()
	a, _ := strconv.Atoi(scanner.Text())
	for a > 0 {
		a--
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				out[i][j] = " "
			}
			out[i][b] = "0"
		}
		if b == 1 {
			fmt.Println("#")
			if a != 0 {
				fmt.Println()
			}
			continue
		}
		if b == 2 {
			fmt.Println("##")
			if a != 0 {
				fmt.Println()
				continue
			}
		}
		row := b -1
		col := 0
		dir := 0
		l := 0
		out[row][col] = "#"
		for {
			nrow := row+dy[dir%4]
			ncol := col+dx[dir%4]
			if dir%4 == 0 && nrow == 0 || dir%4==3&&ncol==0 || dir%4==2&&nrow==b-1 || dir%4==1&&ncol==b-1 {
				row=nrow
				col=ncol
				out[row][col]="#"
				dir++
				l = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if l < 3 {
					break
				}
				dir++
				l = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				l++
			}
		}
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				fmt.Printf("%s", out[i][j])
			}
			fmt.Println()
		}
		if a != 0 {
			fmt.Println()
		}
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <complex>
#include <string>

using namespace std;

#define loop(i,a,b) for(int i=a; i<int(b); i++)
#define rep(i,b) loop(i,0,b)

double const pi = acos(-1);
typedef complex<int> P;
char a[128][128];
int n;

void solve(){
    rep(i,n+4){
        rep(j,n+4){
            if(i*j==0 || (n+3-i)*(n+3-j)==0)a[i][j]='#';
            else a[i][j]=' ';
        }
        a[i][n+4]='\0';
    }

    P d(-1,0), p(n+1,2);
    while(1){
        a[p.real()][p.imag()]='#';
        int y=p.real(), x=p.imag();
        if(a[y+2*d.real()][x+2*d.imag()]=='#')d/=P(0,1);
        if(a[y+2*d.real()][x+2*d.imag()]=='#')break;
        p+=d;
        int c=0;
        rep(i,4){
            int ny=(p+d).real(), nx=(p+d).imag();
            if(a[ny][nx]=='#')c++;
            d*=P(0,1);
        }
        if(c>1)break;
    }
}

int main(){
    int d;cin>>d;
    rep(i,d){
        cin>>n;
        solve();
        rep(i,n){
            rep(j,n)cout<<a[i+2][j+2];
            cout<<endl;
        }
        if(i+1!=d)cout<<endl;
    }
}

C++: #include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int check(int,int,int);
int a[100][100];
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
int n;
int main(void)
{
	int i,j,k,deg,dd,cnt,x,y,xx,yy,d;
	int flg;
	scanf("%d",&d);
	for(k=0;k<d;k++)	{
		if(k!=0)	printf("\n");
		scanf("%d",&n);
		for(i=0;i<n;i++)	{
			for(j=0;j<n;j++)	{
				a[i][j]=0;
			}
		}
		y=n-1;	x=0;	deg=0;
		a[y][x]=1;
		flg=1;
		while(flg==1)	{
			if(check(x,y,deg)==0)	{
				dd=(deg+1)%4;
				if(check(x,y,dd)==0)	{
					flg=0;
					break;
				}
				else {
					deg=dd;
					x=x+dx[deg];	y=y+dy[deg];
					a[y][x]=1;
				}
			}
			else {
				x=x+dx[deg];	y=y+dy[deg];
				a[y][x]=1;		
			}
		}
		for(i=0;i<n;i++)	{
			for(j=0;j<n;j++)	{
				if(a[i][j]==1)	{
					printf("#");
				}
				else {
					printf(" ");
				}
			}
			printf("\n");
		}
	}
	return 0;
}
int check(int x,int y,int d1)
{
	int xx,yy,flg,i,j,dd,x2,y2;
	xx=x+dx[d1];	yy=y+dy[d1];
	if(xx<0 || xx>=n || yy<0 || yy>=n)	{
		return 0;
	}
	x2=xx+dx[d1];	y2=yy+dy[d1];
	if(x2>=0 && x2<n && y2>=0 && y2<n && a[y2][x2]==1)	{
		return 0;
	}
	dd=(d1+1)%4;
	x2=xx+dx[dd];	y2=yy+dy[dd];
	if(x2>=0 && x2<n && y2>=0 && y2<n && a[y2][x2]==1)	{
		return 0;
	}	
	return 1;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int h, w;

bool can(int y, int x){
	return (0 <= y && y < h && 0 <= x && x < w);
}

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};

signed main() {
	int t, n;
	cin >> t;
	rep(k, 0, t){
		int a[110][110]={};
		cin >> n;
		h = w = n;
		vi d;
		int tmp = n;
		d.push_back(tmp);
		while(1){
			if(tmp <= 2){
				d.push_back(tmp);
				break;
			}
			rep(i,0,2) d.push_back(tmp);
			tmp -= 2;
		}
		int y = n - 1, x = 0;
		a[y][x] = 1;
		rep(i, 0, d.size()){
			int dir = i % 4;
			rep(j, 0, d[i] - 1){
				y += dy[dir];
				x += dx[dir];
				a[y][x] = 1;
			}
		}
		rep(i, 0, n){
			rep(j, 0, n){
				cout << (a[i][j] ? "#":" ") ;
			}
			cout << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

char field[101][101];

int dy[]={0,-1,0,1};
int dx[]={-1,0,1,0};
int n;

bool check(int x,int y,int angle){
	if(!(y>=0&&x>=0&&y<n&&x<n)){
		return false;
	}

	for(int i = 0; i < 4; i++){
		if(angle==i)
			continue;
		int nx=dx[i]+x;
		int ny=dy[i]+y;
		if(!(ny>=0&&nx>=0&&ny<n&&nx<n)){
			continue;
		}
		if(field[ny][nx]=='#'){
			return false;
		}
	}
	return true;
}

int main(){

	int nn;
	cin>>nn;
	for(int i = 0; i < nn; i++){
		if(i!=0)
			cout<<endl;
		cin>>n;
		memset(field,0,sizeof(field));
		// 0:ツ右,1:ツ可コ,2:ツ債カ,3:ツ湘」
		int angle=3;
		int cx=0;
		int cy=n-1;
		int cnt=0;
		field[cy][cx]='#';
		while(1){
			if(angle==3){
				int ny=cy-1;
				int nx=cx;
				if(check(nx,ny,angle)){
					field[ny][nx]='#';
					cnt=0;
					cy=ny;
					cx=nx;
				}
				else{
					if(cnt>=1){
						break;
					}
					cnt++;
					angle=0;
				}
			}
			else if(angle==2){
				int ny=cy;
				int nx=cx-1;
				if(check(nx,ny,angle)){
					field[ny][nx]='#';
					cnt=0;
					cy=ny;
					cx=nx;
				}
				else{
					if(cnt>=1){
						break;
					}
					cnt++;
					angle=3;
				}
			}
			else if(angle==1){
				int ny=cy+1;
				int nx=cx;
				if(check(nx,ny,angle)){
					field[ny][nx]='#';
					cnt=0;
					cy=ny;
					cx=nx;
				}
				else{
					if(cnt>=1){
						break;
					}
					cnt++;
					angle=2;
				}
			}
			else if(angle==0){
				int ny=cy;
				int nx=cx+1;
				if(check(nx,ny,angle)){
					field[ny][nx]='#';
					cnt=0;
					cy=ny;
					cx=nx;
				}
				else{
					if(cnt>=1){
						break;
					}
					cnt++;
					angle=1;
				}
			}
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cout<<field[i][j];
			}
			cout<<endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int n,d;

bool inside(int x,int y){
	if(x>=0 && x<d && y>=0 && y<d)return true;
	else return false;
}

int main(){
	cin>>n;
	rep(p,n){
		if(p!=0)cout<<endl;
		cin>>d;
		char ch[d][d];
		rep(i,d)rep(j,d)ch[i][j]='.';
		int dir=0;
		int x=0,y=d-1;
		while(1){
			int check=0;
			if(dir==0){
				while(inside(x,y)){
					ch[y][x]='#';
					if(inside(x,y-1) && ch[y-1][x]=='.'){
						ch[y][x+1]='-';
						y--;
						check|=1;
					}else{if(ch[y][x+1]!='-')x++;break;} 
				}
			}else if(dir==1){
				while(inside(x,y)){
					ch[y][x]='#';
					if(inside(x+1,y) && ch[y][x+1]=='.'){
						ch[y+1][x]='-';
						x++;
						check|=2;
					}else{if(ch[y+1][x]!='-')y++;break;}
				}
			}else if(dir==2){
				while(inside(x,y)){
					ch[y][x]='#';
					if(inside(x,y+1) && ch[y+1][x]=='.'){
						ch[y][x-1]='-';
						y++;
						check|=4;
					}else{if(ch[y][x-1]!='-')x--;break;}
				}
			}else{
				while(inside(x,y)){
					ch[y][x]='#';
					if(inside(x-1,y) && ch[y][x-1]=='.'){
						ch[y-1][x]='-';
						x--;
						check|=8;
					}else{if(ch[y-1][x]!='-')y--;break;}
				}
			}
			dir++;
			dir%=4;
			if(check==0)break;
		}
		rep(i,d){
			rep(j,d){
				if(ch[i][j]=='#')cout<<'#';
				else cout<<' ';
			}
			cout<<endl;
		}
	}
	return 0;
}