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
		len := 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++
				len = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if len < 3 {
					break
				}
				dir++
				len = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				len++
			}
		}
		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>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

void solve(int n)
{
	if(n==1){
		cout<<"#"<<endl;
		return;
	}
	vs a(n,string(n,'#'));
	int di[]={-1,0,1,0};
	int dj[]={0,1,0,-1};
	int ci=n-1,cj=1,k=0;
	rep(i,10000){
		a[ci][cj]=' ';
		int ti=ci+di[k]*2,tj=cj+dj[k]*2;
		if(ti<0 || n<=ti || tj<0 || n<=tj || a[ti][tj]==' ')
			k=(k+1)%4;
		else{
			ci+=di[k];
			cj+=dj[k];
		}
	}
	rep(i,n)
		cout<<a[i]<<endl;
}

int main()
{
	int d; cin>>d;
	rep(i,d){
		if(i) cout<<endl;
		int n; cin>>n;
		solve(n);
	}
	
	return 0;
}

C++: #include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
const int INF = 10000000;
using namespace std;

int main()
{
	char b[100][100];
	int n, d, dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
	
	cin >> d;
	rep(t, d) {
		memset(b, ' ', sizeof(b));
		
		cin >> n;
		rep(i, n) b[0][i]='#';
		int x=0, y=0, nn = n, direct=0;
		
		while (1) {
			for (int i=0; i<nn; i++) {
				b[x][y] = '#';
				x+=dx[direct]; y+=dy[direct];
			}
			x-=dx[direct]; y-=dy[direct];
			direct++; direct%=4;
			
			if (n%2==0 && nn<=2) break;
			
			for (int i=0; i<nn; i++) {
				b[x][y] = '#';
				x+=dx[direct]; y+=dy[direct];
			}
			x-=dx[direct]; y-=dy[direct];
			direct++; direct%=4;
			nn-=2;
			
			if (nn<=1)
				break;
		}
		
		if (t>0) cout << endl;
		rep(y, n) {
			rep(x, n)
			cout << b[x][y];
		cout << endl;
		}

	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char field[101][101];
int N, n, now;
int x, y;
bool flag;

void print(){
  for(int i = 0 ; i <= n ; i++){
    for(int j = 0 ; j <= n ; j++){
      cout << field[i][j];
    }
    cout << endl;
  }
}

void init(){
  flag = false;
  for(int i = 0 ; i < 101 ; i++){
    for(int j = 0 ; j < 101 ; j++){
      field[i][j] = ' ';
    }
  }
  for(int i = n ; i >= 0 ; i--){
    field[i][0] = '#';
  }
  for(int i = 0 ; i < n ; i++){
    field[0][i] = '#';
  }
  for(int i = 0 ; i <= n ; i++){
    field[i][n] = '#';
  }
}

void left(){
  int j;
  for(j = x ; field[y][j-1] != '#' ; j--){
    field[y][j] = '#';
  }
  x = j+1;
}

void up(){
  int i;
  for(i = y ; field[i-1][x] != '#' ; i--){
    field[i][x] = '#';
  }
  y = i+1;
}

void right(){
  int j;
  for(j = x ; field[y][j+1] != '#' ; j++){
    field[y][j] = '#';
  }
  x = j-1;  
}

void down(){
  int i;
  for(i = y ; field[i+1][x] != '#' ; i++){
    field[i][x] = '#';
  }
  y = i-1;
}

    
int main(){
  cin >> N;
  for(int cs = 0 ; cs < N ; cs++){
    cin >> n;
    n--;
    
    if(n == 0) cout << '#' << endl << endl;
    else if(n == 1){
      cout << "##" << endl << "# " << endl << endl;
    }
    else{
    init();
    x = n, y = n;
    int cnt = 3;
    while(1){
      if(cnt == n+1) break;
      left();
      cnt++;
      if(cnt == n+1) break;
      up();
      cnt++;
      if(cnt == n+1) break;
      right();
      cnt++;
      if(cnt == n+1) break;
      down();
      cnt++;
    }
    print();
    if(cs != N-1) cout << endl;
    }
  } 
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int n;
char s[500][500];
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 };
struct st {
	int a, b, c;
};
int main() {
	int d; scanf("%d", &d);
	rep(i, d) {
		scanf("%d", &n);
		memset(s, 0, sizeof(s));
		rep(j, n)rep(k, n)s[j][k] = ' ';
		queue<st>que;
		s[n - 1][0] = '#';
		que.push({ n - 1, 0,0 });
		while (!que.empty()) {
			st p = que.front(); que.pop();
			int x = p.a, y = p.b, idx = p.c;
			int nx = x + dx[idx], ny = y + dy[idx];
			int kx = nx + dx[idx], ky = ny + dy[idx];
			int jx = nx + dx[(idx + 1) % 4], jy = ny + dy[(idx + 1) % 4];
			if (0 <= nx&&nx < n && 0 <= ny&&ny < n&&s[nx][ny] == ' ') {
				if (!(0 <= kx&&kx < n && 0 <= ky&&ky < n&&s[kx][ky] == '#') && !(0 <= jx&&jx < n && 0 <= jy&&jy < n&&s[jx][jy] == '#')) {
					s[nx][ny] = '#';
					que.push({ nx,ny,idx });
					continue;
				}
			}
			idx = (idx + 1) % 4;
			nx = x + dx[idx]; ny = y + dy[idx];
			kx = nx + dx[idx]; ky = ny + dy[idx];
			jx = nx + dx[(idx + 1) % 4]; jy = ny + dy[(idx + 1) % 4];
			if (0 <= nx&&nx < n && 0 <= ny&&ny < n&&s[nx][ny] == ' ') {
				if (!(0 <= kx&&kx < n && 0 <= ky&&ky < n&&s[kx][ky] == '#') && !(0 <= jx&&jx < n && 0 <= jy&&jy < n&&s[jx][jy] == '#')) {
					s[nx][ny] = '#';
					que.push({ nx,ny,idx });
				}
			}
		}
		rep(j, n)printf("%s\n", s[j]);
		if (i != d - 1)printf("\n");
	}
}