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<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define it vector<int>::iterator
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
using namespace std;
int main(){
	int m;
	cin>>m;
	while(m--){
		int n;
		cin>>n;
		vector<vector<char> >in(n+4,vector<char>(n+4,' '));
		rep(i,n+4)rep(j,n+4)
		if(!i||!j||i==n+3||j==n+3)in[i][j]='#';
		int x=n+2,y=2;
		int dx[]={-1,0,1,0};
		int dy[]={0,1,0,-1};
		int co=-1;
		rep(k,n){
			co++;
			if(co==4)co=0;
			while(1){
				int xx=x+dx[co];
				int yy=y+dy[co];
				bool q=true;
				rep(i,4){if(co!=(i+2)%4&&in[xx+dx[i]][yy+dy[i]]=='#')q=false;}
				if(!q)break;
				x=xx;y=yy;
				in[x][y]='#';
			}
		}
		rep(i,n){rep(j,n)cout<<in[i+2][j+2];cout<<endl;}
		if(m)cout<<endl;
	}
}

C++: #include <cstdio>

#define MAX 150

char sp[MAX][MAX];
//u r d l
const int dx[] = {0,1,0,-1};
const int dy[] = {-1,0,1,0};

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

void make(int n){
  int x = 0 , y = n-1 ,dir = 0;

  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      sp[i][j] = '.';
    }
  }

  int end = 0;
  sp[y][x] = '#';
  while(true){
    int nx = x + dx[dir];
    int ny = y + dy[dir];  

    bool flg = true;
    if(!range(nx,ny,n)){
      flg = false;
    }

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

      if((x == nnx && y == nny) 
	 || !range(nnx,nny,n)){
	continue;
      }

      if(sp[nny][nnx] == '#'){
	flg = false;
	break;
      }
    }

    if(!flg){
      dir++;
      dir = dir % 4;
      end++;
      if(end == 4) return;
    }else{
      end = 0;
    }

    if(flg){
      sp[ny][nx] = '#';
      x = nx;
      y = ny;
    }
  }
}

int main(){
  int d,n;
  bool ok = false;

  scanf("%d" ,&d);
  while(d--){
    scanf("%d" ,&n);

    if(ok){
      puts("");
    }
    ok = true;

    make(n);
    for(int i = 0 ; i < n ; i++){
      for(int j = 0 ; j < n ; j++){
	if(sp[i][j] == '.'){
	  printf(" ");
	}else{
	  printf("%c" ,sp[i][j]);
	}
      }
      puts("");
    }   
  }

  return 0;
}

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));
    if(n==2) a[1][0]=1;
    REP(i,n){
      REP(j,n)
        printf("%c",(a[j][i]==1)?'#':' ');
      cout << endl;
    }
    cout << endl;
  }
  return 0;
}

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++;
			}
		}
		if (i) printf("\n");
		for (int j = 0; j < d; j++) printf("%s\n", c[j]);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>

int d,n;
int fie[101][101];
int xmax[101];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};

int max(int a,int b){
	if(a>b)return a;
	return b;
}

int main(void){
	scanf("%d",&d);
	for(int data=0;data<d;data++){
		if(data>0)printf("\n");
		scanf("%d",&n);
		memset(fie,0,sizeof(fie));
		memset(xmax,0,sizeof(xmax));
		for(int i=0;i<n;i++){
			fie[0][i]=1;
		}
		int len=0,mlen=n-1,x=0,y=0,dir=1,cnt=0;
		while(mlen>0){
			x+=dx[dir];
			y+=dy[dir];
			xmax[y]=max(xmax[y],x);
			fie[x][y]=1;
			len++;
			if(len==mlen){
				cnt++;
				if(mlen==1)break;
				dir=(dir+1)%4;
				len=0;
			}
			if(cnt==2){
				mlen-=2;
				cnt=0;
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				printf("%c",fie[j][i]==1?'#':' ');
			}
			printf("\n");
		}
	}
	return 0;
}

C++: #include <stdio.h>
#pragma warning(disable : 4996)
int n, d, x, y, e, f; char c[100][100];
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]);
	}
	return 0;
}