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 <vector>
#include <string>
using namespace std;

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

inline bool in(const int x, const int y, const int w, const int h)
{
  if (0 <= x && x < w && 0 <= y && y < h)
    return true;
  return false;
}

int main()
{
  int d;
  cin >> d;
  while (d--) {
    int n;
    cin >> n;
    vector<string> p(n, string(n, '#'));

    bool update = true;
    int x = 1, y = n-1, d = 3, cnt = 0;
    while (update) {
      update = false;
      for (int i = 0; i < 4 && !update; ++i) {
	int xx = x + dx[d], yy = y + dy[d];
	if (in(xx, yy, n, n) && p[yy][xx] == '#' &&
	    in(xx+dx[d], yy+dy[d], n, n) && p[yy+dy[d]][xx+dx[d]] == '#') {
	  p[y][x] = ' ';
	  x += dx[d];
	  y += dy[d];
	  update = true;
	} else {
	  d = (d+1) % 4;
	}
      }
    }

    p[y][x] = ' ';
    for (int i = 0; i < n; ++i)
      cout << p[i] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LIFT 3

using namespace std;

bool p[100][100];

void dfs (int len, int dir, int r, int c, int cnt )
{
	if (len <= 1 )
		return;

	dir = (dir + 1) % 4;
	if (cnt != 0 && cnt % 2 == 0 ){
		len -= 2;
	} // end if 

	int cr = r, cc = c;
	switch (dir ){
		case UP:
			for (int i = 0; i < len; i++, cr-- )
				p[cr][cc] = true;
			cr++;
			break;
		case DOWN:
			for (int i = 0; i < len; i++, cr++ )
				p[cr][cc] = true;
			cr--;
			break;	
		case RIGHT:
			for (int j = 0; j < len; j++, cc++ )
				p[cr][cc] = true;
			cc--;
			break;
		case LIFT:
			for (int j = 0; j < len; j++, cc-- )
				p[cr][cc] = true;
			cc++;
			break;
	} // end switch

	if (len == 2 )
		return;

	dfs (len, dir, cr, cc, cnt + 1 );
}

void disp_p (int m )
{
	rep (i, m ){
		rep (j, m ){
			if (p[i][j] )
				cout << '#';
			else
				cout << ' ';
		}// end rep
		cout << endl;
	} // end rep
}

int main()
{
	int n;

	while (cin >> n && n ){
		for (int i = 0; i < n; i++ ){
			int m;
			cin >> m;
			if (m == 1 ){
				cout << "#" << endl;
			}else if (m == 2 ){
				cout << "##" << endl;
				cout << "# " << endl;
			}else{
				memset (p, false, sizeof (p ) );
				for (int r = m - 1; r >= 0; r-- )
					p[r][0] = true;
				for (int c = 0; c < m; c++ )
					p[0][c] = true;
				for (int r = 0; r < m; r++ )
					p[r][m-1] = true;
				dfs (m-2, DOWN, m-1, m-1, 0 );
				disp_p (m );
			} // end if
		} // end if
	} // end whlie

	return 0;
}

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

#define MAX 104

enum direction{UP,RIGHT,DOWN,LEFT};

int main()
{
	int d;
	cin >> d;

	for(int x = 0; x < d; ++x){
		int n;
		int s, t;
		int dir;
		short m[MAX][MAX] = {{0,}};
		cin >> n;

		if( x > 0 )
			cout << endl;

		for(int i = 0; i < n + 4; ++i){
			if( i == 0 || i == n+3 )
				for(int j = 1; j < n+3; ++j)
					m[i][j] = 1;
			m[i][0] = m[i][n+3] = 1;
		}

		s = n + 1;
		t = 2;
		dir = UP;
		while( true )
		{
			int cnt = 0;
			const int ds[] = {-1,0,1,0};
			const int dt[] = {0,1,0,-1};

			m[s][t] = 1;

			for(int i = 0; i < 4; ++i){
				if( m[ s + ds[i] ][ t + dt[i] ] )
					++cnt;
			}
			if( cnt >= 2 ){
				m[s][t] = 0;
				break;
			}

			switch( dir ){
				case UP: if( m[s-2][t] ) dir = RIGHT; break;
				case RIGHT: if( m[s][t+2] ) dir = DOWN; break;
				case DOWN: if( m[s+2][t] ) dir = LEFT; break;
				case LEFT: if( m[s][t-2] ) dir = UP; break;
			}
			switch( dir ){
				case UP: --s; break;
				case RIGHT: ++t; break;
				case DOWN: ++s; break;
				case LEFT: --t; break;
			}
		}

		for(int i = 2; i < n + 2; ++i){
			for(int j = 2; j < n + 2; ++j)
				cout << ( m[i][j] ? '#' : ' ' );
			cout << endl;
		}
	}
	return 0;
}

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();
    if(T != 0)cout << endl;
  }
return 0;
}