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, 101)
	}
	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.Printf("##\n# \n")
			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++: #include <iostream>
using namespace std;

int main()
{
	int d;

	while( cin >> d ){
		for( int i = 0; i < d; ++i ){
			if( i != 0 ) cout << endl;
			int n;
			cin >> n;

			int size = n + 4;
			char* map = new char[ size * size ]();
			for( int j = 0; j < size; ++j ){
				for( int k = 0; k < size; ++k ){
					map[ j * size + k ] = ' ';
				}
			}
			for( int j = 0; j < size; ++j ){
				map[ j ] = map[ ( size - 1 ) * size + j ] = map[ size * j ] = map[ ( j + 1 ) * size - 1 ] = '#';
			}
			for( int j = 2; j < size - 2; ++j ){
				map[ j * size + 2 ] = '#';
			}
			int x = 2, y = 2;
			int dir = 1;
			int dirCount = 0;
			int straightCount = 0;
			while( dirCount < 2 ){
				map[ y * size + x ] = '#';
				++straightCount;
				int nx, ny;
				switch( dir ){
					case 0: nx = x; ny = y - 2; break;
					case 1: nx = x + 2; ny = y; break;
					case 2: nx = x; ny = y + 2; break;
					case 3: nx = x - 2; ny = y; break;
				}
				if( map[ ny * size + nx ] == '#' ){
					if( straightCount < 3 ){
						break;
					}
					dir = ( dir + 1 ) % 4;
					++dirCount;
					straightCount = 0;
					continue;
				}else{
					dirCount = 0;
				}
				switch( dir ){
					case 0: y -= 1; break;
					case 1: x += 1; break;
					case 2: y += 1; break;
					case 3: x -= 1; break;
				}
			}

			for( int p = 2; p < size - 2; ++p ){
				for( int q = 2; q < size - 2; ++q ){
					cout << map[ p * size + q ];
				}
				cout << endl;
			}

			delete[] map;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
	char da[102][102];
	int d,n,i,j,x,y,c,dx,dy,dm;
	cin >> d;
	for (;d>0;d--) {
		cin >> n;
		for (i=0;i<n+1;i++) for (j=0;j<n+1;j++) da[i][j]=' ';
		for (i=0;i<n+2;i++) {da[i][n+1]='#'; da[n+1][i]='#';}
		dx=0; dy=-1; x=0; y=n-1; c=0;
		while(true) {
			da[y][x]='#';
			if (da[y+dy*2][x+dx*2]=='#' || y+dy==-1) {
				c++; dm=dx; dx=-dy; dy=dm;
				if (c==n) break;
			}
			x+=dx; y+=dy;
		}
		for (i=0;i<n;i++) {
			for (j=0;j<n;j++) cout << da[i][j];
			cout << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

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

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    bool field[110][110];
    int n;
    cin >> n;
    for(int p=0; p < n; p++){
        if(p != 0) cout << endl;
        int m;
        cin >> m; m--;
        if(m == 0){cout << "#" << endl; continue;}
        rep(i, 110)rep(j, 110)field[i][j] = false;
        int x = 0, y = 0, d = 0;
        while(true){
            if(field[x+dx[d]][y+dy[d]]) break;
            int count = 0;
            for(int i=0; i< m && !field[x+dx[d]*2][y+dy[d]*2]; i++){
                count++;
                field[x][y]= true;
                x += dx[d]; y += dy[d];
                field[x][y] = true;
            }
            if(count == 1) break;
            d = (d+1)%4;
        }

        for(int i=m; 0 <= i; i--){
            for(int j=0; j <= m; j++){
                cout << (field[i][j] ? "#" : " ");
            }
            cout << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
using namespace std;

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

void debug(vector<string> m){
	for(int y=0 ; y < n ; y++ ){
		for(int x=0 ; x < n ; x++ ){
			cout << m[y][x];
		}
		cout << endl;
	}
}

void solve(){
	vector<string> m(n);
	for(int y=0 ; y < n ; y++ ){
		for(int x=0 ; x < n ; x++ ){
			m[y].push_back(' ');
		}
	}
	int x = 0, y = n-1, d=0;
	m[y][x] = '#';
	int cnt=0;
	while( cnt < 4 ){
		int mx, my;
		bool flag = false;
		for(int i=-1 ; i <= 1 ; i++ ){
			mx = x + dx[d%4];
			my = y + dy[d%4];
			mx = mx + dx[(d-i+4)%4];
			my = my + dy[(d-i+4)%4];
			if( mx >= 0 && my >= 0 && mx < n && my < n && m[my][mx] == '#' ){
				flag = true;
			}
		}
		if( flag ){
			d = (d + 1) % 4;
			cnt++;
			continue;
		}
		
		mx = x + dx[d%4];
		my = y + dy[d%4];
		if( mx < 0 || my < 0 || mx >= n || my >= n ){
			d = (d + 1) % 4;
			cnt++;
			continue;
		}
		m[my][mx] = '#';
		x = mx;
		y = my;
		cnt = 0;
	}
	for(int y=0 ; y < n ; y++ ){
		cout << m[y] << endl;
	}
}

int main(){
	int d;
	cin >> d;
	for(int t=0 ; t < d ; t++ ){
		cin >> n;
		solve();
		if( t != d-1 ){
			cout << endl;
		}
	}
}

C++: #include <iostream>
#include <cstring>
using namespace std;
char buf[150][150];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
bool check(int tx,int ty)
{
    if(tx<0||ty<0)return false;
    return buf[tx][ty]=='#';
}
void solve(int x)
{
    memset(buf,' ',sizeof(buf));
    for(int i=1;i<=x;i++)
    {
        for(int j=1;j<=x;j++)
        {
            buf[i][j]='#';
        }
    }
    int X=x,Y=2;
    int cnt=0;
    for(int p=0;p<=10000;p++)
    {
        if(X>x+10||Y>x+10||X<0||Y<0)break;
        if(x%4==1&&X==(x+3)/2&&Y==(x+1)/2)break;
        if(x%4==3&&X==(x+1)/2&&Y==(x+3)/2)break;
        buf[X][Y]=' ';
        X+=dx[cnt];Y+=dy[cnt];
        if(check(X+dx[cnt],Y+dy[cnt])&&!check(X+dx[cnt]*2,Y+dy[cnt]*2))
        {
            cnt++;
            cnt%=4;
        }
    }
    for(int i=1;i<=x;i++)
    {
        for(int j=1;j<=x;j++)
        {
            cout << buf[i][j];
        }
        cout << endl;
    }
    return;
}
int main()
{
    int q;
    cin >> q;
    for(int i=0;i<q;i++)
    {
        int k;
        cin >> k;
		if(k==2)
		{
			cout << "##" << endl;
			cout << "# " << endl;
		}
        else solve(k);
		if(i!=q-1)cout << endl;
    }
    return 0;
}