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>
#include<string>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 0; i<n; i++) {
		int a, f;
		cin >> a;
		f = a*a;
		char b[100][100], c[100][100];
		for (int j = 0; j < a; j++) {
			for (int z = 0; z < a; z++) {
				b[j][z] = ' ';
				c[j][z] = ' ';
			}
		}
		int h = (a - 1), w = 0, h2 = -1, w2 = 0;
		b[h][w] = '#';
		c[h][w + 1] = '#';
		c[h][w] = '#';
		f -= 2;
		int k = 0;
		while (f >= 0) {
			if (h + h2 >= a || h + h2 < 0 || w + w2 >= a || w + w2 < 0 || c[h + h2][w+w2] == '#' ) {
				if (h2 == -1) {
					h2 = 0;
					w2 = 1;
				}
				else if (h2 == 1) {
					h2 = 0;
					w2 = -1;
				}
				else if (w2 == -1) {
					w2 = 0;
					h2 = -1;
				}
				else if (w2 == 1) {
					w2 = 0;
					h2 = 1;
				}
			}
			h += h2;
			w += w2;
			if (c[h][w] != '#')
				f--;
			b[h][w] = '#';
			c[h][w] = '#';
			if (h2 == -1) {
				if (c[h][w + 1] != '#')
					f--;
				c[h][w + 1] = '#';
			}
			else if (h2 == 1) {
				if (c[h][w - 1] != '#')
					f--;
				c[h][w - 1] = '#';
			}
			else if (w2 == -1) {
				if (c[h - 1][w] != '#')
					f--;
				c[h - 1][w] = '#';
			}
			else if (w2 == 1) {
				if (c[h + 1][w] != '#')
					f--;
				c[h + 1][w] = '#';
			}
			if (f == 0) {
				k++;
				if (k ==2) {
					f--;
				}
			}
			if (a % 2 == 1 && f == 0)
				break;
		}
		for (int i = 0; i<a; i++) {
			for (int j = 0; j<a; j++) {
				cout << b[i][j];
			}
			cout << endl;
		}
		if (i < n - 1)
			cout << endl;
	}
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

void solve(char a[105][105], int inp, int d, int y, int x){
    int i;
    for(i = 0; i < inp - 1; i++){
        a[y][x] = '#';
        if(d == 0){
            if(a[y - 2][x] == '#') break;
            y--;
        }else if(d == 1){
            if(a[y][x + 2] == '#') break;
            x++;
        }else if(d == 2){
            if(a[y + 2][x] == '#') break;
            y++;
        }else{
            if(a[y][x - 2] == '#') break;
            x--;
        }
    }
    if(i <= 1) return;
    solve(a, inp, (d + 1) % 4, y, x);

}

int main(){
    int n;
    char a[105][105];
    cin >> n;
    rep(i,n){
        rep(i,105) rep(j,105) a[i][j] = ' ';
        int inp;
        cin >> inp;
        solve(a, inp, 0, 104, 0);
        for(int i = 104 - inp; i <= 104; i++){
            rep(j,inp){
                cout << a[i][j];
            }
            cout << endl;
        }
    }
}

C++: #include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(),n.end()
#define pb push_back
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define INF (1<<27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const int dx[4] = {-1,0,+1,0};
const int dy[4] = {0,-1,0,+1};

class Solve{
private:
  int d;
  int imap[110][110];
public:

  Solve(int n):d(n){}
  ~Solve(){}

  int Connect(P p){
    int cnt = 0;
    rep(i,4){
      int nx = p.F+dx[i],ny = p.S+dy[i];
      if(!(0<=nx && nx <d && 0<= ny && ny < d))continue;
      if(imap[ny][nx] == 1)cnt++;
    }
    return cnt;
  }


  void display(){
      cout << "--- in display ---" << endl;
      rep(i,d){
	rep(j,d){
	  if(imap[i][j] == 1)cout << '#';
	  else cout << ' ';
	}
	cout << endl;
      }
      cout << endl;
    }


  void run(){
    rep(i,d+1)rep(j,d+1)imap[i][j] = 0;

    rep(i,d){
      imap[0][i] = 1;
      imap[i][0] = 1;
      imap[i][d-1] = 1;
    }

  


    P pos = P(d-1,d-1);
    int dir = 0;
    int cnt = 0;
    bool fin = false;
    while(true){

      int nx,ny;
      nx  = pos.F,ny = pos.S;
      while(true){
	//cout << "nx = " << nx << ",ny = "<< ny << ",dir = " << dir << endl;
	//display();


	if(!(Connect(P(nx,ny)) == 2 || Connect(P(nx,ny)) == 1)){
	  imap[ny-dy[dir]][nx-dx[dir]] = 0;
	  //cout << "Lase nx = " << nx << ",ny = "<< ny << endl;
	  //cout << "FIN! connnect = " << Connect(P(nx,ny)) << endl;
	  fin = true;
	  break;
	}

	if(imap[ny+dy[dir]][nx+dx[dir]] == 1){
	  //cout << "IN nx = " << nx+dx[dir] << ",ny = " << ny+dy[dir] << endl;
	  imap[ny][nx] = 0;
	  cnt--;
	  break;
	}
	imap[ny][nx] = 1;
	cnt++;	


	nx += dx[dir],ny += dy[dir];
      }
      //cout << "------ cnt = " << cnt << endl;
      if(fin){
	//cout << "dir = " << dir << endl;
	break;
      }
      if(cnt == 0)break;
      cnt =0;

      pos.F = nx-dx[dir],pos.S = ny-dy[dir];
      dir++;
      dir = dir%4;
    }

    rep(i,d){
      rep(j,d){
	if(imap[i][j] == 1)cout << '#';
	else cout << ' ';
      }
      cout << endl;
    }


  }


};


int main(){
  int T;
  cin >> T;
  while(T-- > 0){
    int _n;
    cin >> _n;
    Solve ans = Solve(_n);
    ans.run();
  }
return 0;
}

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

using namespace std;

int n;
char pattern[104][104];

void rec(int d, bool c, int y, int x) {
  pattern[y][x] = '#';
  switch (d) {
  case 0:
    if (pattern[y-2][x] == '#')
      if (c && pattern[y+1][x+1] != '#') rec(1, false, y, x);
      else return;
    else
      rec(0, true, y-1, x);
    break;
  case 1:
    if (pattern[y][x+2] == '#')
      if (c && pattern[y+1][x-1] != '#') rec(2, false, y, x);
      else return;
    else
      rec(1, true, y, x+1);
    break;
  case 2:
    if (pattern[y+2][x] == '#')
      if (c && pattern[y-1][x-1] != '#') rec(3, false, y, x);
      else return;
    else
      rec(2, true, y+1, x);
    break;
  case 3:
    if (pattern[y][x-2] == '#')
      if (c && pattern[y-1][x+1] != '#') rec(0, false, y, x);
      else return;
    else
      rec(3, true, y, x-1);
    break;
  default:
    break;
  }
}

int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++) {
    memset(pattern, ' ', sizeof pattern);
    cin >> n;
    for (int j = 0; j < n+4; j++)
      pattern[0][j] = pattern[n+3][j] = pattern[j][0] = pattern[j][n+3] = '#';
    rec(0, true, n+1, 2);
    for (int y = 2; y < n+2; y++) {
      for (int x = 2; x < n+2; x++)
        cout << pattern[y][x];
      cout << endl;
    }
    if (i != d-1) cout << endl;
  }
}