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
		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 <bits/stdc++.h>

using namespace std;

char MapData[100][100];
bool Finished;
int N;

bool check(int x, int y, int i) {
	 
	if (i == 0 || i==2) {
		if (MapData[y][x-1] == ' ' && MapData[y][x+1] == ' ') {
			return true;
		}
	}
	else if (i == 1 || i==3) {
		if (MapData[y-1][x] == ' ' && MapData[y+1][x] == ' ') {
			return true;
		}
	}
	return false;
}

void solve(int x, int y, int direction) {
	int dx[] = { 0,2,0,-2 };
	int dy[] = { -2,0,2,0 };
	int currentX, currentY, i, cnt;

	cnt = 0;
	for (i = direction; cnt < 4; i++) {
		currentX = x + dx[i%4];
		currentY = y + dy[i%4];
		if (currentX<0 || currentX>N-1 || currentY<0 || currentY>N-1) {
			continue;
		}
		if (MapData[currentY][currentX] == ' ') {
			switch (i%4)
			{
			case 0:currentY++; break;
			case 1:currentX--; break;
			case 2:currentY--; break;
			case 3:currentX++; break;
			}
			if (MapData[currentY][currentX] == ' ' && check(currentX, currentY, i%4)) {
				MapData[currentY][currentX] = '#';
				solve(currentX, currentY,i%4);
			}
		}
		if (Finished) {
			return;
		}
		cnt++;
	}
	if (cnt == 4) {
		Finished = true;
	}
}

int main() {
	int d;

	cin >> d;
	while (d--) {
		cin >> N;

		memset(MapData, ' ', sizeof(MapData));
		Finished = false;
		for (int i = 0; i < N; i++) {
			MapData[0][i] = '#';
			MapData[i][0] = MapData[i][N - 1] = '#';
			MapData[N - 1][i] = '#';
		}
		if (N > 1) {
			MapData[N - 1][1] = ' ';
		}
		if (N > 4) {
			solve(2, N - 1, 0);
		}


		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				cout << MapData[i][j];
			}
			cout << endl;
		}
		if (d != 0) {
			cout << endl;
		}
	}

	return 0;
}

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

bool field[100][100];

void migi(int y,int x,int j)
{
	for(int i=1;i<=j;i++){
		field[y][x+i] = 1;
	}
}
void sita(int y,int x,int j)
{
	if(j <= 1)
		return;
	for(int i=1;i<=j;i++){
		field[y+i][x] = 1;
	}
}
void hidari(int y,int x,int j)
{
	for(int i=1;i<=j;i++){
		field[y][x-i] = 1;
	}
}
void ue(int y,int x,int j)
{
	if(j <= 1)
		return;
	for(int i=1;i<=j;i++){
		field[y-i][x] = 1;
	}
}	
int main()
{
	int a;
	cin >> a;
	for(int i=0;i<a;i++){
		int n;
		cin >> n;
		for(int j=0;j<n;j++){
			for(int k=0;k<n;k++){
				field[j][k] = 0;
			}
		}
		int y=n-1,x=0;
		for(int j=0;j<n;j++){
			field[y-j][x] = 1;
		}
		y = 0;
		
		for(int j=n-1;j>=0;j-=2){
			migi(y,x,j);
			x = x+j;
			sita(y,x,j);
			y = y+j;			
			j-=2;
			hidari(y,x,j);
			x = x-j;
			ue(y,x,j);
			y = y-j;
		}
		
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(field[i][j] == 1)
					cout << "#";
				else
					cout << " ";
			}
			cout << endl;
		}
		cout << endl;
	}
	return 0;
}

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

int n;
char c[999][999];
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};

bool chk(int y, int x){
  if(y > -1 && x > -1 && y < n+2 && x < n+2 && c[y][x] != ' ') return true;
  return false;
}

void solve(int y, int x, int p){

  int py = y + dy[p], px = x + dx[p];
  int pyy = y + dy[p]*3, pxx = x + dx[p]*3;
  
  if(n == 1){ c[y][x] = '#'; return; }
  if(y < 0 || x < 0 || y == n || x == n || c[y][x] != ' ') return;
  for(int i=0;i<4;i++) if(c[py][px] != ' ' && (i+2)%4 != p) return;
  
  c[y][x] = '#';
  
  if((!py || chk(pyy, pxx)) && !p) p++;
  else if((px == n-1 || chk(pyy, pxx)) && p == 1) p++;
  else if((py == n-1 || chk(pyy, pxx)) && p == 2) p++;
  else if((!px || chk(pyy, pxx)) && p == 3) p = 0;
  
  solve(py, px, p);
}

int main(){

  int l;
  cin >> l;
  while(l--){
    memset(c, ' ', sizeof(c));
    cin >> n;
    solve(n-1,0,0);
    for(int i=0;i<n;i++) c[i][n] = '\0', cout << c[i] << endl;
  }
}

C++: #include<stdio.h>
int main()
{
	const int N = 100;
	char a[N][N];

	int n,d,caseCount=0,i,j,x,y,dd[]={-1,0,1,0},angle,c;
	scanf("%d",&d);
	while(d--)
	{
		scanf("%d",&n);
		for(i=0;i<n;++i)for(j=0;j<n;++j)a[i][j]=' ';

		x=0;y=n-1;
		angle=0;
		for(c=0;c<3;++c)
		{
			for(;x>=0&&y>=0&&x<n&&y<n; x+=dd[(angle+1)%4],y+=dd[angle])
			{
				a[y][x]='#';
			}
			x-=dd[(angle+1)%4];
			y-=dd[angle];
			angle=(angle+1)%4;;
		}
		if(n>2)
		{
			for(;n;)
			{
				c=0;
				for(; a[y+2*dd[angle]][x+2*dd[(angle+1)%4]]!='#'; x+=dd[(angle+1)%4],y+=dd[angle])
					a[y][x]='#',c++;
				if(c<2) break;
				angle=(angle+1)%4;;
			}
			a[y][x]='#';
		}
		else if(n==2)
			a[n-1][n-1]=' ';

		printf("%s",(caseCount++?"\n":""));
		for(i=0;i<n;++i)
		{
			for(j=0;j<n;++j)
				putchar(a[i][j]);
			putchar('\n');
		}
	}
	return 0;
}

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

using namespace std;

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

inline int next(int d)
{
    return (d + 1) % 4;
}

int main()
{
    int dataset, n;

    cin >> dataset;
    while (dataset--) {
        cin >> n;

        vector<vector<bool> > map(n, vector<bool>(n, false));
        int d = 0, x = 0, y = n - 1;
        bool flag = true;

        while (flag) {
            map[y][x] = true;

            int nx = x + dx[d];
            int ny = y + dy[d];

            if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
                d = next(d);
                x += dx[d];
                y += dy[d];
                continue;
            }

            for (int i = 0; i < 4; ++i) {
                if ((i + 2) % 4 == d)
                    continue;
                int nnx = nx + dx[i];
                int nny = ny + dy[i];

                if (nnx < 0 || nnx >= n || nny < 0 || nny >= n)
                    continue;
                if (map[nny][nnx]) {
                   d =  next(d);
                   break;
                }
            }
            x += dx[d];
            y += dy[d];

            for (int i = 0; i < 4; ++i) {
                if ((i + 2) % 4 == d)
                    continue;
                nx = x + dx[i];
                ny = y + dy[i];

                if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                    continue;
                if (map[ny][nx]) {
                    flag = false;
                    break;
                }
            }
        }

        // Output
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (map[i][j])
                    cout << "#";
                else
                    cout << " ";
            }
            cout << "\n";
        }
    }

    return 0;
}