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++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<double,int> P;

static const double eps = 10e-9;

const int tgt[][2] = {{0,1},{1,0},{0,-1},{-1,0}};

void dfs(int stage[101][101],int x,int y,int dir,int w){

	//printf("x:%d y:%d\n",x,y);
	if(stage[y][x]) return;	

	int dx;
	int dy;

	int c=0;
	for(int i=0;i<4;i++){
		dx = x+tgt[i][0];
		dy = y+tgt[i][1];
		if(dx < 0 || dx >= w || dy < 0 || dy >= w) continue;
		if(stage[dy][dx]) c++;
	}

	if(c>=2) return;

	dx = x+tgt[dir][0];
	dy = y+tgt[dir][1];

	int jx = x+tgt[dir][0]*2;
	int jy = y+tgt[dir][1]*2;

	stage[y][x] = 1;

	if(!(jx < 0 || jx >= w || jy < 0 || jy >= w) && stage[jy][jx]){
		dx = x+tgt[(dir+1)%4][0];
		dy = y+tgt[(dir+1)%4][1];
		dfs(stage,dx,dy,(dir+1)%4,w);
	}

	else if(dx < 0 || dx >= w || dy < 0 || dy >= w){
		dx = x+tgt[(dir+1)%4][0];
		dy = y+tgt[(dir+1)%4][1];
		dfs(stage,dx,dy,(dir+1)%4,w);
	}


	else dfs(stage,dx,dy,dir,w);

}

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int w;
		scanf("%d",&w);
		int x=0;
		int y=0;

		int stage[101][101];
		memset(stage,0,sizeof(stage));
		int dir = 0;
		int change = 0;

		dfs(stage,0,0,0,w);
		for(int ty = w-1; ty>=0;ty--){
			for(int tx=0; tx<w;tx++){
				cout << (stage[ty][tx] ? '#' : ' ');
			}
			cout << endl;
		}

		if(i<n-1) cout << endl;
	}
}

C++: #include <iostream>
#include <string.h>

using namespace std;
typedef pair<int, int> P;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
bool field[104][104];

void solve()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; ++i)
	{
		memset(field, 1, sizeof(field));
		int num;
		cin >> num;
		for(int j = 0; j < num + 4; ++j)
		{
			field[0][j] = false;
			field[num + 3][j] = false;
		}
		for(int j = 1; j < num + 4; ++j)
		{
			field[j][0] = false;
			field[j][num + 3] = false;
		}
		P pos;
		pos.first = 2;
		pos.second = num + 1;
		int count = num;
		int step = 0;
		field[pos.second][pos.first] = false;
		while(count)
		{
			while(2 <= pos.first && pos.first < (num + 2) && 2 <= pos.second && pos.second < (num + 2))
			{
				if(field[pos.second + 2 * dy[step]][pos.first + 2 * dx[step]] == false)
				{
					break;
				}
				pos.first += dx[step];
				pos.second += dy[step];
				field[pos.second][pos.first] = false;
			}
			step = (step + 1) % 4;
			count--;
		}
		for(int j = 2; j < num + 2; ++j)
		{
			for(int k = 2; k < num + 2; ++k)
			{
				field[j][k] == true ? cout << ' ' : cout << '#';
			}
			cout << endl;
		}
		if(i != n - 1)
		{
			cout << endl;
		}
	}
}

int main()
{
	solve();
	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<char> > 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(;;){
    	if(x<0||y<0)break;
      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>
using namespace std;

bool in(int x, int y, int n) {
	return 0 <= x && x < n && 0 <= y && y < n;
}

int main() {
	int d;
	cin >> d;
	for(int t = 0; t < d; t++) {
		char str[128][128];
		int n;
		cin >> n;

		for(int i = 0; i < 128; i++)
			for(int j = 0; j < 128; j++)
				str[i][j] = ' ';

		int x = 0, y = n - 1;
		int dir = 0;
		int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
		bool flag = false;
		while(1) {
			str[y][x] = '#';
			/*
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < n; j++)
					cout << ((i == y && j == x) ? '*' : str[i][j]);
				cout << endl;
			}
			cout << (dir == 0 ? "up" : dir == 1 ? "forward" : dir == 2 ? "down" : "back") << endl;
			cout << endl;
			*/

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

			if(in(nx+dx[dir], ny+dx[dir], n))
				turn_flag |= str[ny+dx[dir]][nx+dx[dir]] == '#';
			if(in(nx+dx[dir], ny        , n))
				turn_flag |= str[ny        ][nx+dx[dir]] == '#';
			if(in(nx+dx[dir], ny-dx[dir], n))
				turn_flag |= str[ny-dx[dir]][nx+dx[dir]] == '#';

			if(in(nx, ny+dx[dir], n))
				turn_flag |= str[ny+dx[dir]][nx] == '#';
			if(in(nx, ny        , n))
				turn_flag |= str[ny        ][nx] == '#';
			if(in(nx, ny-dx[dir], n))
				turn_flag |= str[ny-dx[dir]][nx] == '#';

			if(in(nx+dy[dir], ny+dy[dir], n))
				turn_flag |= str[ny+dy[dir]][nx+dy[dir]] == '#';
			if(in(nx        , ny+dy[dir], n))
				turn_flag |= str[ny+dy[dir]][nx        ] == '#';
			if(in(nx-dy[dir], ny+dy[dir], n))
				turn_flag |= str[ny+dy[dir]][nx-dy[dir]] == '#';

			if(in(nx+dy[dir], ny, n))
				turn_flag |= str[ny][nx+dy[dir]] == '#';
			if(in(nx        , ny, n))
				turn_flag |= str[ny][nx        ] == '#';
			if(in(nx-dy[dir], ny, n))
				turn_flag |= str[ny][nx-dy[dir]] == '#';

			if(!in(nx,ny,n))
				turn_flag = true;

			if(turn_flag) {
				if(flag)
					break;
				dir = (dir + 1) % 4;
				flag = true;
			}
			else {
				x += dx[dir];
				y += dy[dir];
				flag = false;
			}
		}

		if(t > 0)
			cout << endl;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++)
				cout << str[i][j];
			cout << endl;
		}
	}
}