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<vector>
#include<map>
#include<climits>
#include<set>
#include<queue>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<string>
#include<bitset>
#include<cstring>
#include<list>
using namespace std;
typedef vector<int>VI;
typedef vector<VI>VVI;
typedef vector<string>VS;
typedef pair<int,int>PII;
typedef long long LL;
typedef pair<LL,LL>PLL;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define PB push_back
#define EACH(i,c) for(typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define F first
#define S second

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

char fld[200][200];
int N;
void init(){
    fill(*fld,*fld+40000,' ');
    REP(i,N+4)fld[0][i]=fld[N+3][i]=fld[i][0]=fld[i][N+3]='#';
}

void solve(){
    int x=2,y=N+1,d=1;
    while(true){
        fld[y][x]='#';
        bool f2=false;
        REP(i,4){
            int nd=(d+i)%4;
            int ny=y+dy[nd];
            int nx=x+dx[nd];
            bool flag=true;
            REP(j,4){
                if(j==(nd+2)%4)continue;
                if(fld[ny+dy[j]][nx+dx[j]]=='#')flag=false;
            }
            if(flag){
                x=nx;
                y=ny;
                d=nd;
                f2=true;
                break;
            }

        }
        if(f2==false)return;
    }
}


int main(){
    int n;cin>>n;
    bool flag=false;
    while(n--){
        cin>>N;
        if(flag)cout<<endl;
        else flag=true;
        init();
        solve();
        if(N==2){
            cout<<"##"<<endl;
            cout<<"# "<<endl;
            continue;
        }
        FOR(i,2,N+2){
            FOR(j,2,N+2)cout<<fld[i][j];
            cout<<endl;
        }
    }

    return 0;
}

C++: #include <stdio.h>
#include <stack>
#include <math.h>

using namespace std;

int n;

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= n-1 && col >= 0 && col <= n-1)return true;
	else{
		return false;
	}
}

int main(){

	int d,row,col,move;
	char current,table[100][101];
	bool FLG,First = true;

	scanf("%d",&d);

	for(int loop=0; loop < d; loop++){
		scanf("%d",&n);


		if(!First)printf("\n");

		for(int i=0; i < n; i++){
			for(int k=0; k < n; k++)table[i][k] = ' ';
		}

		row = n-1,col = 0;
		current = 'U';

		FLG = true;

		while(FLG){
			move = 0;

			switch(current){
			case 'U':

				for(;row >= 0; row--){
					if((rangeCheck(row-1,col) == true && table[row-1][col] == '#')||
					   (rangeCheck(row,col-1) == true && table[row][col-1] == '#')||
					   (rangeCheck(row,col+1) == true && table[row][col+1] == '#'))break;

					table[row][col] = '#';
					move++;
				}
				if(move == 0){
					FLG = false;
				}else{
					row += 1;
					col += 1;
					current = 'R';
				}
				break;
			case 'R':

				for(;col <= n-1; col++){
					if((rangeCheck(row-1,col) == true && table[row-1][col] == '#')||
					   (rangeCheck(row+1,col) == true && table[row+1][col] == '#')||
					   (rangeCheck(row,col+1) == true && table[row][col+1] == '#'))break;

					table[row][col] = '#';
					move++;
				}
				if(move == 0){
					FLG =false;
				}else{
					col -= 1;
					row += 1;
					current = 'D';
				}
				break;
			case 'D':

				for(; row <= n-1; row++){
					if((rangeCheck(row,col-1) == true && table[row][col-1] == '#')||
					   (rangeCheck(row+1,col) == true && table[row+1][col] == '#')||
					   (rangeCheck(row,col+1) == true && table[row][col+1] == '#'))break;

					table[row][col] = '#';
					move++;
				}
				if(move == 0){
					FLG = false;
				}else{
					row -= 1;
					col -= 1;
					current = 'L';
				}
				break;
			case 'L':

				for(; col >= 0; col--){
					if((rangeCheck(row-1,col) == true && table[row-1][col] == '#')||
					   (rangeCheck(row+1,col) == true && table[row+1][col] == '#')||
					   (rangeCheck(row,col-1) == true && table[row][col-1] == '#'))break;

					table[row][col] = '#';
					move++;
				}
				if(move == 0){
					FLG = false;
				}else{
					col += 1;
					row -= 1;
					current = 'U';
				}
				break;
			}
		}

		for(int i=0; i < n; i++){
			for(int k=0; k < n; k++)printf("%c",table[i][k]);
			printf("\n");
		}

		if(First)First = false;

	}


    return 0;
}

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

using namespace std;

typedef unsigned long long ull;

#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 m;
bool isIn(int x, int y){
    return 0 <= x && x < m && 0 <= y && y < m;
}

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;
        cin >> m;
        if(m == 1){cout << "#" << endl; continue;}
        else if(m == 2){cout << "##" << endl; cout << "# " << endl; continue;}
        rep(i, 110)rep(j, 110)field[i][j] = false;
        int x = 0, y = 0, d = 0;
        field[0][0] = true;
        while(true){
            bool change = false;
            while(isIn(x+dx[d], y+dy[d])){
                if(field[x+dx[d]][y+dy[d]]){
                    field[x][y] = false;
                    x -= dx[d]; y -= dy[d];
                    break;
                }
                change = true;
                x += dx[d]; y += dy[d];
                field[x][y] = true;
            }
            if(!change) break;
            d = (d+1)%4;
        }
        if(m%2 == 1){
            x += dx[d]; y += dy[d];
            field[x][y] = true;
        }

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