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 xdir[4] = {0, 1, 0, -1}, ydir[4] = {-1, 0, 1, 0}, d;
char a[100][100];

bool check(int x, int y)
{
  int cnt = 0;
  for(int i = 0; i < 4; i++){
    int dx = x+xdir[i], dy = y+ydir[i];
    if(a[dy][dx] == '#') cnt++;
  }

  return cnt == 1;
}

void func(int x, int y, int dr)
{
  int dx = x+xdir[dr], dy = y+ydir[dr];
  int tx = x+2*xdir[dr], ty = y+2*ydir[dr];
  a[y][x] = '#';

  if((dx < 0 || dy < 0 || dx == d || dy == d)){
    int td = (dr+1)%4;
    dx = x+xdir[td]; dy = y+ydir[td];
    if(dx < 0 || dy < 0 || dx == d || dy == d) return ;
    tx = x+2*xdir[td]; ty = y+2*ydir[td];
    if(tx >= 0 && tx < d && ty >= 0 && ty < d && a[ty][tx] == ' ') func(dx, dy, td);
  } else if(tx >= 0 && tx < d && ty >= 0 && ty < d && a[ty][tx] == '#'){
    int td = (dr+1)%4;
    dx = x+xdir[td]; dy = y+ydir[td];
    tx = x+2*xdir[td]; ty = y+2*ydir[td];
    if(tx >= 0 && tx < d && ty >= 0 && ty < d && a[ty][tx] == '#') return ;
    if(check(dx, dy)) func(dx, dy, td);
  } else {
    func(dx, dy, dr);
  }

  return ;
}

int main()
{
  int n;
  bool f = false;

  while(cin>>n)
    while(n--){
      cin >> d;
      if(f) cout << endl;
      f = true;
      
      for(int i = 0; i < 100; i++)
	for(int j = 0; j < 100; j++) a[i][j] = ' ';
      
      func(0, d-1, 0);
      
      for(int i = 0; i < d; i++){
	for(int j = 0; j < d; j++) cout << a[i][j];
	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]='#';
        bool f2=false;
        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;
                f2=true;
                break;
            }
        }
        if(f2==false)return;
    }
}


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>
#include <vector>

using namespace std;

int main(void){
  int d;cin>>d;
  const int POS[][2]={{0,-1},{1,0},{0,1},{-1,0}};
  while(d--){
    int n;cin>>n;
    vector<vector<bool> > field(n+2);
    for(int i=0;i<=n+1;++i){
      field[i].reserve(n+2);
      for(int j=0;j<=n+1;++j)field[i][j]=(i==n+1||j==n+1)?1:0;
    }
    int x=0,y=n-1,p=0;
    for(;;){
      int dx=POS[p][0],dy=POS[p][1];
      int px=POS[(p+4+1)%4][0],py=POS[(p+4+1)%4][1],qx=POS[(p+4-1)%4][0],qy=POS[(p+4-1)%4][1];
      field[y][x]=1;
      x+=dx,y+=dy;
      if(y==0&&x==0||(y+2*dy>=0&&x+2*dx>=0&&field[y+2*dy][x+2*dx]==1))p=(p+1)%4;
      if((y+dy+py>=0&&x+dx+px>=0&&field[y+dy+py][x+dx+px]==1)||(y+dy+qy>=0&&x+dx+qx>=0&&field[y+dy+qy][x+dx+qx]==1))
      	break;
    }
    for(int i=0;i<n;++i){
      for(int j=0;j<n;++j)cout<<((field[i][j])?'#':' ');
      cout<<endl;
    }
  }
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int nn;
  cin >> nn;
  REP(datan,nn) {
    int n;
    cin >> n;
    
    char c[n][n];
    memset(c,' ',sizeof(c));
    int dx[] = {0,1,0,-1};
    int dy[] = {-1,0,1,0};

    int x = 0, y = n - 1;

    c[x][y] = '#';
    int h = n-1;
    int d = 0;
    int j = 0;
    while(h>0) {
      REP(i,h) {
        x += dx[d];
        y += dy[d];
        c[x][y]=  '#';
      }

      if (h==1) break;
      d = (d+1)%4;
      if (j>=2 && j%2==0)
        h-=2;
      j++;
    }
    if (datan) cout << endl;
    REP(y,n) {
      REP(x,n) {
        cout << c[x][y];
      }
      cout << endl;
    }
  }
}