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 <stdio.h>
#pragma warning(disable : 4996)
int n, d, x, y, e, f; char c[100][101];
const int dx[4] = { 0, 1, 0, -1 };
const int dy[4] = { -1, 0, 1, 0 };
bool ok() {
	for (int i = 0; i < 4; i++) {
		if (i == (e + 2) % 4) continue;
		int ex = x + dx[i];
		int ey = y + dy[i];
		if (0 <= ex && ex < d && 0 <= ey && ey < d) {
			if (c[ey][ex] == '#') {
				return false;
			}
		}
	}
	return true;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &d);
		x = 0, y = d - 1, e = 0, f = 0;
		for (int j = 0; j < d; j++) {
			for (int k = 0; k < d; k++) {
				c[j][k] = ' ';
			}
			c[j][d] = '\0';
		}
		while (f++ < d * d) {
			c[y][x] = '#';
			x += dx[e % 4];
			y += dy[e % 4];
			if (0 <= x && x < d && 0 <= y && y < d) {
				if (!ok()) {
					x -= dx[e % 4];
					y -= dy[e % 4];
					e++;
				}
			}
			else {
				x -= dx[e % 4];
				y -= dy[e % 4];
				e++;
			}
		}
		for (int i = 0; i < d; i++) printf("%s\n", c[i]);
		printf("\n");
	}
	return 0;
}

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

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

int main()
{
	int d, i, n, j, k;
	int x, y, dir;
	int turn;
	vector<string> s;
	int dx[4] = {0, 1, 0, -1};
	int dy[4] = {-1, 0, 1, 0};
	int chkx[2][4] = { {-1, 1, -1, -1}, {1, 1, 1, -1} };
	int chky[2][4] = { {-1, -1, 1, -1}, {-1, 1, 1, 1} };
	cin>>d;
	for (i = 0; i < d; i++) {
		cin>>n;
		s.resize(n);
		for (j = 0; j < n; j++)
			for (k = 0; k < n; k++)
				s[k][j] = ' ';
		x = 0; y = n-1; dir = 0;
		turn = 0;
		k = 0;
		while (turn<4) {
			s[x][y] = '#';
			if ( isOver(x+dx[dir], y+dy[dir], n) ) {
				dir = (dir+1)%4;
				turn++;
				continue;
			}
			if ( !isOver(x+2*dx[dir], y+2*dy[dir], n) ) {
				if (s[x+2*dx[dir]][y+2*dy[dir]] == '#') {
					dir = (dir+1)%4;
					turn++;
					continue;
				}
			}
			if ( !isOver(x+chkx[0][dir], y+chky[0][dir], n) )
				if (s[x+chkx[0][dir]][y+chky[0][dir]] == '#') break;
			if ( !isOver(x+chkx[1][dir], y+chky[1][dir], n) )
				if (s[x+chkx[1][dir]][y+chky[1][dir]] == '#') break;
			x += dx[dir];
			y += dy[dir];
			turn = 0;
		}
		if (i) cout<<endl;
		for (j = 0; j < n; j++) {
			for (k = 0; k < n; k++) {
				cout<<s[k][j];
			}
			cout<<endl;
		}
	}
	return 0;
}

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

int main()
{
    int d;
    cin>>d;
    for(int h=0;h<d;h++)
    {
        if(h)cout<<endl;
        int n;cin>>n;
        int a[150][150]={};
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)a[i][j]=1;
        for(int i=1;i<=n;i++)a[1][i]=a[n][i]=a[i][1]=a[i][n]=0;
        a[2][n]=1;
        int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
        int x=3,y=n;

        for(int k=0;k<1000;k++)
        {
            while(1)
            {
                int X=x+dx[k%4];
                int Y=y+dy[k%4];
                int s=0;
                for(int j=0;j<4;j++)
                {
                    if(a[X+dx[j]][Y+dy[j]]==0)s++;
                }
                if(s!=1)break;

                a[X][Y]=0;
                x=X;
                y=Y;
            }
        }

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cout<<(a[j][i]?' ':'#');
            }
            cout<<endl;
        }
    }
}

C++: #include<iostream>
#include<queue>
#include<map>
#include<cstring>
using namespace std;
#define fr first
#define sc second
#define INF 100
typedef pair<int,int>Pt;
int main(){
  int d,n,dx[]={-1,0,1,0},dy[]={0,1,0,-1};
  char mas[101][101]; //上右下左
  bool ok = false;
  cin >> d;
  while(d--){
    memset(mas,' ',sizeof(mas));
    cin >> n;
    for(int i=0,x=n-1,MUKI=0,y=0;i<n;i++,x+=dx[MUKI],y+=dy[MUKI]) mas[x][y] = '#';
    for(int i=0,cnt=n,x=0,y=0,MUKI=1;i<n-1;i++,MUKI=(MUKI+1)%4){
      for(int j=0;j<cnt;j++,x+=dx[MUKI],y+=dy[MUKI]) mas[x][y] = '#';
      x-=dx[MUKI],y-=dy[MUKI];
      if(i%2==1)cnt-=2;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++) cout << mas[i][j];
      cout << endl;
    }
    if(!ok) cout << endl,ok = true;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#define int long long
using namespace std;
int d, n;
char m[110][110];
signed main() {
	cin >> d;
	for (int v = 0; v < d; v++) {
		if (v)cout << endl;
		cin >> n;
		for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)m[i][j] = ' ';
		int x = -1, y = 0, p = n - 1, q = 0;
		while (1) {
			m[p][q] = '#';
			if (p + x < 0) {
				if (n == 1)break;
				x = 0; y = 1;
			}
			if (q + y >= n) {
				if (n == 2)break;
				x = 1; y = 0;
			}
			if (p + x >= n) {
				if (n == 3)break;
				x = 0; y = -1;
			}
			if (y == -1 && m[p][q - 2] == '#') {
				if (m[p - 1][q + 1] == '#')break;
				x = -1; y = 0;
				if (m[p - 2][q] == '#')break;
			}
			if (x == -1 && m[p - 2][q] == '#') {
				if (m[p + 1][q + 1] == '#')break;
				x = 0; y = 1;
				if (m[p][q + 2] == '#')break;
			}
			if (y == 1 && m[p][q + 2] == '#') {
				if (m[p + 1][q - 1] == '#')break;
				x = 1; y = 0;
				if (m[p + 2][q] == '#')break;
			}
			if (x == 1 && m[p + 2][q] == '#') {
				if (m[p - 1][q - 1] == '#')break;
				x = 0; y = -1;
				if (m[p][q - 2] == '#')break;
			}
			p += x; q += y;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++)cout << m[i][j];
			cout << endl;
		}
	}
}

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

using namespace std;

main(){
  int n;
  char data[105][105];
  int dx[] = {0, 1, 0, -1};
  int dy[] = {-1, 0, 1, 0};
  int t;
  cin >> t;
  //t = 100;
  for(int tc=0;tc<t;tc++){
    cin >> n;
    //n = tc+1;
    if(tc != 0) cout << endl;
    if(n == 1){
      cout << "#" << endl;
      continue;
    }
    int y = n-1;
    int x = 0;
    fill(data[0], data[100], ' ');
    data[y][x] = '#';
    int d = 0;
    while(1){
      int ty = y + dy[d];
      int tx = x + dx[d];
      if(ty<0 || ty>=n){ d = (d+1)%4; continue; }
      if(tx<0 || tx>=n){ d = (d+1)%4; continue; }
      if(data[ty][tx] == '#') break;
      int tty = ty + dy[d];
      int ttx = tx + dx[d];
      if((tty>=0 || tty<n) && (ttx>=0 || ttx<n)){
	if(data[tty][ttx] == '#'){
	  d = (d+1)%4; 
	  continue; 
	}
      }
      int cnt = 0;
      for(int i=0;i<4;i++){
	int tty = ty + dy[i];
	int ttx = tx + dx[i];
	if(tty<0 || tty>=n) continue;
	if(ttx<0 || ttx>=n) continue;
	if(data[tty][ttx] == '#') cnt++;
      }
      if(cnt > 1) break;
      data[ty][tx] = '#';
      y = ty;
      x = tx;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cout << data[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}