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<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int m,n,x,y,p,xx,yy,d,l;
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
main(){
  cin>>m;
  while(m--){
    cin>>n;l=0;
    string s[n];
    r(i,n)r(j,n)s[i]+='#';
    s[n-1][1]=' ';
    xx=0,yy=n-1,d=0;
    while(1){
      int c=0;
      r(i,4){
        if(!l)c--;
        y=dy[i]+yy,x=dx[i]+xx;
        if(y<0||y>=n||x<0||x>=n){
          c++;
          continue;
        }
        else if(s[y][x]==' ')c++;
      }
      if(c>=3)break;
      y=dy[d]+yy,x=dx[d]+xx;
      if(y<0||y>=n||x<0||x>=n)d=(d+1)%4;
      else if(s[y][x]==' ')d=(d+1)%4;
      else{
        yy=y+dy[d],xx=x+dx[d];
        if(yy>=0&&yy<n&&xx>=0&&xx<n&&s[yy][xx]!=' '){
          p=(d+1)%4;
          yy=y+dy[p],xx=x+dx[p];
          s[yy][xx]=' ';
        }
        yy=y,xx=x;
      }l++;
    }
    r(i,n)cout<<s[i]<<endl;
  }
}

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

int main()
{
	int d;

	while( cin >> d ){
		for( int i = 0; i < d; ++i ){
			if( i != 0 ) cout << endl << endl;
			int n;
			cin >> n;

			int size = n + 4;
			char* map = new char[ size * size ]();
			for( int j = 0; j < size; ++j ){
				for( int k = 0; k < size; ++k ){
					map[ j * size + k ] = ' ';
				}
			}
			for( int j = 0; j < size; ++j ){
				map[ j ] = map[ ( size - 1 ) * size + j ] = map[ size * j ] = map[ ( j + 1 ) * size - 1 ] = '#';
			}
			for( int j = 2; j < size - 2; ++j ){
				map[ j * size + 2 ] = '#';
			}
			int x = 2, y = 2;
			int dir = 1;
			int dirCount = 0;
			int straightCount = 0;
			while( dirCount < 2 ){
				map[ y * size + x ] = '#';
				++straightCount;
				int nx, ny;
				switch( dir ){
					case 0: nx = x; ny = y - 2; break;
					case 1: nx = x + 2; ny = y; break;
					case 2: nx = x; ny = y + 2; break;
					case 3: nx = x - 2; ny = y; break;
				}
				if( map[ ny * size + nx ] == '#' ){
					if( straightCount < 3 ){
						break;
					}
					dir = ( dir + 1 ) % 4;
					++dirCount;
					straightCount = 0;
					continue;
				}else{
					dirCount = 0;
				}
				switch( dir ){
					case 0: y -= 1; break;
					case 1: x += 1; break;
					case 2: y += 1; break;
					case 3: x -= 1; break;
				}
			}

			for( int p = 2; p < size - 2; ++p ){
				for( int q = 2; q < size - 2; ++q ){
					cout << map[ p * size + q ];
				}
				if( p != size - 3 ) cout << endl;
			}

			delete[] map;
		}
	}

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
int main() {
	int D; cin >> D;
	while (D--) {
		int N; cin >> N;
		vector<vector<char>>field(N, vector<char>(N, ' '));
		int way = 3;
		int nx = 0;
		int ny = N - 1;
		bool flag = true;
		while (flag) {
			field[ny][nx] = '#';
			if (N == 1)break;
			for (int i = 0; i < 2; ++i) {
				int nextx = nx + dx[way];
				int nexty = ny + dy[way];
				if (nextx >= 0 && nextx < N&&nexty >= 0 && nexty < N) {
					bool aok = true;
					for (int w = -1; w <= 1; ++w) {
						int nextway = (way + 4 + w) % 4;
						int next2x = nextx + dx[nextway];
						int next2y = nexty + dy[nextway];
						if (next2x >= 0 && next2x < N&&next2y >= 0 && next2y < N) {
							if (field[next2y][next2x] == '#') {
								aok = false;
							}
						}
					}
					if(!aok){
						if(i==1)flag = false;
						
					}
					else {
						nx = nextx;
						ny = nexty;
						break;
					}
				}
				way = (way + 3) % 4;
			}
			
		}
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				cout << field[i][j];
			}
			cout << endl;
		}
		if(N-1)cout << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
const int MOD=100000;
int main(){
	int d,n;
	cin>>d;
	while(d--){
		cin>>n;
		char fld[n][n],a,b;
		REP(i,(n+1)/2){
			if(i%2==0){a='#';b=' ';}
			else{a=' ';b='#';}
			REP(j,(n-i*2)){
				fld[i+j][i]=a;
				fld[i][i+j]=a;
				fld[n-1-i][i+j]=a;
				fld[i+j][n-1-i]=a;
			}
			fld[i+1][n-1-i]=b;
		}
		REP(i,n){
			REP(j,n)cout<<fld[j][i];
			cout<<endl;
		}
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <cstring>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 
int nx,ny,nr;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int a[100][100];
bool plot(int x, int y, int r, int n){
  int count = 0;
  bool flag = true;
  if((x+2*dx[r]>=0&&x+2*dx[r]<n)&&
      y+2*dy[r]>=0&&y+2*dy[r]<n&&a[x+2*dx[r]][y+2*dy[r]]==1)
    return false;
  x+=dx[r]; y+=dy[r];
  while((x>=0&&x<n)&&(y>=0&&y<n)){
    if((x+2*dx[r]>=0&&x+2*dx[r]<n)&&y+2*dy[r]>=0&&y+2*dy[r]<n&&a[x+2*dx[r]][y+2*dy[r]]==1) flag = false;
    a[x][y] = 1;
    x+=dx[r]; y+=dy[r];
    count++;
    if(!flag) break;
  }
  x-=dx[r];y-=dy[r]; nx=x; ny=y; nr=(r+1)%4;
  if(count<2) return false;
  return true;
}
int main(void){
  int d;
  cin>>d;
  while(d--){
    int n;
    cin>>n;
    memset(a,0,sizeof(a));
    a[0][n-1]=1;
    nx=0; ny=n-1; nr=3;
    while(plot(nx,ny,nr,n));
    REP(i,n){
      REP(j,n)
        printf("%c",(a[j][i]==1)?'#':' ');
      cout << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

typedef vector<string> vs;

int n;

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

int main() {
	int N;
	cin >> N;
	while (N--) {
		cin >> n;
		vs str(n);
		REP(i, n) str[i].resize(n, ' ');
		
		int dx[4] = { 0, 1, 0, -1 };
		int dy[4] = { -1, 0, 1, 0 };
		int nx = 0, ny = n - 1, d = 0, cnt = 0;
		
		while (true) {
			if (cnt == n) break;
			str[ny][nx] = '#';
			
			if (!check(nx + dx[d], ny + dy[d]) ||
				(check(nx + dx[d]*2, ny + dy[d]*2) && str[ny + dy[d]*2][nx + dx[d]*2] == '#') ) {
				d = (d + 1) % 4;
				cnt++;
			}
			else {
				nx += dx[d]; ny += dy[d];
			}
		}
		
		REP(i, n) cout << str[i] << endl;
		if (N != 0) putchar('\n');
	}
	return 0;
}