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++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i,0,(n))
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n, m)
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
vector<string> s;

bool outOfRange(int y, int x){
	if(y < 0 || y >= n || x < 0 || x >= n){
		return true;
	}
	return false;
}

bool check(int ny, int nx, int dir){
	if(outOfRange(ny, nx)) return true;
	for(int i = 0; i < 4; i++){
		if(i == (dir+2)%4) continue;
		int nny = ny + dy[i], nnx = nx + dx[i];
		if((!outOfRange(nny, nnx)) && s[nny][nnx] == '#'){
			return true;
		}
	}
	return false;
}

int main(){
	int d;
	cin>>d;
	while(d--){
		cin>>n;
		s = vector<string>(n,string(n, ' '));
		pii pos = pii(n-1, 0);
		int dir = 0;
		while(1){
			if(s[pos.fi][pos.se]=='#') break;
			s[pos.fi][pos.se] = '#';
			int ny = pos.fi+dy[dir], nx = pos.se+dx[dir];
			int cnt = 4;
			while(check(ny, nx, dir) &&(--cnt)){
				dir++;
				dir%=4;
				ny = pos.fi+dy[dir]; nx = pos.se+dx[dir];

			}
			if(cnt == 0) break;
			pos=pii(ny ,nx);
		}
		rep(i, n){
			cout<<s[i]<<endl;
		}
	}
}

C++: #include <iostream>

#include <string>

using namespace std;

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

int main(){

	int d;

	cin >> d;

	for(int t = 0;t < d;t++){

		if(t) cout << endl;

		char s[102][102];

		int n;

		cin >> n;
		
		if(n == 1){
			cout << "#" << endl;
			continue;
			}

		for(int i = 0;i < n;i++){

			for(int j = 0;j < n;j++) s[i][j] = ' ';

		}

		int x = n - 1,y = 0,dir = 0;

		for(int k = 0;k < n;k++){

			bool flag = false;

			while(1){

				int nx = x + dx[dir],ny = y + dy[dir];

			 if(nx < 0 || nx >= n + 1 || ny < 0 || ny >= n + 1 || s[nx][ny] == '#') break;

s[x][y] = '#';

flag = true;

				x = nx;

				y = ny;

			}

if(x)x -= dx[dir];

if(y)y -= dy[dir];

			if(!flag) break;

			dir = (dir + 1) % 4;

		}

for(int i = 0;i < n;i++) {

for(int j = 0;j < n;j++){

cout << s[i][j];

}

cout << endl;}

	}

}

C++: 
int n;
char G[100][100]; // grid
static int di[4] = {-1, 0, 1, 0};
static int dj[4] = {0, 1, 0, -1};

int main(){
    int dir, width, i, j, a, w;
    int t; scanf("%d", &t);
    for ( int i = 0; i < t; i++ ){
      scanf("%d", &n);
      
      for ( i = 0; i < n; i++ )
        for ( j = 0; j < n; j++ ) G[i][j] = ' ';
      
      width = n - 1;
      i = width; j = dir = 0;
      
      for ( a = 1; a <= (n + n % 2); a++, dir = (dir + 1) % 4 ){
        for ( w = 0; w <= width; w++ ) G[i+di[dir]*w][j+dj[dir]*w] = '#';
        i += di[dir] * width;
        j += dj[dir] * width;
        if ( a % 2 == 1 && a >= 3 ) width -= 2;
      }
      
      for ( i = 0; i < n; i++ ){
        for ( j = 0; j < n; j++ ) printf("%c", G[i][j]);
        printf("\n");
      }
    }
}

C++: #include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
  const int dx[4] = {1,0,-1,0};
  const int dy[4] = {0,1,0,-1};
  int n,d;
  string s;
  for(cin>>n;n>0;n--){
    cin>>d;
    s = "";
    for(int i=0;i<d;i++) s+=" ";
    vector<string> spiral(d);
    for(int i=0;i<d;i++)spiral[i] = s;
    int x=0,y=d-1,dir=2,i=0;
    while(i<=d){
      spiral[y][x] = '#';
      if((i<4&&(y==0||y==d-1)&&(x==0||x==d-1))||(i>3&&spiral[y+2*dy[dir]][x+2*dx[dir]]=='#')){
	dir = (++dir)%4;
	i++;
	if(d==1)break;
      }
      x+=dx[dir];
      y+=dy[dir];
    }
    for(int y=0;y<d;y++)
      cout<<spiral[y]<<endl;
    if(n>1)cout<<endl;
  }
  return 0;
}

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]='#';
        REP(i,4){
            int nd=(d+i)%4;
            int ny=y+dy[nd];
            int nx=x+dx[nd];
            bool flag=true;
            if(fld[ny][nx]=='#')return;
            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;
                break;
            }
        }
    }
}


int main(){
    int n;cin>>n;
    while(n--){
        cin>>N;
        init();
        solve();
        FOR(i,2,N+2){
            FOR(j,2,N+2)cout<<fld[i][j];
            cout<<endl;
        }
    }

    return 0;
}

C++: #include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
// U    0
//L R  3 1
// D    2
int n;
char d[200][200];
int dx[]={0,1,0,-1},dy[]={-1,0,1,0};
void ff(int y,int x,int dir){
	d[y][x]=' ';
	if(d[y+dy[dir]*2][x+dx[dir]*2]=='#'&&y+dy[dir]*2>=0&&y+dy[dir]*2<n&&x+dx[dir]*2>=0&&x+dx[dir]*2<n)
		ff(y+dy[dir],x+dx[dir],dir);
	else
	if(d[y+dy[(dir+1)%4]][x+dx[(dir+1)%4]]!=' '&&d[y+2*dy[(dir+1)%4]][x+2*dx[(dir+1)%4]]=='#'){
		ff(y+dy[(dir+1)%4],x+dx[(dir+1)%4],(dir+1)%4);
	}
}
int main(){
	int s;
	cin>>s;
	while(s--){
		cin>>n;
		rep(i,n)
		rep(j,n)
		d[i][j]='#';
		ff(n-1,1,0);
		rep(i,n){
		rep(j,n)
			cout<<d[i][j];
			cout<<endl;
		}
		if(s!=0)
		cout<<endl;
	}
	return 0;
}